import gmpy2 from Crypto.Util.number import * import hashlib p = getPrime(1024) q = getPrime(1024) p1 = getPrime(1024) q1 = getPrime(1024) n = p * q n1 = p1 * q1 e = 0x10001 phi = (p - 1) * (q - 1) phi1 = (p1 - 1) * (q1 - 1) def chatEncrypt(pepole, message): if pepole == 'Tom': sender = hashlib.sha256(str(pepole).encode()).hexdigest() sign = hashlib.sha256(str(message).encode()).hexdigest() encryptMessage = hex(pow(bytes_to_long(message), e, n)) return sender, encryptMessage, sign elif pepole == 'Jerry': sender = hashlib.sha256(str(pepole).encode()).hexdigest() sign = hashlib.sha256(str(message).encode()).hexdigest() encryptMessage = hex(pow(bytes_to_long(message), e + 2, n)) return sender, encryptMessage, sign chatLog = open("Chatting.log", "rb") encryptedChatLog = open("ChatEncrypted.log", "w+") log = [] for line in chatLog: log.append(line) chatLog.close() encryptedChatLog.write(hex(n)+'\n') encryptedChatLog.write(hex(n1)+'\n') encryptedChatLog.write(hex(phi1)+'\n') for i in range(int(len(log)/2)): if log[i*2] == b'Tom\r\n': encryptedChatLog.write(str(chatEncrypt('Tom', log[i*2+1]))+'\n') print(chatEncrypt('Tom', log[i*2+1])) elif log[i*2] == b'Jerry\r\n': encryptedChatLog.write(str(chatEncrypt('Jerry', log[i*2+1]))+'\n') print(chatEncrypt('Jerry', log[i*2+1])) encryptedChatLog.close()解密脚本
时间: 2023-05-29 13:07:45 浏览: 161
import gmpy2 from Crypto.Util.number import * import hashlib def chatDecrypt(pepole, encryptedMessage, signature, n, phi, e): sender = hashlib.sha256(str(pepole).encode()).hexdigest() if sender != pepole: print("Invalid sender!") return None sign = hashlib.sha256(str(encryptedMessage).encode()).hexdigest() if sign != signature: print("Invalid signature!") return None message = long_to_bytes(pow(int(encryptedMessage, 16), gmpy2.invert(e, phi), n)) return message encryptedChatLog = open("ChatEncrypted.log", "rb") decryptedChatLog = open("ChatDecrypted.log", "wb") n = int(encryptedChatLog.readline().strip(), 16) n1 = int(encryptedChatLog.readline().strip(), 16) phi1 = int(encryptedChatLog.readline().strip(), 16) e = 0x10001 for line in encryptedChatLog: pepole, encryptedMessage, signature = eval(line.strip()) if n == 0: message = chatDecrypt(pepole, encryptedMessage, signature, n1, phi1, e 2) else: message = chatDecrypt(pepole, encryptedMessage, signature, n, (p-1)*(q-1), e) if message != None: decryptedChatLog.write(pepole.encode() + b': ' + message + b'\n') encryptedChatLog.close() decryptedChatLog.close()
阅读全文