zoukankan      html  css  js  c++  java
  • Python中使用RSA算法

    # -*- coding: utf-8 -*-
    # Author: areful
    
    import base64
    
    from Crypto import Random
    from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
    from Crypto.PublicKey import RSA
    
    
    class 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)
    

      

  • 相关阅读:
    神经网络与数字货币量化交易系列(1)——LSTM预测比特币价格
    FMZ发明者量化平台回测机制说明
    使用JavaScript实现量化策略并发执行
    数字货币期货与现货JavaScript量化策略代码详解汇总
    极简比特币高频策略机器人
    爬虫爬取币安公告自动出售将要下架币策略
    Deribit交易所 websocket API 连接范例
    极简版OKEX比特币跨期对冲策略
    OKEX websocket API 连接Python范例
    Vmare安装Linux 虚拟机流程
  • 原文地址:https://www.cnblogs.com/areful/p/10369365.html
Copyright © 2011-2022 走看看