def encryptionOracle(ptByteArray): global GLOBAL_KEY global UNKNOWN_STRING unknownString = base64.b64decode(UNKNOWN_STRING) if GLOBAL_KEY == None: GLOBAL_KEY = getRandBytes(16) ptInput = PKCS7.PKCS7(ptByteArray + unknownString, 16) ct = AES_ECB.AESencrypt(ptInput, GLOBAL_KEY) return ct
def AES_CBCencrypt(ptByteArray, key, IV): prevCt = IV ct = [] for i in range(len(ptByteArray) // 16): ptInput = xorEnc.fixedXor(ptByteArray[i * 16:(i + 1) * 16], prevCt) newCtBlock = AES_ECB.AESencrypt(ptInput, key) for j in range(len(newCtBlock)): ct.append(newCtBlock[j]) prevCt = newCtBlock ct = bytes(ct) return ct
def profile_for(email): hasMetaChars = checkForMetachars(email) if hasMetaChars: print("Invalid Id") return None else: global GLOBAL_KEY if GLOBAL_KEY == None: GLOBAL_KEY = getRandBytes(16) uid = random.randrange(100, 1001) uid = str(uid) encodedUser = "******" + email + "&uid=" + uid + "&role=user" encodedUser = bytes(encodedUser.encode('utf-8')) ct = AES_ECB.AESencrypt(PKCS7.PKCS7(encodedUser, 16), GLOBAL_KEY) return ct
def blackBox(ptByteArray): padBefore = random.randrange(5, 11) padAfter = random.randrange(5, 11) randPaddedPt = randPadPlaintext(ptByteArray, padBefore, padAfter) key = getRandBytes(16) encMode = random.randrange(0, 2) if encMode == 0: ptInput = PKCS7.PKCS7(randPaddedPt, 16) ct = AES_ECB.AESencrypt(ptInput, key) #print('Original: ECB') return ct else: ptInput = PKCS7.PKCS7(randPaddedPt, 16) IV = getRandBytes(16) ct = AES_CBC.AES_CBCencrypt(ptInput, key, IV) #print('Original: CBC') return ct
def AES_CTR(ipBytes, key, nonce): blockSize = 16 nonce = get64BitLittleEndianBytes(nonce) noOfBlocks = math.ceil(len(ipBytes) / blockSize) opBytes = bytes(''.encode('utf-8')) for i in range(noOfBlocks): ctr = get64BitLittleEndianBytes(i) ipToAes = nonce + ctr encryptedCtr = AES_ECB.AESencrypt(ipToAes, key) if i == noOfBlocks - 1: ipBytesForXor = ipBytes[i * blockSize:] op = ENC.fixedXor(encryptedCtr[:len(ipBytesForXor)], ipBytesForXor) opBytes += op else: op = ENC.fixedXor(encryptedCtr, ipBytes[i * blockSize:(i + 1) * blockSize]) opBytes += op return opBytes