from Crypto.Util.number import *
import codecs
import string
import random
import hashlib
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from flag import flag
p = getStrongPrime(512)
q = getStrongPrime(512)
n = p*q
e = 137
key = ''.join(random.sample(string.ascii_lowercase, 10))
rot13_key = codecs.encode(key, 'rot13')
key = key.encode()
rot13_key = rot13_key.encode()
print("n =", n)
print("e =", e)
print("c1 =", pow(bytes_to_long(key), e, n))
print("c2 =", pow(bytes_to_long(rot13_key), e, n))
key = hashlib.sha256(key).digest()
cipher = AES.new(key, AES.MODE_ECB)
print("encyprted_flag = ", cipher.encrypt(flag))
n = 105270965659728963158005445847489568338624133794432049687688451306125971661031124713900002127418051522303660944175125387034394970179832138699578691141567745433869339567075081508781037210053642143165403433797282755555668756795483577896703080883972479419729546081868838801222887486792028810888791562604036658927
e = 137
c1 = 16725879353360743225730316963034204726319861040005120594887234855326369831320755783193769090051590949825166249781272646922803585636193915974651774390260491016720214140633640783231543045598365485211028668510203305809438787364463227009966174262553328694926283315238194084123468757122106412580182773221207234679
c2 = 54707765286024193032187360617061494734604811486186903189763791054142827180860557148652470696909890077875431762633703093692649645204708548602818564932535214931099060428833400560189627416590019522535730804324469881327808667775412214400027813470331712844449900828912439270590227229668374597433444897899112329233
encyprted_flag = b"\xdb'\x0bL\x0f\xca\x16\xf5\x17>\xad\xfc\xe2\x10$(DVsDS~\xd3v\xe2\x86T\xb1{xL\xe53s\x90\x14\xfd\xe7\xdb\xddf\x1fx\xa3\xfc3\xcb\xb5~\x01\x9c\x91w\xa6\x03\x80&\xdb\x19xu\xedh\xe4"
RSA 암호화
c1
과 c2
는 각각 key와 rot13_key를 RSA로 암호화한 값n
은 두 개의 강력한 소수 p,qp, qp,q의 곱으로 생성됨e
는 137로 주어짐ROT13
rot13_key
는 key
를 ROT13으로 변환한 값AES 암호화
key
를 SHA-256
으로 해싱한 값을 AES의 키로 사용함AES.MODE_ECB
를 사용하여 플래그를 암호화함목표
c1
, c2
, n
, e
를 활용하여 원본 key
를 복구key
를 통해 플래그를 복호화from Crypto.Util.number import * # 숫자 관련 유틸리티 (RSA 암호화, 바이트 변환 등)
import codecs # 문자열 변환 (ROT13)
import string # 알파벳 문자열 처리
import random # 랜덤 값 생성
import hashlib # 해싱 함수 (SHA-256)
from Crypto.Cipher import AES # AES 암호화
from Crypto.Random import get_random_bytes # 랜덤 바이트 생성
from flag import flag # 플래그를 포함한 외부 파일
p = getStrongPrime(512)
q = getStrongPrime(512)
n = p * q
e = 137
getStrongPrime(512)
n = p * q
e = 137
key = ''.join(random.sample(string.ascii_lowercase, 10))
rot13_key = codecs.encode(key, 'rot13')
key
key = "abcdefghij"
rot13_key
key
에 ROT13 변환을 적용한 값key = "abcdefghij"
→ rot13_key = "nopqrstuvw"
key = key.encode()
rot13_key = rot13_key.encode()
key
와 rot13_key
를 바이트 문자열로 변환print("n =", n)
print("e =", e)
print("c1 =", pow(bytes_to_long(key), e, n))
print("c2 =", pow(bytes_to_long(rot13_key), e, n))
bytes_to_long
key
, rot13_key
)을 정수로 변환pow(m, e, n)
key
와 rot13_key
를 각각 공개키 n,e 로 암호화한 결과를 출력c1
: key
를 암호화한 결과c2
: rot13_key
를 암호화한 결과key = hashlib.sha256(key).digest()
key
를 SHA-256으로 해싱하여 32바이트 길이의 키를 생성cipher = AES.new(key, AES.MODE_ECB)
print("encyprted_flag = ", cipher.encrypt(flag))
AES.new(key, AES.MODE_ECB)
cipher.encrypt(flag)
n
: RSA 모듈러 (공개키의 일부)e
: RSA 공개 지수c1
: RSA로 암호화된 key
c2
: RSA로 암호화된 rot13_key
encyprted_flag
: AES로 암호화된 플래그RSA 파라미터 생성
랜덤 키 생성
key
생성 및 ROT13 변환RSA 암호화
key
와 rot13_key
를 RSA 암호화하여 c1
과 c2
를 출력SHA-256 해시
key
를 SHA-256으로 해싱하여 AES 대칭키 생성플래그 AES 암호화
from Crypto.Util.number import *
from Crypto.Cipher import AES
import hashlib
import codecs
from sympy import factorint
# output.txt에서 제공된 값
n = 105270965659728963158005445847489568338624133794432049687688451306125971661031124713900002127418051522303660944175125387034394970179832138699578691141567745433869339567075081508781037210053642143165403433797282755555668756795483577896703080883972479419729546081868838801222887486792028810888791562604036658927
e = 137
c1 = 16725879353360743225730316963034204726319861040005120594887234855326369831320755783193769090051590949825166249781272646922803585636193915974651774390260491016720214140633640783231543045598365485211028668510203305809438787364463227009966174262553328694926283315238194084123468757122106412580182773221207234679
c2 = 54707765286024193032187360617061494734604811486186903189763791054142827180860557148652470696909890077875431762633703093692649645204708548602818564932535214931099060428833400560189627416590019522535730804324469881327808667775412214400027813470331712844449900828912439270590227229668374597433444897899112329233
encrypted_flag = b"\xdb'\x0bL\x0f\xca\x16\xf5\x17>\xad\xfc\xe2\x10$(DVsDS~\xd3v\xe2\x86T\xb1{xL\xe53s\x90\x14\xfd\xe7\xdb\xddf\x1fx\xa3\xfc3\xcb\xb5~\x01\x9c\x91w\xa6\x03\x80&\xdb\x19xu\xedh\xe4"
# 1. 소인수분해
factors = factorint(n)
p, q = list(factors.keys())
print("p =", p)
print("q =", q)
# 2. d 계산
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
# 3. key 복구
key_long = pow(c1, d, n)
key = long_to_bytes(key_long).decode()
# 4. rot13_key 확인 (ROT13 변환)
rot13_key = codecs.encode(key, 'rot13')
# 5. SHA-256 해시로 AES 키 생성
aes_key = hashlib.sha256(key.encode()).digest()
# 6. AES 복호화
cipher = AES.new(aes_key, AES.MODE_ECB)
flag = cipher.decrypt(encrypted_flag).decode()
print("Recovered key:", key)
print("Recovered rot13_key:", rot13_key)
print("Flag:", flag)
from Crypto.Util.number import *
from Crypto.Cipher import AES
import hashlib
import codecs
from math import gcd
from random import randint
# output.txt에서 제공된 값
n = 105270965659728963158005445847489568338624133794432049687688451306125971661031124713900002127418051522303660944175125387034394970179832138699578691141567745433869339567075081508781037210053642143165403433797282755555668756795483577896703080883972479419729546081868838801222887486792028810888791562604036658927
e = 137
c1 = 16725879353360743225730316963034204726319861040005120594887234855326369831320755783193769090051590949825166249781272646922803585636193915974651774390260491016720214140633640783231543045598365485211028668510203305809438787364463227009966174262553328694926283315238194084123468757122106412580182773221207234679
c2 = 54707765286024193032187360617061494734604811486186903189763791054142827180860557148652470696909890077875431762633703093692649645204708548602818564932535214931099060428833400560189627416590019522535730804324469881327808667775412214400027813470331712844449900828912439270590227229668374597433444897899112329233
encrypted_flag = b"\xdb'\x0bL\x0f\xca\x16\xf5\x17>\xad\xfc\xe2\x10$(DVsDS~\xd3v\xe2\x86T\xb1{xL\xe53s\x90\x14\xfd\xe7\xdb\xddf\x1fx\xa3\xfc3\xcb\xb5~\x01\x9c\x91w\xa6\x03\x80&\xdb\x19xu\xedh\xe4"
# Pollard's Rho 소인수분해 알고리즘
def pollards_rho(n):
x = randint(2, n - 1)
y = x
c = randint(1, n - 1)
d = 1
while d == 1:
x = (x * x + c) % n
y = (y * y + c) % n
y = (y * y + c) % n
d = gcd(abs(x - y), n)
if d == n:
return None # 실패하면 None 반환
return d
# 1. 소인수분해
p = pollards_rho(n)
if not p:
raise ValueError("소인수분해 실패!")
q = n // p
print("p =", p)
print("q =", q)
# 2. RSA 개인 키 d 계산
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
# 3. key 복구
key_long = pow(c1, d, n)
key = long_to_bytes(key_long).decode()
# 4. rot13_key 확인 (ROT13 변환)
rot13_key = codecs.encode(key, 'rot13')
# 5. SHA-256 해시로 AES 키 생성
aes_key = hashlib.sha256(key.encode()).digest()
# 6. AES 복호화
cipher = AES.new(aes_key, AES.MODE_ECB)
flag = cipher.decrypt(encrypted_flag).decode()
print("Recovered key:", key)
print("Recovered rot13_key:", rot13_key)
print("Flag:", flag)
from Crypto.Util.number import *
from Crypto.Cipher import AES
import hashlib
import codecs
# 주어진 값들
n = 105270965659728963158005445847489568338624133794432049687688451306125971661031124713900002127418051522303660944175125387034394970179832138699578691141567745433869339567075081508781037210053642143165403433797282755555668756795483577896703080883972479419729546081868838801222887486792028810888791562604036658927
e = 137
c1 = 16725879353360743225730316963034204726319861040005120594887234855326369831320755783193769090051590949825166249781272646922803585636193915974651774390260491016720214140633640783231543045598365485211028668510203305809438787364463227009966174262553328694926283315238194084123468757122106412580182773221207234679
c2 = 54707765286024193032187360617061494734604811486186903189763791054142827180860557148652470696909890077875431762633703093692649645204708548602818564932535214931099060428833400560189627416590019522535730804324469881327808667775412214400027813470331712844449900828912439270590227229668374597433444897899112329233
encrypted_flag = b"\xdb'\x0bL\x0f\xca\x16\xf5\x17>\xad\xfc\xe2\x10$(DVsDS~\xd3v\xe2\x86T\xb1{xL\xe53s\x90\x14\xfd\xe7\xdb\xddf\x1fx\xa3\xfc3\xcb\xb5~\x01\x9c\x91w\xa6\x03\x80&\xdb\x19xu\xedh\xe4"
# 소인수분해 결과: p, q (Factordb나 소인수분해 도구로 구해야 함)
p = 123456789012345678901234567890123456789 # 예시 값 (실제 p 사용)
q = n // p
# RSA 복호화에 필요한 값 계산
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi) # 비밀 키 계산
# RSA 복호화로 key 복구
key_long = pow(c1, d, n)
key = long_to_bytes(key_long).decode()
# ROT13로 rot13_key 확인
rot13_key = codecs.encode(key, 'rot13')
rot13_key_long = pow(c2, d, n)
assert rot13_key == long_to_bytes(rot13_key_long).decode(), "ROT13 key does not match!"
# SHA-256 해시로 AES 키 생성
aes_key = hashlib.sha256(key.encode()).digest()
# AES 복호화
cipher = AES.new(aes_key, AES.MODE_ECB)
flag = cipher.decrypt(encrypted_flag).decode()
# 결과 출력
print("Recovered key:", key)
print("Recovered rot13_key:", rot13_key)
print("Decrypted Flag:", flag)