parent
56903eebfd
commit
62273d7208
@ -0,0 +1,65 @@
|
|||||||
|
from binascii import b2a_hex, a2b_hex
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
from binascii import b2a_hex, a2b_hex
|
||||||
|
from Crypto import Random
|
||||||
|
from Crypto.Util import Counter
|
||||||
|
import time
|
||||||
|
random_generator = Random.new()
|
||||||
|
IV = random_generator.read(8)
|
||||||
|
|
||||||
|
|
||||||
|
class aestest():
|
||||||
|
# ***********Begin**************
|
||||||
|
def __init__(self, key):
|
||||||
|
self.key = key.encode('utf-8')
|
||||||
|
|
||||||
|
def encrypt(self, text):
|
||||||
|
text = text.encode('utf-8')
|
||||||
|
ctr_e = Counter.new(64, prefix=IV)
|
||||||
|
aes = AES.new(self.key, AES.MODE_CTR, counter=ctr_e)
|
||||||
|
|
||||||
|
ctext = aes.encrypt(text) # 加密
|
||||||
|
|
||||||
|
return ctext
|
||||||
|
|
||||||
|
def decrypt(self, text):
|
||||||
|
text = text.encode('utf-8')
|
||||||
|
ctr_d = Counter.new(64, prefix=IV)
|
||||||
|
aes = AES.new(self.key, AES.MODE_CTR, counter=ctr_d)
|
||||||
|
plain = aes.decrypt(a2b_hex(text))
|
||||||
|
return plain
|
||||||
|
|
||||||
|
|
||||||
|
# ************End***************
|
||||||
|
|
||||||
|
def Evidence(text, key):
|
||||||
|
# 要求key长度为16
|
||||||
|
|
||||||
|
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')
|
Loading…
Reference in new issue