def enumerateBlock(oneByteLess):
	all = list()
	for i in range(256):
		all.append(oneByteLess + bytearray([i]))

	return all
	

# Merge this with stuff from @Home
bs = 16

# Initialise known
known = bytearray(bs - 1)
howManyBlocks = len(encryptionOracle(bytearray([0x00]*bs)))//bs

for k in range(howManyBlocks):
	for iBytes in range( bs):
		# Generate iBytes Less input
		iBytesLess = bytearray(bs-1-iBytes)
		forgedInput = known[-1 *(bs-1):]
		realBlock = encryptionOracle(iBytesLess)
		for enum in enumerateBlock(forgedInput):
			forgedBlock = encryptionOracle(enum)
			if forgedBlock[0:bs] == realBlock[k*bs:k*bs+bs]:
				print("Block {0} Byte {1} MUST be {2:x}".format(k, iBytes, enum[-1]))
				known.append(enum[-1]) # Append cracked Byte
				break

hexdump(known[bs-1:], title="Broke the encryption Oracle")
# 	it becomes
# key=bytearray(b"Terminator X: Bring\'the noiseTerminator X: Bring the
# noise")

#"5. Now that you probably know the KEYSIZE: break the ciphertext into blocks of KEYSIZE length."
blocks = partitionList(ciphertext, bestKeysize)
transposed = list()

#"6. Now transpose the blocks: make a block that is the first byte of every block, and a block that is the second byte of every block, and so on."
#(You basically transform the problem of repeatingKeyXOR to a single KeyXOR, which we can already break using libs.xor.breakSingleByteXOR)
for i in range(bestKeysize):
    tmp = bytearray()
    for block in blocks:
        if i < len(block):
            tmp.append(block[i])
    transposed.append(tmp)

#"7. Solve each block as if it was single-character XOR. You already have code to do this."
key = bytearray()
for i in range(len(transposed)):
    #"8. For each block, the single-byte XOR key that produces the best looking histogram
    # is the repeating-key XOR key byte for that block. Put them together and
    # you have the key."
    p, k, s = breakSingleByteXOR(transposed[i])
    key.append(k)

# Finally decrypt everything
print("Decrypt ciphertext with key={0}".format(key))
plaintext = repeatingKeyXOR(ciphertext, key)
hexdump(plaintext)
# https://cryptopals.com/sets/1/challenges/2

from libs.utils import hex2bytes, bytes2hex, hexdump
from libs.crypto.xor import fixedXOR

buffer1 = hex2bytes("1c0111001f010100061a024b53535009181c")
buffer2 = hex2bytes("686974207468652062756c6c277320657965")
result = fixedXOR(buffer1, buffer2)

hexdump(buffer1)
hexdump(buffer2)
print("== equals ==")
hexdump(result)

if result == hex2bytes("746865206b696420646f6e277420706c6179"):
    print("Challenge 2 successful!")
else:
    print("Challenge 2 failed miserably")
from libs.utils import hexdump, hex2bytes
from libs.analysis import detectECB
import base64

# Read File
file = open("Set1/Set1Challenge8.txt", "r")
i = 0
for line in file:
    ciphertext = hex2bytes(line[:-1])
    score = detectECB(ciphertext)

    if score > 0:
        hexdump(
            ciphertext,
            title="Detected ECB-Encrypted Ciphertext with score={0}".format(
                score))
    i += 1
key = bytearray("YELLOW SUBMARINE", "utf-8")
# https://cryptopals.com/sets/1/challenges/3

from libs.utils import hex2bytes, bytes2hex, hexdump
from libs.analysis import scoreEnglish
from libs.crypto.xor import breakSingleByteXOR

ciphertext = hex2bytes(
    "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
p, k, s = breakSingleByteXOR(ciphertext)

hexdump(ciphertext)
print("Broke it with with k={1}:".format(ciphertext, k))
hexdump(p)
Example #6
0
# https://cryptopals.com/sets/1/challenges/5

from libs.utils import hexdump, hex2bytes
from libs.crypto.xor import repeatingKeyXOR

plaintext = bytearray(
    "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal", "utf-8")
key = bytearray("ICE", "utf-8")

solution = hex2bytes(
    "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f")
ciphertext = repeatingKeyXOR(plaintext, key)

print("Plaintext:")
hexdump(plaintext)
print("Encrypted with k={0}".format(key))
hexdump(ciphertext)

if ciphertext == solution:
    print("Challenge 5 successful!")
else:
    print("Challenge 5 failed miserably")
# https://cryptopals.com/sets/1/challenges/4

from libs.utils import hex2bytes, bytes2hex, hexdump
from libs.analysis import scoreEnglish
from libs.crypto.xor import breakSingleByteXOR

file = open("Set1/Set1Challenge4.txt", "r")
bestScore = 0
bestKey = None
bestPlaintext = None
bestCiphertext = None
for line in file:
    ciphertext = hex2bytes(line[:-1])  # ommit \n
    plaintext, key, score = breakSingleByteXOR(ciphertext)

    if bestScore < score:
        bestScore = score
        bestKey = key
        bestPlaintext = plaintext
        bestCiphertext = ciphertext

print("Ciphertext:")
hexdump(bestCiphertext)
print("Broke it with with k={0}:".format(bestKey))
hexdump(bestPlaintext)
from libs.utils import hexdump
from libs.crypto.blockciphermodes import AES128Decrypt_ECB

import base64
# Read File
with open("Set1/Set1Challenge7.txt", "rb") as file:
    ciphertext = base64.b64decode(file.read())

key = bytearray("YELLOW SUBMARINE", "utf-8")
plaintext = AES128Decrypt_ECB(ciphertext, key)

hexdump(plaintext, title="Decrypted using AES128-ECB and k={0}".format(key))
Example #9
0
from libs.crypto.blockciphermodes import AES128Decrypt_CBC
from libs.utils import hexdump
import base64

with open("Set2/Set2Challenge10.txt", "rb") as file:
    ciphertext = base64.b64decode(file.read())

key = bytearray("YELLOW SUBMARINE", "utf-8")
iv = bytearray([0x0] * 16)

plaintext = AES128Decrypt_CBC(ciphertext, key, iv)
hexdump(plaintext,
        title="Decrypted using AES128-CBC, k={0} and iv={1}:".format(key, iv))