コード例 #1
0
ファイル: aes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #2
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #3
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #4
0
ファイル: aes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #5
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #6
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #7
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #8
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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
コード例 #9
0
ファイル: modes.py プロジェクト: MingyaoChen/aes-encryption
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