NTRU_IoT/infidel-code/py_wrappers/aes.py
2022-02-01 23:45:47 +07:00

53 lines
1.6 KiB
Python

from Crypto.Cipher import AES
import os
import binascii
import time
import hashlib
def aes(key,PLAIN):
# print("=====================================AES128")
# print("Length Key AES: ",len(key))
# key = '12345678901234567890123456789012'
IV = 16 * '\x00'
cipher = AES.new(key, AES.MODE_CBC, IV=IV)
msg =cipher.encrypt(PLAIN)
# haha="Let the Silence be with us all, through all the hard time we embrace"
# haha=haha.zfill(96)
# print(len(haha))# #
decipher = AES.new(key, AES.MODE_CBC,IV=IV)
# print(decipher.decrypt(msg))
hal=(binascii.hexlify(msg).decode('utf8'))
print("Length AES Cipher: ",len(hal))
return msg
def rsa(key2,PLAIN):
# print("=====================================RSA128"
# from Crypto.PublicKey import RSA
# key2=RSA.generate(3072)
start=time.time()
publickey=key2.publickey()
# enc=publickey.encrypt("Attack the Fortress at midnight ",0)
print(PLAIN)
enc=publickey.encrypt(PLAIN.encode('utf-8'),0)
o=(binascii.hexlify(enc[0])).decode('utf8')
print("Length RSA Cipher: ",len(o))
end=time.time()-start
return o, end
# b=binascii.unhexlify(o)
# if b==enc[0]:
# print("TRUE")
def ntru(key,PLAIN):
# print("=====================================NTRU")
with open("fortress","rb") as n:
d=n.read()
nn=(binascii.hexlify(d).decode('utf8'))
print("Length: ",len(nn))
# with open("aes128.txt","w") as aes:
# aes.write(msg.encode("hex"))
# with open("rsa128.txt","w") as rsa:
# rsa.write(o)
# with open("ntru128.txt","w") as ntru:
# ntru.write(nn)