예제 #1
0
파일: m34.py 프로젝트: fionn/cryptopals
 def forward_and_decrypt(self, key: bytes) -> bytes:
     key = SHA1(key).digest()[:16]
     iv = self.received_cyphertext[-16:]
     message = de_pkcs7(decrypt_aes_cbc(key, iv,
                                        self.received_cyphertext[:-16]))
     self.peer.receive_message(self.received_cyphertext)
     return message
예제 #2
0
파일: m10.py 프로젝트: fionn/cryptopals
def main() -> None:
    with open("data/10.txt", "r") as data:
        cyphertext = b64decode(data.read())

    key = b"YELLOW SUBMARINE"
    iv = bytes(len(key))

    plaintext = de_pkcs7(decrypt_aes_cbc(key, iv, cyphertext))
    print(plaintext.decode())
예제 #3
0
def break_ecb(oracle: Callable[[bytes], bytes]) -> bytes:
    bs = blocksize(oracle)
    l = len(oracle(b""))
    string_length = len_string(oracle)

    plaintext = b""
    prefix = (l + bs - 1) * b"A"
    while len(plaintext) <= string_length:
        oracle_prefix = oracle(prefix)
        for i in range(127):
            test = prefix + plaintext + bytes([i])
            if oracle(test)[l:l + bs] == oracle_prefix[l:l + bs]:
                prefix = prefix[1:]
                plaintext += bytes([i])
                break

    return de_pkcs7(plaintext)
예제 #4
0
파일: m14.py 프로젝트: fionn/cryptopals
def break_ecb(oracle: Callable[[bytes], bytes]) -> bytes:
    bs = blocksize(oracle)
    l = len(oracle(b""))
    prefix_length = len_prefix(oracle)
    string_length = len_string(oracle) - prefix_length

    plaintext = b""
    uc = (l + bs - prefix_length - 1) * b"A"
    while len(plaintext) <= string_length:
        oracle_input = oracle(uc)
        for i in range(127):
            test = uc + plaintext + bytes([i])
            if oracle(test)[l:l + bs] == oracle_input[l:l + bs]:
                uc = uc[1:]
                plaintext += bytes([i])
                break

    return de_pkcs7(plaintext)
예제 #5
0
파일: m14.py 프로젝트: iamfionn/matasano
def break_ecb(oracle):
    bs = blocksize(oracle)
    l = len(oracle())
    string_length = len_string(oracle)
    prefix_length = len_prefix(oracle)

    plaintext = b''
    uc = (l + bs - len_prefix(oracle) - 1) * b'A'
    while len(plaintext) <= string_length:
        oracle_input = oracle(uc)
        for i in range(127):
            test = uc + plaintext + bytes([i])
            if oracle(test)[l:l + bs] == oracle_input[l:l + bs]:
                uc = uc[1:]
                plaintext += bytes([i])
                #print(chr(i), end = "", flush = True)
                break

    return de_pkcs7(plaintext)
예제 #6
0
파일: m12.py 프로젝트: iamfionn/matasano
def break_ecb(oracle):
    bs = blocksize(oracle)
    l = len(oracle())
    string_length = len_string(oracle)

    plaintext = b''
    prefix = (l + bs - 1) * b'A'
    while len(plaintext) <= string_length:
        oracle_prefix = oracle(prefix)
        for i in range(127):
            test = prefix + plaintext + bytes([i])
            if oracle(test)[l:l + bs] == oracle_prefix[l:l + bs]:
                #if i < 10:             # hack to break if non-
                #    return plaintext   # printable padding found
                prefix = prefix[1:]
                plaintext += bytes([i])
                #print(chr(i), end = "", flush = True)
                break

    return de_pkcs7(plaintext)
예제 #7
0
파일: m14.py 프로젝트: iamfionn/matasano
def decrypt(cyphertext):
    cypher = AES.new(RANDOM_KEY, AES.MODE_ECB)
    return de_pkcs7(cypher.decrypt(cyphertext))
예제 #8
0
파일: m34.py 프로젝트: fionn/cryptopals
 def receive_message(self, cyphertext: bytes) -> None:
     self.received_cyphertext = cyphertext
     iv = cyphertext[-16:]
     message = de_pkcs7(decrypt_aes_cbc(self._aes_key(), iv,
                                        cyphertext[:-16]))
     self.received_message = message
예제 #9
0
def decrypt_oracle(profile: bytes) -> dict[str, str]:
    cypher = AES.new(RANDOM_KEY, AES.MODE_ECB)
    profile = de_pkcs7(cypher.decrypt(profile))
    return parse(profile.decode())
예제 #10
0
파일: m13.py 프로젝트: iamfionn/matasano
def decrypt_oracle(profile):
    cypher = AES.new(RANDOM_KEY, AES.MODE_ECB)
    profile = de_pkcs7(cypher.decrypt(profile))
    return parse(profile.decode())
예제 #11
0
파일: m14.py 프로젝트: fionn/cryptopals
def decrypt(cyphertext: bytes) -> bytes:
    cypher = AES.new(RANDOM_KEY, AES.MODE_ECB)
    return de_pkcs7(cypher.decrypt(cyphertext))
예제 #12
0
파일: m10.py 프로젝트: iamfionn/matasano
    for block in blocks:
        block = fixed_xor(block, vector)
        block = cypher.encrypt(block)
        cyphertext += block
        vector = block

    return cyphertext

def decrypt_aes_cbc(cyphertext, key, iv):
    cypher = AES.new(key, AES.MODE_ECB)
    blocks = [cyphertext[i:i + len(key)] for i in range(0, len(cyphertext), len(key))]

    vector = iv
    plaintext = b''
    for aesblock in blocks:
        block = cypher.decrypt(aesblock)
        plaintext += fixed_xor(block, vector)
        vector = aesblock

    return plaintext

if __name__ == "__main__":
    cyphertext = b64decode(open("data/10.txt", "r").read())
    
    key = bytes("YELLOW SUBMARINE", "utf8")
    iv = bytes(len(key))

    plaintext = de_pkcs7(decrypt_aes_cbc(cyphertext, key, iv))
    print(plaintext.decode())

예제 #13
0
 def decrypt(self, cyphertext: bytes = None) -> bytes:
     cyphertext = cyphertext or self._message_buffer.pop()
     iv = cyphertext[-16:]
     message = de_pkcs7(
         decrypt_aes_cbc(self._aes_key(), iv, cyphertext[:-16]))
     return message