def concatAndEncrypt(plaintext): prepend = b"comment1=cooking%20MCs;userdata=" append = b";comment2=%20like%20a%20pound%20of%20bacon" plaintext = cleanPlaintext(plaintext) longPlaintext = prepend + plaintext + append nextMult = next16Multiple(len(longPlaintext)) fullPlaintext = PKCS7pad(longPlaintext, nextMult) ciphertext = CBCencrypt(fullPlaintext, KEY, IV) return ciphertext
def ECBOracle(yourString): #yourString should be a BYTEstring unknownString = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK" decodedString = base64.b64decode(unknownString) totalString = yourString + decodedString targetLen = next16Multiple(len(totalString)) paddedPlaintext = PKCS7pad(totalString, targetLen) ciphertext = AESencrypt(paddedPlaintext, KEY) return ciphertext
def getListOfBlocks(plaintext): #assuming we want 128 bit blocks -- 16 byte blocks result = [] for i in range(len(plaintext)): if i % 16 == 0: result.append(bytes([plaintext[i]])) else: result[-1] += bytes([plaintext[i]]) if len(result[-1]) != 16: result[-1] = PKCS7pad(result[-1], 16) return result
def encryptionOracle(plaintext): key = random16Bytes() padLens = randrange(5, 11) plaintext = padPlaintext(plaintext, padLens, padLens) ECBorCBC = randrange(0, 2) if ECBorCBC == 1: print("using ECB") length = next16Multiple(len(plaintext)) plaintext = PKCS7pad(plaintext, length) ciphertext = AESencrypt(plaintext, key) else: print("using CBC") IV = random16Bytes() ciphertext = CBCencrypt(plaintext, key, IV) return ciphertext
def encryptAString(): possibilities = [b"MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=", b"MDAwMDAxV2l0aCB0aGUgYmFzcyBraWNrZWQgaW4gYW5kIHRoZSBWZWdhJ3MgYXJlIHB1bXBpbic=", b"MDAwMDAyUXVpY2sgdG8gdGhlIHBvaW50LCB0byB0aGUgcG9pbnQsIG5vIGZha2luZw==", b"MDAwMDAzQ29va2luZyBNQydzIGxpa2UgYSBwb3VuZCBvZiBiYWNvbg==", b"MDAwMDA0QnVybmluZyAnZW0sIGlmIHlvdSBhaW4ndCBxdWljayBhbmQgbmltYmxl", b"MDAwMDA1SSBnbyBjcmF6eSB3aGVuIEkgaGVhciBhIGN5bWJhbA==", b"MDAwMDA2QW5kIGEgaGlnaCBoYXQgd2l0aCBhIHNvdXBlZCB1cCB0ZW1wbw==", b"MDAwMDA3SSdtIG9uIGEgcm9sbCwgaXQncyB0aW1lIHRvIGdvIHNvbG8=", b"MDAwMDA4b2xsaW4nIGluIG15IGZpdmUgcG9pbnQgb2g=", b"MDAwMDA5aXRoIG15IHJhZy10b3AgZG93biBzbyBteSBoYWlyIGNhbiBibG93"] #index = randrange(0, len(possibilities)) #MAKE IT RANDOM HERE TOO index = 2 mult = next16Multiple(len(possibilities[index])) padded = PKCS7pad(possibilities[index], mult) ciphertext = CBCencrypt(padded, KEY, IV) return ciphertext
def decryptUserProfile(ciphertext): ciphertext = ciphertext ################# targetLen = next16Multiple(len(ciphertext)) paddedCiphertext = PKCS7pad(ciphertext, targetLen) plaintext = AESdecrypt(paddedCiphertext, KEY) return plaintext
def encryptUserProfile(string): byteString = string ################### targetLen = next16Multiple(len(byteString)) paddedPlaintext = PKCS7pad(byteString, targetLen) ciphertext = AESencrypt(paddedPlaintext, KEY) return ciphertext