You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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')