python3 使用Pycrypto进行RSA加密
2018-10-18 本文已影响12人
高思阳
import base64
from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.PublicKey import RSA
message = b"this is test"
rsa_private_key = b'-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEAz4C8rDjl6NAAvdUxLl7p8eA+xxc4Sa20puq2gFPk+8B5W30o+8xAaktLoji9wEsf7WTEllxZcIXw7d4K9b1M34ZDQT1iR4ZreB3GkxEXDeQIV3f/W7C3NL1bF5ePn7DRGZYKTEry8pp/lK/1ESP9Z+giaN29xm+jsmvNAOJlLjndn8nYcmA1sdDzuX8JSlZOiijAwyKUkwrBd+FqlhbAfF9ebcEgQcRISZsBMpcsZ66HQ0nvEnhmLzkfXxLUu2w7ccwLc/W7a4V1SNFEMZifRyiI7IhYYpRFw7egQN2RgzVWW/O6HKzk2Mxaz/6Y+HEehuDfRDn5MjMCkhnfbpRKKwIDAQABAoIBAD7xpkn32IYO0qrDCPAwCnc8ts5d1M1EzA/2EvZKfKOBd06xYMVA6ClWPdMvsx+pqj2w25Fg4iCoALWnLVvt2GvVpoEbxbw8ok0Fez8RaBTmn0EpPWTq7igpaIB3kTG3yTYhtW3/LkM5aooH9icE69GhMQ7udKrFIrr0yHMEOf0TNKBUNlwwHWJrpUuodXxIbERonkNdri1AnsWsnh4ePkKFlgaIdOZ24W4kHjfQAQkz3bPTjRzU5Q0LpkCpuYxk+TeOvR5tNPHXtzTWYek7JbCbnIKr1SiJx+ncwWM7QEnuvbzCU2VeTwb6Rq94WIg3RpfqSKJPr5w5e/df4jmi58ECgYEA74ty4UiChdp27/xvy2sJMilgkcL5Z0A+McPrjM82CqfPvr1+ZMBkTwuQGxFWlNtdE7DLpneVCWmzAexUJ02dFndSJMqsN81335VYfLc94qk5HXwmc9UBK7+qL2Ugyz76KoRGogqkKYw0ZR2kdpcNoXjQfeVgqrumt+jnZChpAl8CgYEA3cHPuDbdqqhcslz4+Zz8B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gzVWW/O6HKzk2Mxaz/6Y+HEehuDfRDn5MjMCkhnfbpRKKwIDAQAB\n-----END PUBLIC KEY-----\n'
rsakey = RSA.importKey(rsa_public_key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
cipher_text = base64.b64encode(cipher.encrypt(message))
print(cipher_text)
rsakey = RSA.importKey(rsa_private_key)
cipher = Cipher_pkcs1_v1_5.new(rsakey)
random_generator = Random.new().read
text = cipher.decrypt(base64.b64decode(cipher_text), None)
print(text.decode('utf8'))