Exemplo n.º 1
0
def encryption(key, plaintext):
    keylist=[]
    keylist.append(key)
    for i in range(1,6):
        key = newKey(keylist[i-1])
        keylist.append(key)

    state = tl.b2h(tl.xor(keylist[0],plaintext))
    for i in range(1,5):
        state = substituted(state)
        state = permutation(tl.h2b(state))
        state = tl.b2h(tl.xor(keylist[i],state))

    state = substituted(state)
    state = tl.b2h(tl.xor(keylist[5],tl.h2b(state)))
    return state
Exemplo n.º 2
0
def decryptionCTR(key,ciphertext,original_Counter,padding_size):
    count = len(ciphertext)/16
    plain=''
    encryCounter_List = getCounter(tl.b2h(original_Counter),count,key)
    for i in xrange(0,count,1):
        plain+= str(tl.b2h(tl.xor(tl.h2b(encryCounter_List[i]),tl.h2b(ciphertext[i*16:(i*16)+16]))))
    plain = tl.unPadding(plain,padding_size) 
    return plain
Exemplo n.º 3
0
def encryptionCTR(key,plaintext,original_Counter):
    count = len(plaintext)/64
    cipher = ''
    hexPlain = tl.b2h(plaintext)
    encryCounter_List = getCounter(tl.b2h(original_Counter),count,key)
    for i in xrange(0,count,1):
        cipher+= str(tl.b2h(tl.xor(tl.h2b(encryCounter_List[i]),tl.h2b(hexPlain[i*16:(i*16)+16]))))
    return cipher
Exemplo n.º 4
0
def decryption(key,ciphertext):
    keylist=[]
    keylist.append(key)

    for i in range(1,6):
        key = newKey(keylist[i-1])
        keylist.append(key)
    # Round 1 XOR KEY5
    state = tl.b2h(tl.xor(keylist[5],ciphertext))
    state = inverse_substituted(state)
    # Round 2 to 5
    for i in xrange(4,0,-1):
        state = tl.b2h(tl.xor(keylist[i],tl.h2b(state)))
        state = inverse_permutation(tl.h2b(state))
        state = inverse_substituted(tl.b2h(state))
    # Round 6 XOR KEY0
    state = tl.b2h(tl.xor(keylist[0],tl.h2b(state)))
    return state
Exemplo n.º 5
0
def encryptionCFB(key,plaintext,iv):
	count = len(plaintext)/64
	cipher = ''

	for i in xrange(0,count,1):
		key_plain = tl.h2b(aes.encryption(key,iv))
		c = tl.b2h(tl.xor(key_plain,plaintext[i*64:(i*64)+64]))
		cipher += c
		iv = tl.h2b(c)
	return cipher
Exemplo n.º 6
0
def encryptionOFB(iv, key, plaintext):
	biPlain = plaintext
	count = len(plaintext)/64
	cipher = []
	for i in xrange(0,count,1):
		iv = tl.h2b(aes.encryption(key, iv))
		biP = biPlain[i*64:(i*64)+64]
		cipher += tl.xor(iv, biP)
	cipherStr = tl.b2h(cipher)
	return cipherStr
Exemplo n.º 7
0
def encryptionCBC(key,plaintext,iv):
	count = len(plaintext)/64
	cipher = ''
	for i in xrange(0,count,1):
		plain = tl.xor(plaintext[i*64:(i*64)+64],iv)

		c = aes.encryption(key,plain)
		cipher += c
		iv = tl.h2b(c)
	return cipher
Exemplo n.º 8
0
def decryptionOFB(iv, key, ciphertext,padding_size):
	biCipher = ciphertext
	count = len(biCipher)/64
	plain = []
	for i in xrange(0,count,1):
		iv = tl.h2b(aes.encryption(key, iv))
		biC = ciphertext[i*64:(i*64)+64]
		plain += tl.xor(iv, biC)
	plainStr = tl.b2h(plain)
	plainStr = tl.unPadding(plainStr,padding_size)
	return plainStr
Exemplo n.º 9
0
def decryptionCFB(key,ciphertext,iv):
	hexCipher = tl.h2b(ciphertext[0])
	count = len(tl.h2b(ciphertext[0]))/64
	plain = ''
	for i in xrange(0,count,1):

		key_plain = tl.h2b(aes.encryption(key,iv))

		p = tl.b2h(tl.xor(key_plain,hexCipher[i*64:(i*64)+64]))
		iv = hexCipher[i*64:(i*64)+64]
		plain += p
	plain = tl.unPadding(plain, ciphertext[1])
	return plain