#coding:utf-8
"""
orangleliu 2017.5.14 rsa_test.py
python2.7
pycrypto (2.6.1)
"""
import base64
from Crypto.PublicKey import RSA as rsa
from Crypto.Cipher import PKCS1_v1_5
pub_key_str = """-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAuw4T755fepEyXTM66pzf6nv8NtnukQTMGnhmBFIFHp/P2vEpxjXU
BBDUpzKkVFR3wuK9O1FNmRDAGNGYC0N/9cZNdhykA1NixJfKQzncN31VJTmNqJNZ
W0x7H9ZGoh2aE0zCCZpRlC1Rf5rL0SVlBoQkn/n9LnYFwyLLIK5/d/y/NZVL6Z6L
cyvga0zRajamLIjY0Dy/8YIwVV6kaSsHeRv2cOB03eam6gbhLGIz/l8wuJhIn1rO
yJLQ36IOJymbbNmcC7+2hEQJP40qLvH7hZ1LaAkgQUHjfi8RvH2T1Jmce7XGPxCo
Ed0yfeFz+pL1KeSWNey6cL3N5hJZE8EntQIDAQAB
-----END RSA PUBLIC KEY-----"""
priv_key_str = """-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuw4T755fepEyXTM66pzf6nv8NtnukQTMGnhmBFIFHp/P2vEp
xjXUBBDUpzKkVFR3wuK9O1FNmRDAGNGYC0N/9cZNdhykA1NixJfKQzncN31VJTmN
qJNZW0x7H9ZGoh2aE0zCCZpRlC1Rf5rL0SVlBoQkn/n9LnYFwyLLIK5/d/y/NZVL
6Z6Lcyvga0zRajamLIjY0Dy/8YIwVV6kaSsHeRv2cOB03eam6gbhLGIz/l8wuJhI
n1rOyJLQ36IOJymbbNmcC7+2hEQJP40qLvH7hZ1LaAkgQUHjfi8RvH2T1Jmce7XG
PxCoEd0yfeFz+pL1KeSWNey6cL3N5hJZE8EntQIDAQABAoIBAGim1ayIFK8EMQNH
uDyui/Aqcc9WWky0PGTK23irUsXxb1708gQ89WNY70Cj6qBrqZ1VMb3QHPP4FSFN
kh0rJJoi2g+ssm5R5r5KlhTKeFRrQInVC1Y3KhUUUwZa4aWtnhgS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R4lCrnAkEtz23vhZKWhFw==
-----END RSA PRIVATE KEY-----"""
def encrypt(pub_key_str, msg):
pubobj = rsa.importKey(pub_key_str)
pubobj = PKCS1_v1_5.new(pubobj)
return base64.b64encode(pubobj.encrypt(msg))
def decrypt(priv_key_str, msg):
privobj = rsa.importKey(priv_key_str)
privobj = PKCS1_v1_5.new(privobj)
# 关于 privobj.decrypt 第二个参数请见
# https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Cipher/PKCS1_v1_5.py#L154
return privobj.decrypt(base64.b64decode(msg), 'xyz')
if __name__ == "__main__":
msg = "1234567890654xxx"
mmsg = encrypt(pub_key_str, msg)
print mmsg
dmsg = decrypt(priv_key_str, mmsg)
print dmsg