xor加密

xor加密

第一步交互一个密码的值并进行MD5加密

由于python3没有MD5函数,所以从hashlib中引用

raw=input("请输入密码:") #交互输入密码值
import hashlib #引用hashlib数据库
str =password
h1 =  hashlib.md5()
h1.update(str.encode(encoding='utf-8'))   #忽略错误
print('密码的MD5加密码为:' + h1.hexdigest())

生成一个key

from secrets import token_bytes   #secrets 库是 Python 3.6 引入的伪随机数模块,适合生成随机密钥。token_bytes 函数接受一个 int 参数,用于指定随机字节串的长度。int.from_bytes 把字节串转换为 int,也就是我们需要的二进制数。
from typing import Tuple
def random_key(length:int) -> int:
    key:bytes = token_bytes(nbytes=length)
    key_int:int = int.from_bytes(key, 'big')
    return key_int

进行异或加密运算

def encrypt(raw:str) -> Tuple[int, int]:      #encrypt 函数接受一个 str 对象,返回元组 (int, int)。通过 encode 方法,我们将字符串编码成字节串。int.from_bytes 函数将字节串转换为 int 对象。最后对二进制对象和随机密钥进行异或操作,就得到了加密文本。
    raw_bytes:bytes = raw.encode()
    raw_int:int = int.from_bytes(raw_bytes, 'big')
    key_int:int = random_key(len(raw_bytes))
    return raw_int ^ key_int, key_int

代码运行截图

参考链接

https://blog.csdn.net/weixin_44799217/article/details/112486097
http://www.ruanyifeng.com/blog/2017/05/xor.html
https://blog.csdn.net/weixin_39691968/article/details/111016389
https://blog.csdn.net/weixin_39633807/article/details/112275849

posted @ 2021-10-29 15:03  20211308wjc  阅读(225)  评论(0编辑  收藏  举报