Пример #1
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
Пример #2
0
def encrypt(ptBytes, seed=None):
    if seed == None:
        seed = random.randrange(0, pow(2, 20))
    #print(seed%pow(2,16))
    noOfRandBytes = random.randrange(10, 20)
    ipBytes = helpers.getRandBytes(noOfRandBytes) + ptBytes
    ct = MT19937cipher(ipBytes, seed)
    return ct
Пример #3
0
def generateHMAC(ipBytes):
    global GLOBAL_KEY

    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(16)
    hashFunc = hashlib.new('sha1')
    calcMac = HMAC.hmac(ipBytes, GLOBAL_KEY, hashFunc, hashFunc.block_size, hashFunc.digest_size)
    
    return calcMac
Пример #4
0
def generateCts(ipFile):
    key = helper.getRandBytes(16)
    nonce = 0
    cts = []
    for line in ipFile:
        pt = line[:len(line) - 1]
        pt = base64.b64decode(pt)
        ct = AES_CTR.AES_CTR(pt, key, nonce)
        cts.append(ct)
    return cts
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')
Пример #6
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
def generateMAC(ipBytes):
    global KEY_LEN
    global GLOBAL_KEY

    if KEY_LEN == None:
        KEY_LEN = random.randrange(7, 20)
        #print(KEY_LEN)
    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(KEY_LEN)

    mac = MD4keyedMAC.MD4MAC(ipBytes, GLOBAL_KEY)
    return mac
def generateMac(ipBytes):
    global GLOBAL_KEY
    global KEY_SIZE

    if KEY_SIZE == None:
        KEY_SIZE = random.randrange(7,20)
        #print(KEY_SIZE)
    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(KEY_SIZE)

    mac = SHA1keyedMAC.SHA1MAC(ipBytes, GLOBAL_KEY)
    mac2 = mysha1.sha1(GLOBAL_KEY+ipBytes)
    return mac
def encryptUnknownPt():
    global GLOBAL_NONCE
    global GLOBAL_KEY 
    ipFile = open("input.txt", 'r')
    contents = ipFile.read().replace('\n', '')
    pt = base64.b64decode(contents)
    ipFile.close()

    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(16)
    if GLOBAL_NONCE == None:
        GLOBAL_NONCE = random.randrange(0, pow(2,64))

    ct = AES_CTR.AES_CTR(pt, GLOBAL_KEY, GLOBAL_NONCE)
    return ct, pt
Пример #10
0
def encrypt(ipString):
    global GLOBAL_KEY
    global GLOBAL_NONCE
    stringToPrepend = "comment1=cooking%20MCs;userdata="
    stringToAppend = ";comment2=%20like%20a%20pound%20of%20bacon"

    if GLOBAL_KEY == None:
        GLOBAL_KEY = helpers.getRandBytes(16)
    if GLOBAL_NONCE == None:
        GLOBAL_NONCE = random.randrange(0, pow(2, 64))

    modifiedIp = quoteMetachars(ipString)
    modifiedIp = stringToPrepend + modifiedIp + stringToAppend
    ipBytes = bytes(modifiedIp.encode('latin1'))

    ct = AES_CTR.AES_CTR(ipBytes, GLOBAL_KEY, GLOBAL_NONCE)
    return 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')