def RandEncrypt(Key,IV):
    chosen = random.randint(1,10)
    cline = 1
    Message = ""
    file = open("RandStuff","r")
    for line in file:
        if cline == chosen:
            Message = line
        cline += 1
    file.close()
    Binary = binascii.a2b_base64(Message)
    print Binary[16:32]
    print Message[16:32]
    Blocks = pad(Binary,16)
    ready = ""
    for Block in Blocks:
        ready += Block
    cipher = AES.AESCipher(Key, AES.MODE_CBC, IV)
    return cipher.encrypt(ready)
Ejemplo n.º 2
0
def RandEncrypt(Key, IV):
    chosen = random.randint(1, 10)
    cline = 1
    Message = ""
    file = open("RandStuff", "r")
    for line in file:
        if cline == chosen:
            Message = line
        cline += 1
    file.close()
    Binary = binascii.a2b_base64(Message)
    print Binary[16:32]
    print Message[16:32]
    Blocks = pad(Binary, 16)
    ready = ""
    for Block in Blocks:
        ready += Block
    cipher = AES.AESCipher(Key, AES.MODE_CBC, IV)
    return cipher.encrypt(ready)
s_B = dh.getShared(p_B, A_B, b)

hash_A = sha1("%d" % s_A)
hash_B = sha1("%d" % s_B)

key_A = "".join([chr(int(hash_A[i] + hash_A[i+1],16)) for i in range(0,len(hash_A),2)])
key_B = "".join([chr(int(hash_B[i] + hash_B[i+1],16)) for i in range(0,len(hash_B),2)])
key_A = key_A[:16]
key_B = key_B[:16]

iv_A = os.urandom(16)
iv_B = os.urandom(16)

msg_A = "Hello World"
cipher = AES.AESCipher(key_A, AES.MODE_CBC, iv_A)
send_A = cipher.encrypt("".join(pad(msg_A,16))) + iv_A

print "A -> B"
print "Send AES-CBC(SHA1(s)[0:16],iv=random(16),msg)"

received_B = send_A
iv_A_B = received_B[len(received_B) - 16:]
encrypted_A_B = received_B[:len(received_B) - 16]
cipher = AES.AESCipher(key_B, AES.MODE_CBC, iv_A_B)
msg_B = unPad(cipher.decrypt(encrypted_A_B),16)

cipher = AES.AESCipher(key_B, AES.MODE_CBC, iv_B)
send_B = cipher.encrypt("".join(pad(msg_B,16))) + iv_B

print "B -> A"
print "Send AES-CBC(SHA1(s)[0:16],iv=random(16),A's msg)"