Exemplo n.º 1
0
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 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 keyExchangeProtocol():
    p_A, g_A = DiffieHellman.p, DiffieHellman.g

    keyPairA = DiffieHellman.generateKeyPair(p_A, g_A)
    A = keyPairA[1]
    print("A to B: p, g, A")
    keyPairB = DiffieHellman.generateKeyPair(p_A, g_A)
    B = keyPairB[1]
    print("B to A: B")

    secretKeyA = DiffieHellman.DH(B, keyPairA, p_A)
    secretKeyB = DiffieHellman.DH(A, keyPairB, p_A)

    messageA = 'This is my message suckerr.\nYour\'s Sincerely\nA'
    messageA = bytes(messageA.encode('latin1'))
    secretKeyA_bytes = bytes.fromhex(hex(secretKeyA)[2:])
    AESkeyA = hashlib.sha1(secretKeyA_bytes).digest()[:16]
    IV_A = helpers.getRandBytes(16)
    ct = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(messageA, 16), AESkeyA, IV_A)

    secretKeyB_bytes = bytes.fromhex(hex(secretKeyB)[2:])
    AESkeyB = hashlib.sha1(secretKeyB_bytes).digest()[:16]
    pt = AES_CBC.AES_CBCdecrypt(ct, AESkeyB, IV_A)
    if PKCS7.PKCS7validate(pt, 16):
        pt = PKCS7.PKCS7unpad(pt)
    else:
        raise Exception('Something is wrong')

    IV_B = helpers.getRandBytes(16)
    ct_BtoA = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(pt, 16), AESkeyB, IV_B)

    messageA2 = AES_CBC.AES_CBCdecrypt(ct_BtoA, AESkeyA, IV_B)
    if PKCS7.PKCS7validate(messageA2, 16):
        messageA2 = PKCS7.PKCS7unpad(messageA2)
    else:
        raise Exception('Something is wrong')

    if messageA == messageA2:
        print('Connection established')
    else:
        print('No connection')
Exemplo n.º 4
0
def encrypt(ipBytes):
    global GLOBAL_KEY
    global GLOBAL_IV

    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(16)
    if GLOBAL_IV == None:
        GLOBAL_IV = GLOBAL_KEY

    ct = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(ipBytes, 16), GLOBAL_KEY,
                                GLOBAL_IV)
    return ct
Exemplo n.º 5
0
def encrypt(userInputString):
    global GLOBAL_KEY
    global IV
    if GLOBAL_KEY == None:
        GLOBAL_KEY = getRandBytes(16)
    if IV == None:
        IV = getRandBytes(16)

    userInputString = quoteMetachars(userInputString)
    stringToPrepend = "comment1=cooking%20MCs;userdata="
    stringToAppend = ";comment2=%20like%20a%20pound%20of%20bacon"
    finalString = stringToPrepend + userInputString + stringToAppend

    ipBytes = bytes(finalString.encode('utf-8'))
    ct = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(ipBytes, 16), GLOBAL_KEY, IV)
    return ct
Exemplo n.º 6
0
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
Exemplo n.º 7
0
def encryptRandString():
    global GLOBAL_KEY
    if GLOBAL_KEY == None:
        GLOBAL_KEY = helper.getRandBytes(16)
    IV = helper.getRandBytes(16)

    ipFile = open("input.txt", 'r')
    choice = random.randrange(1, 11)
    randString = ''
    for i in range(choice):
        randString = ipFile.readline()
    randString = randString[:len(randString) - 1]
    randString = base64.b64decode(randString)

    ptInput = PKCS7.PKCS7(randString, 16)
    ct = AES_CBC.AES_CBCencrypt(ptInput, GLOBAL_KEY, IV)
    return IV, ct
def MITM():
    p_A, g_A = DiffieHellman.p, DiffieHellman.g

    keyPairA = DiffieHellman.generateKeyPair(p_A, g_A)
    A = keyPairA[1]
    print("A to B: p, g, A")
    print("Intercepted by M")
    keyPairB = DiffieHellman.generateKeyPair(p_A, g_A)
    B = keyPairB[1]
    print("M to B: p, g, p")
    print("B to M: B")
    print("M to A: p")

    secretKeyA = DiffieHellman.DH(p_A, keyPairA, p_A)
    secretKeyB = DiffieHellman.DH(p_A, keyPairB, p_A)

    messageA = 'This is my message suckerr.\nYour\'s Sincerely\nA'
    messageA = bytes(messageA.encode('latin1'))
    secretKeyA_bytes = bytes.fromhex(
        hex(secretKeyA)[2:] if len(hex(secretKeyA)[2:]) % 2 == 0 else '0' +
        hex(secretKeyA)[2:])
    AESkeyA = hashlib.sha1(secretKeyA_bytes).digest()[:16]
    IV_A = helpers.getRandBytes(16)
    ct = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(messageA, 16), AESkeyA, IV_A)

    secretKeyB_bytes = bytes.fromhex(
        hex(secretKeyB)[2:] if len(hex(secretKeyB)[2:]) % 2 == 0 else '0' +
        hex(secretKeyB)[2:])
    AESkeyB = hashlib.sha1(secretKeyB_bytes).digest()[:16]
    pt = AES_CBC.AES_CBCdecrypt(ct, AESkeyB, IV_A)
    if PKCS7.PKCS7validate(pt, 16):
        pt = PKCS7.PKCS7unpad(pt)
    else:
        raise Exception('Something is wrong')

    IV_B = helpers.getRandBytes(16)
    ct_BtoA = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(pt, 16), AESkeyB, IV_B)

    messageA2 = AES_CBC.AES_CBCdecrypt(ct_BtoA, AESkeyA, IV_B)
    if PKCS7.PKCS7validate(messageA2, 16):
        messageA2 = PKCS7.PKCS7unpad(messageA2)
    else:
        raise Exception('Something is wrong')

    if messageA == messageA2:
        print('Connection established')
    else:
        print('No connection')

    AESkeyM = hashlib.sha1(bytes.fromhex('00')).digest()[:16]
    messageA = 'Message 1'
    messageA = bytes(messageA.encode('latin1'))
    IV_A = helpers.getRandBytes(16)
    ct = AES_CBC.AES_CBCencrypt(PKCS7.PKCS7(messageA, 16), AESkeyA, IV_A)
    print('A:', messageA)
    messageM = AES_CBC.AES_CBCdecrypt(ct, AESkeyM, IV_A)
    if PKCS7.PKCS7validate(messageM, 16):
        messageM = PKCS7.PKCS7unpad(messageM)
        print('M:', messageM)
    else:
        raise Exception('Something is wrong')
    messageB = AES_CBC.AES_CBCdecrypt(ct, AESkeyB, IV_A)
    if PKCS7.PKCS7validate(messageB, 16):
        messageB = PKCS7.PKCS7unpad(messageB)
        print('B:', messageB)
    else:
        raise Exception('Something is wrong')