|
|
|
|
from binascii import b2a_hex, a2b_hex
|
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
from Crypto import Random
|
|
|
|
|
import time # 引入time模块
|
|
|
|
|
iv = Random.new().read(AES.block_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class aestest():
|
|
|
|
|
# ***********Begin**************
|
|
|
|
|
def __init__(self, key):
|
|
|
|
|
self.key = key.encode('utf-8')
|
|
|
|
|
|
|
|
|
|
def encrypt(self, text):
|
|
|
|
|
text = text.encode('utf-8')
|
|
|
|
|
aes = AES.new(self.key, AES.MODE_CFB,iv) # 这里是直接调用函数AES.new,参数是密钥和ECB模式设置。初始化加密器
|
|
|
|
|
|
|
|
|
|
ctext = aes.encrypt(text) # 加密
|
|
|
|
|
|
|
|
|
|
return ctext
|
|
|
|
|
|
|
|
|
|
def decrypt(self, text):
|
|
|
|
|
text = text.encode('utf-8')
|
|
|
|
|
aes = AES.new(self.key, AES.MODE_CFB,iv)
|
|
|
|
|
plain = aes.decrypt(a2b_hex(text))
|
|
|
|
|
return plain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ************End***************
|
|
|
|
|
|
|
|
|
|
def Evidence(text, key):
|
|
|
|
|
|
|
|
|
|
aes = aestest(key)
|
|
|
|
|
text = text.encode('utf-8')
|
|
|
|
|
T1 = time.time()
|
|
|
|
|
for i in range(100000):
|
|
|
|
|
enc = aes.encrypt(text)
|
|
|
|
|
T2 = time.time()
|
|
|
|
|
# print(T2-T1)
|
|
|
|
|
T_e = T2-T1
|
|
|
|
|
enc = b2a_hex(enc) # 字符串转换
|
|
|
|
|
T3 = time.time()
|
|
|
|
|
for i in range(100000):
|
|
|
|
|
detext = aes.decrypt(enc)
|
|
|
|
|
T4 = time.time()
|
|
|
|
|
# print(T4-T3)
|
|
|
|
|
T_d = T4 - T3
|
|
|
|
|
return T_e,T_d
|
|
|
|
|
|
|
|
|
|
# if __name__ == "__main__":
|
|
|
|
|
# # str1, str2 = input().split()
|
|
|
|
|
# # Evidence(str1, str2)
|
|
|
|
|
# test = {16:'1234567812345678',24:'123456781234567812345678',32:'12345678123456781234567812345678'}
|
|
|
|
|
# for b,str2 in test.items():
|
|
|
|
|
# T_e = Evidence('xyqshiyigedasgya', str2)[0]
|
|
|
|
|
# T_d = Evidence('xyqshiyigedasgya', str2)[1]
|
|
|
|
|
# print(str(b)+'位的加密时间是'+str(T_e)+" 它加密的数据是"+str2)
|
|
|
|
|
# print('它的加密效率是'+str(12.8/T_e)+' Mbps')
|
|
|
|
|
# print(str(b) + '位的解密时间是' + str(T_d))
|
|
|
|
|
# print('它的解密效率是' + str(12.8 / T_d) + ' Mbps')
|