def challenge_12(): with open('12.txt') as f: suffix = b64decode(f.read()) blocksize = detect_blocksize(suffix) x = 2 * blocksize * b'A' ciphertext = deterministic_oracle(x, suffix) chunks = chunk_into(ciphertext, blocksize) if len(chunks) > len(set(chunks)): logger.info("Cipher is running in ECB mode") datalen = len(deterministic_oracle(b'', suffix)) logger.critical(datalen) known = list() for offset in range(1, datalen): padder = (datalen - offset) * b'A' rtable = dict() for c in range(256): char = bytes((c, )) plain = padder + b''.join(known) + char # logger.debug(plain) rtable[deterministic_oracle(plain, suffix)[:datalen]] = char ciphertext = deterministic_oracle(padder, suffix) try: known.append(rtable[ciphertext[:datalen]]) except KeyError: return unpad(b''.join(known))
def main(): ct = encrypt() iv, *blocks = chunk_into(ct, BLOCKSIZE) logger.info(iv) logger.info(blocks) ret = list() prev_block = iv for block in blocks: known = list() while len(known) < BLOCKSIZE: i = len(known) known_trailer = bytes((ord(x) ^ y ^ (i + 1)) for x, y in (zip(known, prev_block[-i:]))) logger.debug("Trailer: {}".format(known_trailer)) for c in range(256): char = bytes((c,)) fake_block = b'\x00' * (BLOCKSIZE - len(known_trailer) - 1) + char + known_trailer if padding_oracle(fake_block + block): Ca = char[0] padsize = len(known) + 1 known.insert(0, bytes((Ca ^ prev_block[-padsize] ^ padsize,))) logger.info('Decrypted: {}'.format(b''.join(known))) break ret.extend(known) prev_block = block return unpad(b''.join(ret))
def main(): ct = encrypt() iv, *blocks = chunk_into(ct, BLOCKSIZE) logger.info(iv) logger.info(blocks) ret = list() prev_block = iv for block in blocks: known = list() while len(known) < BLOCKSIZE: i = len(known) known_trailer = bytes((ord(x) ^ y ^ (i + 1)) for x, y in (zip(known, prev_block[-i:]))) logger.debug("Trailer: {}".format(known_trailer)) for c in range(256): char = bytes((c, )) fake_block = b'\x00' * (BLOCKSIZE - len(known_trailer) - 1) + char + known_trailer if padding_oracle(fake_block + block): Ca = char[0] padsize = len(known) + 1 known.insert( 0, bytes((Ca ^ prev_block[-padsize] ^ padsize, ))) logger.info('Decrypted: {}'.format(b''.join(known))) break ret.extend(known) prev_block = block return unpad(b''.join(ret))
def decrypt(self, data): assert len(data) % 16 == 0 blocks = chunk_into(data, 16) plain = list() for block in blocks: plain.append(xor_with_key(ECB(self.key).decrypt(block), self._state)) self._state = block return unpad(b''.join(plain))
def decrypt(self, data): assert len(data) % 16 == 0 blocks = chunk_into(data, 16) plain = list() for block in blocks: plain.append( xor_with_key(ECB(self.key).decrypt(block), self._state)) self._state = block return unpad(b''.join(plain))
def data_decry_pack(self,data_pack): key_iv = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] key_iv_pack = struct.pack('%dB'%len(key_iv), *key_iv) key_pack= struct.pack('%dB'%len(self.sess_key), *self.sess_key) #print 'sess_key_pack',binascii.hexlify(key_pack) ackBody_AES = AES.new(key_pack, AES.MODE_CBC, key_iv_pack) print 'rc_Packet_data before decrypt AES',binascii.hexlify(data_pack) data_encryed=tools.unpad(ackBody_AES.decrypt(data_pack)) print 'rc_Packet_data after decrypt AES',binascii.hexlify(data_encryed) #self.msg_len=len(data_encryed) #print 'rc_genPacket msg_len',self.msg_len return data_encryed
def parse_profile(ciphertext): key = base64.b64decode(b'XJeZGgXzH89F0q1vTJfTgw==') plaintext = unpad(ECB(key).decrypt(ciphertext)) logger.critical(plaintext) return kvparse(plaintext)
file = open(namespace.encryptedfile, 'r') except: print('Encrypted file path error') raise SystemExit(1) crypto: str = file.read() file.close() key: bytes = namespace.password.encode() key = pad(key, AES.block_size) iv = b64decode(crypto[0:24]) ct = b64decode(crypto[24:]) cipher: AES = AES.new(key, AES.MODE_CBC, iv) try: data: bytes = unpad(cipher.decrypt(ct), AES.block_size) except ValueError: print("Error password") raise SystemExit(1) try: passwords = data.decode() except UnicodeDecodeError: print('Error password') raise SystemExit(1) path = namespace.sourcefile name = namespace.name try:
def test_challenge_7_aes_ecb(self): cipher = aes.ECB(KEY) with open('7.txt') as f: data = b64decode(f.read()) self.assertEquals(unpad(cipher.decrypt(data)), PLAINTEXT)
def test_challenge_15_unpad(self): self.assertEqual(unpad(b"ICE ICE BABY\x04\x04\x04\x04"), b'ICE ICE BABY') self.assertRaises(ValueError, unpad, b"ICE ICE BABY\x05\x05\x05\x05") self.assertRaises(ValueError, unpad, b"ICE ICE BABY\x01\x02\x03\x04")