This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
generated from BCACTF/chall-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
89 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,82 @@ | ||
Look at this link: https://cims.nyu.edu/~regev/teaching/lattices_fall_2004/ln/rsa | ||
Or just plug in to this website: https://chineseremaindertheorem.com/calculator.php | ||
and take the cube root (forget the modulus) then decode utf8 to get the flag | ||
from Crypto.PublicKey import RSA | ||
from Crypto.Cipher import PKCS1_OAEP | ||
from Crypto.Util.number import getPrime, bytes_to_long, long_to_bytes | ||
import random | ||
import math | ||
import decimal | ||
|
||
decimal.setcontext(decimal.Context(prec=2000)) | ||
|
||
def find_invpow(x,n): | ||
high = 1 | ||
while high ** n < x: | ||
high *= 2 | ||
low = high//2 | ||
while low < high: | ||
mid = (low + high) // 2 | ||
if low < mid and mid**n < x: | ||
low = mid | ||
elif high > mid and mid**n > x: | ||
high = mid | ||
else: | ||
return mid | ||
return mid + 1 | ||
|
||
def inv(a, m) : | ||
|
||
m0 = m | ||
x0 = 0 | ||
x1 = 1 | ||
|
||
if (m == 1) : | ||
return 0 | ||
while (a > 1) : | ||
q = a // m | ||
|
||
t = m | ||
m = a % m | ||
a = t | ||
|
||
t = x0 | ||
|
||
x0 = x1 - q * x0 | ||
|
||
x1 = t | ||
if (x1 < 0) : | ||
x1 = x1 + m0 | ||
|
||
return x1 | ||
|
||
|
||
def findMinX(num, rem, k) : | ||
|
||
# Compute product of all numbers | ||
prod = 1 | ||
for i in range(0, k) : | ||
prod = prod * num[i] | ||
|
||
# Initialize result | ||
result = 0 | ||
|
||
# Apply above formula | ||
for i in range(0,k): | ||
pp = prod // num[i] | ||
result = result + rem[i] * inv(pp, num[i]) * pp | ||
|
||
|
||
return result % prod | ||
|
||
# Driver method | ||
num = [0, 0, 0] | ||
rem = [0, 0, 0] | ||
k = 3 | ||
|
||
num[0] = put the first modulus here | ||
rem[0] = put the first ciphertext here | ||
num[1] = put the second modulus here | ||
rem[1] = put the second ciphertext here | ||
num[2] = put the third modulus here | ||
rem[2] = put the third ciphertext here | ||
|
||
print(findMinX(num,rem,k)==bytes_to_long(message)) | ||
print(long_to_bytes(find_invpow(decimal.Decimal(findMinX(num, rem, k)),3)).decode('utf-8')) |