def toHex(n): s=hex(n) s=s[2:] if len(s)==1: s="0"+s return s这段代码的功能
时间: 2024-05-18 09:16:01 浏览: 123
这段代码定义了一个名为 `toHex` 的函数,其功能是将一个整数转换为对应的十六进制字符串。具体来说,函数首先将整数 `n` 转换为十六进制字符串,然后去掉字符串开头的 '0x' 字符串,将转换后的字符串保存到变量 `s` 中。接着,如果 `s` 的长度为 1,则在字符串开头添加一个 '0' 字符,最后返回处理后的字符串 `s`。需要注意的是,由于十六进制字符串中每个字符只能表示 4 个二进制位,因此转换后的十六进制字符串长度可能为 1、2 或 4。
相关问题
import uuid import os import winreg from binascii import b2a_hex from Crypto.Cipher import AES def reg_confirm(key_path): try: key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_path, 0, winreg.KEY_READ) value = winreg.QueryValueEx(key, 'UUID')[0] print("注册表值为:", value) winreg.CloseKey(key) return value except: print("注册表不存在,正在创建...") # 创建注册表 key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, key_path) # 写入键值 value = str(uuid.uuid1()).replace('-', '')[:16] winreg.SetValueEx(key, 'UUID', 0, winreg.REG_SZ, value) # 关闭注册表 winreg.CloseKey(key) print("已创建并写入值:", value) return key def add_to_hex16(text: bytes): if len(text) % 16: add = 16 - (len(text) % 16) else: add = 0 text = text + (b'\0' * add) return text def encrypt(text: bytes, key): mode = AES.MODE_CBC text = add_to_hex16(text) cryptos = AES.new(key, mode, key) cipher_text = cryptos.encrypt(text) return b2a_hex(cipher_text) if __name__ == '__main__': path = input('请输入pth路径') hkey = str(reg_confirm("Software\\Jiujikeruan")).encode('utf8') with open(path, 'rb') as f1: encrypted = encrypt(f1.read(), hkey) with open(path + '.jiujikeruan', 'wb') as f2: f2.write(encrypted) 将这些代码中的path=input改为用tkgui让用户选择路径
你可以使用tkinter模块来实现GUI界面,代码如下所示:
```python
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
if not file_path:
print('未选择文件!')
else:
print('已选择文件:', file_path)
```
将这段代码替换掉原来的`path=input('请输入pth路径')`即可。
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()解密脚本
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()
阅读全文