python实现rsa加密

阅读更多

一 代码

  1. import rsa
  2. key = rsa.newkeys(3000)#生成随机秘钥
  3. privateKey = key[1]#私钥
  4. publicKey = key[0]#公钥
  5. message ='sanxi Now is better than never.'
  6. print('Before encrypted:',message)
  7. message = message.encode()
  8. cryptedMessage = rsa.encrypt(message, publicKey)
  9. print('After encrypted:\n',cryptedMessage)
  10. message = rsa.decrypt(cryptedMessage, privateKey)
  11. message = message.decode()
  12. print('After decrypted:',message)
二 运行结果
Before encrypted: sanxi Now is better than never.
After encrypted:
 b"P\xaa\xfb\x13\x0c\xbd\x92\x9cn\x98\xab\xe0C\xa8B\x08\xbbQ\x83w\x93*\xeai\x8d\xec\xeb\xb2+\x8d\x01\xa5\xdeO\xc2\x87\x81\x19\xf9\x96\x14h\xf6\xa2\x14.Q>K\x92\xcdd\xa4\xb0\xee!+\xea\xb7)\x8f\xa5iN\xedY%#\xcdE\xae\xc1#\x9d\xb2\xd9\xe4C)|\xc2[$\xb1\x04\xc1|a\x99B\xbf\x0c\x95\x85\x96A\x99f_\xfa(\x0f\x00\x9av\x07\x82\xcb\x8b\x8b\xd9\xfe\xd23\xb9\x8a\x0b\x17\x89\x9d\x88\xc4?\x88c\xf4\x89eO\x1a\x84\xec\x08\xffZ6\xefL\x08!\xb0\xc0\xda\r\x1ah\x84\x84\x1dEHKaC\x07\xe5&\x7f\x85c\x1b\xcc42\x13\xd3\xe8`\xf2\x98l\x88X/`\xa0MBqA\xacT\xf4\xc8Y\xd2qNd1\x9cI\x1d\xff\x9f\x05\x8bI\xb88'}\xddaQ\xb5DQ\xd4r(\x0f\xd5{=\xcd`vq\xca\x80\x14\x9b\x04\xce\xb1%A\x1d\xaf\x03+(\x17\xb1\xb4l\xa2\x94\xf1B\xca\x95\xd6\xd4\xbb\xbc\x87p\xbd\xe8
After decrypted: sanxi Now is better than never.

你可能感兴趣的:(python,rsa)