RSA加密是非对称加密 . 非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。简单的说是“公钥加密,私钥解密;私钥加密,公钥解密”。
class RsaCrypto(object):
def __init__(self,encrypt_content):
self.content = self.content.encode('utf-8')
def entrypto(self):
"""
1.首先获取加密的公钥
2.利用公钥加密数据
"""
pub_key = """pub_key"""
rsa_key = RSA.import_key(pub_key)
cipher = PKCS1_v1_5.new(rsa_key)
entrypto_content = cipher.encrypt(self.key)
entrypto_content = urlsafe_b64encode(entrypto_content)
return entrypto_content.decode('utf-8')
def decrypto(self,entrypto_content):
# 先获取私钥,利用私钥进行解密
private_key = """"""
entrypto_content = urlsafe_b64decode(entrypto_content)
rsa_key = RSA.import_key(private_key)
cipher = PKCS1_v1_5.new(rsa_key)
decrypto_content = cipher.decrypt(entrypto_content, b'decrypto error')
return decrypto_content.decode('utf-8')
if __name__ == '__main__':
key = "8ymWLWJzYA1MvLF8"
rsa_crypto=RsaCrypto(key)
entrypto_content=rsa_crypto.entrypto()
decrypto_content=rsa_crypto.decrypto(entrypto_content)