博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中使用RSA算法
阅读量:5233 次
发布时间:2019-06-14

本文共 2762 字,大约阅读时间需要 9 分钟。

# -*- coding: utf-8 -*-# Author: arefulimport base64from Crypto import Randomfrom Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5from Crypto.PublicKey import RSAclass RSACipher(object):    _private_pem = None    _public_pem = None    def __init__(self):        _random_generator = Random.new().read        _rsa = RSA.generate(1024, _random_generator)        self._private_pem = _rsa.exportKey()        self._public_pem = _rsa.publickey().exportKey()    def get_public_key(self):        return self._public_pem    def get_private_key(self):        return self._private_pem    # def load_keys(self):    #     with open('master-public.pem', "r") as f:    #         self._public_pem = f.read()    #     with open('master-private.pem', "r") as f:    #         self._private_pem = f.read()    #    # def save_keys(self):    #     with open('master-public.pem', 'wb') as f:    #         f.write(self._public_pem)    #     with open('master-private.pem', 'wb') as f:    #         f.write(self._private_pem)    def decrypt_with_private_key(self, _cipher_text):        _rsa_key = RSA.importKey(self._private_pem)        _cipher = Cipher_pkcs1_v1_5.new(_rsa_key)        _text = _cipher.decrypt(base64.b64decode(_cipher_text), "ERROR")        return _text.decode(encoding="utf-8")    def encrypt_with_public_key(self, _text):        _rsa_key = RSA.importKey(self._public_pem)        _cipher = Cipher_pkcs1_v1_5.new(_rsa_key)        _cipher_text = base64.b64encode(_cipher.encrypt(_text.encode(encoding="utf-8")))        return _cipher_text    # encrypt with private key & decrypt with public key is not allowed in Python    # although it is allowed in RSA    def encrypt_with_private_key(self, _text):        _rsa_key = RSA.importKey(self._private_pem)        _cipher = Cipher_pkcs1_v1_5.new(_rsa_key)        _cipher_text = base64.b64encode(_cipher.encrypt(_text.encode(encoding="utf-8")))        return _cipher_text    def decrypt_with_public_key(self, _cipher_text):        _rsa_key = RSA.importKey(self._public_pem)        _cipher = Cipher_pkcs1_v1_5.new(_rsa_key)        _text = _cipher.decrypt(base64.b64decode(_cipher_text), "ERROR")        return _text.decode(encoding="utf-8")if __name__ == "__main__":    cipher = RSACipher()    # cipher.save_keys()    # cipher.load_keys()    text = 'Encrypt with public key, and decrypt with private key'    # 公钥加密    cipherText = cipher.encrypt_with_public_key(text)    print(cipherText)    # 私钥解密    plainText = cipher.decrypt_with_private_key(cipherText)    print(plainText)    # # RSA算法本身允许私钥加密公钥解密,实际python不允许    # # raise TypeError("No private key")    # cipherText = cipher.encrypt_with_private_key(text)    # plainText = cipher.decrypt_with_public_key(cipherText)

  

转载于:https://www.cnblogs.com/areful/p/10369365.html

你可能感兴趣的文章
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Lintcode: Partition Array
查看>>
[Linux]PHP-FPM与NGINX的两种通讯方式
查看>>
Java实现二分查找
查看>>
[LintCode] 462 Total Occurrence of Target
查看>>
架构图-模型
查看>>
黑马程序员_Java基础枚举类型
查看>>
UIImage 和 iOS 图片压缩UIImage / UIImageVIew
查看>>
ajax向后台传递数组
查看>>
疯狂JAVA16课之对象与内存控制
查看>>
[转载]树、森林和二叉树的转换
查看>>
软件测试-----Graph Coverage作业
查看>>
django ORM创建数据库方法
查看>>
创建Oracle synonym 详解
查看>>
php7 新特性整理
查看>>
RabbitMQ、Redis、Memcache、SQLAlchemy
查看>>
linux查看端口占用
查看>>
Sql常见面试题 受用了
查看>>