can you give me hints about this code -> from random import randint from…
- User
can you give me hints about this code -> from random import randint from Crypto.Util.number import getPrime, long_to_bytes as l2b from Crypto.Cipher import AES from Crypto.Util.Padding import pad from secret import FLAG from hashlib import sha256
class RSAGen: def init(self, bits): self.bits = bits
codedef keygen(self): p = getPrime(self.bits//2) q = getPrime(self.bits//2) self.n = p*q self.e = 0x10001 phi = (p-1)*(q-1) self.d = pow(self.e, -1, phi) return (self.n, self.e), (p, q, phi, self.d) def encrypt(self, m): return pow(m, self.e, self.n) def decrypt(self, c): return pow(c, self.d, self.n)class AESGen: def init(self, size): key1 = randint(1 << size, 1 << (size+1)) key2 = randint(1 << size, 1 << (size+1)) self.k = key1 * key2 assert 40 <= self.k.bit_length() <= 42 self.KEY = sha256(str(self.k).encode()).digest()
codedef encrypt(self, m): cipher = AES.new(self.KEY, AES.MODE_ECB) enc_secret = cipher.encrypt(pad(m, 16)) return enc_secretdef main(): rsa = RSAGen(1024) aes = AESGen(20) pubkey, _ = rsa.keygen() enc_aes_key = l2b(rsa.encrypt(aes.k)) enc_secret = aes.encrypt(FLAG)
codewith open('output.txt', 'w') as f: f.write("Bob :: Hi Alice, here is my public key:\n") f.write(f"({pubkey[0]}, {pubkey[1]})\n") f.write("Alice :: Hi Bob, here is my encrypted AES key, don't forget to sha256-hash it!\n") f.write(f"Encrypted AES Key = {enc_aes_key.hex()}\n") f.write("Bob :: Got it, here is the encrypted secret you requested:\n") f.write(f"Encrypted Secret = {enc_secret.hex()}")if name == 'main': main()
- Assistant
- User
yes it's from security challenge or capture-the-flag (CTF) competition . output.txt contains : Bob :: Hi Alice, here is my public key: (72741423208033405403492275698762968936514657314823442485453559870105200118330405900858299998747406127848670334407387228444029070060538220448699667905891284937839901536444798774307744865631349770845980738192442639071823803272283670943732769371979418893953521892212745135191661138195973757608889180051128601323, 65537) Alice :: Hi Bob, here is my encrypted AES key, don't forget to sha256-hash it! Encrypted AES Key = 4da0230d2b8b46e3a7065f32c46df019739cc002a208cc37767a82c3e94edfc3440fa4b24a32274e35d5ddceaa33505c4f2a57281c3a5d6d4db3a0dbdbb30dbf373241319ce4a7fdd1780b6bafc86e37d283c9f9787c567434e2fc29c988fb05fd411fe4453ea40eb45fc41a423839b485e238dd2530fba284e9b07a0bba6546 Bob :: Got it, here is the encrypted secret you requested: Encrypted Secret = ce8f36aa844ab00319bcd4f86460a10d77492c060b2c2a91615f4cd1f2d0702e76b68f1ec0f11d15704ba52c5dacc60018d5ed87368464acd030ce6230efdbff7b18cba72ccaa9455a6fe6021b908dd1 HOW to recover the FLAG with all consideratins ?
- Assistant
- User
most likely finding RSA private key is not fesible. What if we concentrate on Weak AES Key: How to approach this ?
- Assistant
- User
brutforce is not a solution
- Assistant