一、什么是编码
想要实现加密就必须要先了解什么是编码。
编码是信息从另一种形式或格式转换为另一种形式或格式的过程,解码则是编码的逆过程。
字符编码(Character Encoding)是把字符集中的字符编码为指定集合中的某个对象,以便信息在计算机中传输。在密码学中,加密其实就是在编码,解密其实就是在解码。
Python常用的编码方式:
(1)ASCII码(美国信息交换标准代码)
(2)GBK 和 GB2312(中文编码)
(3)Unicode(全球统一编码)
(4)UTF-8(全球统一编码2)
这写编码方式中,现代计算机主要使用的编码方式是UTF-8.
二、Python常用的加密方式
(1)对称加密(AES、3DES、DES)
(2)非对称加密(RSA、DES、ECDSA、Rabin(RSA的特例)、ELGamal)
(3)散列函数(MD5、SHA)
今天我要讲的就是非对称加密中的RSA算法。
在Python中可以使用rsa库
三、实现过程
1.安装依赖
pip install rsa
2.实现代码
生成公钥:
import rsa
f = rsa.newkeys(1024) # 生成公钥
f = f.save_pkcs1() # 保存为 .pem 格式
with open("public.pem", "wb") as x: # 保存公钥
x.write(f)
生成私钥:
import rsa
e = rsa.newkeys(1024) # 生成私钥
e = e.save_pkcs1() # 保存为 .pem 格式
with open("private.pem", "wb") as x: # 保存私钥
x.write(e)
RSA加密:
import rsa
import base64
password = 'password'#加密内容
print('password:%s' % password)
y = base64.b64encode(password.encode())
print('y:%s' % y)
with open("public.pem", "rb") as x:
f = x.read()
f = rsa.PublicKey.load_pkcs1(f) #公钥
with open("private.pem", "rb") as x:
e = x.read()
e = rsa.PrivateKey.load_pkcs1(e) #私钥
cipher_text = rsa.encrypt(y, f) # 使用公钥加密
print('cipher_text:%s' % cipher_text)
msg = base64.b64encode(cipher_text).decode()
print('msg:%s' % msg)
crypto = base64.b64decode(msg)
print('crypto:%s' % crypto)
text = rsa.decrypt(crypto, e).decode() # 使用私钥解密
print('text:%s' % text)
password = base64.b64decode(text).decode()
print('password:%s' % password)
四、代码汇总
import rsa
import base64
public,private = rsa.newkeys(2048) # 生成公钥、私钥
public = public.save_pkcs1() # 保存为 .pem 格式
with open("public.pem", "wb") as x: # 保存公钥
x.write(public)
private = private.save_pkcs1() # 保存为 .pem 格式
with open("private.pem", "wb") as x: # 保存私钥
x.write(private)
password = 'password'#加密内容
print('password:%s' % password)
y = base64.b64encode(password.encode())
print('y:%s' % y)
with open("public.pem", "rb") as x:
public = x.read()
public = rsa.PublicKey.load_pkcs1(public) #公钥
with open("private.pem", "rb") as x:
private = x.read()
private = rsa.PrivateKey.load_pkcs1(private) #私钥
cipher_text = rsa.encrypt(y, public) # 使用公钥加密
print('cipher_text:%s' % cipher_text)
msg = base64.b64encode(cipher_text).decode()
print('msg:%s' % msg)
crypto = base64.b64decode(msg)
print('crypto:%s' % crypto)
text = rsa.decrypt(crypto, private).decode() # 使用私钥解密
print('text:%s' % text)
password = base64.b64decode(text).decode()
print('password:%s' % password)
五、运行结果
password:password
y:b'cGFzc3dvcmQ='
cipher_text:b'W^\x04~#\x987\xf6\x91\xa9\xbbP4\xfa\xb7Q\x84+(\'\x1e\xa8\xcdl\x06\x87]\xd0\x99\xc7T\x8ff\x14\\,\xb6q/\x8c\xb0\x05\x9d#]\xfc\xd4c-\xfd\xf4\xcbI\x0cr\xa3L!])\xc8\xd0\xe1,x\x1f\x9bh?\xdfa$\x19\x07\xaaYH6\xa6\x07\x86jg`~\xc4\x1e\xacYa,$\xb4\x9f\x80\x00\xdeHtQT\x05\xa1\x92\xf1\xc9\x9a\x076\xde\x8e~\xc2\xb8\xc3\xfc0\xf5\x04\x0e\x82\xe3\xee\xa1\x07PA\xd95\xae\xca)\xca\xbfV\xe1(\xebY\xafQ\x965T\xe1\x10"t)E:\xc3\xc4:\xa6}\x91\xa9\xbb\xbeL\r/0\x9aK\x9f\x13\x15\xa8\xda\xb0Ci\xc7\xf3\x11J\xec".\xc7\x8eT\x88\x0c\x08\\j!{\x83\xdf\x92z\x9b\r\x14\xd7\xabK\xc2\x12\xe9\x7f\xa1Z\xacH\xe5\xa2\xf3\x10\x8a\xfc\x03\xc1<C\x93&\xb3u(y\x17\\b\x91jC\x91\x03\xaf\xecV\xabM\x86}\x95\x81\xbeI[\xc4\xa2D\x17p\xd1i\x95U\x97\x1a'
msg:V14EfiOYN/aRqbtQNPq3UYQrKCceqM1sBodd0JnHVI9mFFwstnEvjLAFnSNd/NRjLf30y0kMcqNMIV0pyNDhLHgfm2g/32EkGQeqWUg2pgeGamdgfsQerFlhLCS0n4AA3kh0UVQFoZLxyZoHNt6OfsK4w/ww9QQOguPuoQdQQdk1rsopyr9W4SjrWa9RljVU4RAidClFOsPEOqZ9kam7vkwNLzCaS58TFajasENpx/MRSuwiLseOVIgMCFxqIXuD35J6mw0U16tLwhLpf6FarEjlovMQivwDwTxDkyazdSh5F1xikWpDkQOv7FarTYZ9lYG+SVvEokQXcNFplVWXGg==
crypto:b'W^\x04~#\x987\xf6\x91\xa9\xbbP4\xfa\xb7Q\x84+(\'\x1e\xa8\xcdl\x06\x87]\xd0\x99\xc7T\x8ff\x14\\,\xb6q/\x8c\xb0\x05\x9d#]\xfc\xd4c-\xfd\xf4\xcbI\x0cr\xa3L!])\xc8\xd0\xe1,x\x1f\x9bh?\xdfa$\x19\x07\xaaYH6\xa6\x07\x86jg`~\xc4\x1e\xacYa,$\xb4\x9f\x80\x00\xdeHtQT\x05\xa1\x92\xf1\xc9\x9a\x076\xde\x8e~\xc2\xb8\xc3\xfc0\xf5\x04\x0e\x82\xe3\xee\xa1\x07PA\xd95\xae\xca)\xca\xbfV\xe1(\xebY\xafQ\x965T\xe1\x10"t)E:\xc3\xc4:\xa6}\x91\xa9\xbb\xbeL\r/0\x9aK\x9f\x13\x15\xa8\xda\xb0Ci\xc7\xf3\x11J\xec".\xc7\x8eT\x88\x0c\x08\\j!{\x83\xdf\x92z\x9b\r\x14\xd7\xabK\xc2\x12\xe9\x7f\xa1Z\xacH\xe5\xa2\xf3\x10\x8a\xfc\x03\xc1<C\x93&\xb3u(y\x17\\b\x91jC\x91\x03\xaf\xecV\xabM\x86}\x95\x81\xbeI[\xc4\xa2D\x17p\xd1i\x95U\x97\x1a'
text:cGFzc3dvcmQ=
password:password
标签:Python,text,RSA,private,rsa,pem,print,password,加密算法
From: https://blog.csdn.net/gaoyuchenw/article/details/140664068