一 代码
import rsa
key = rsa.newkeys(3000)#生成随机秘钥
privateKey = key[1]#私钥
publicKey = key[0]#公钥
message ='sanxi Now is better than never.'
print('Before encrypted:',message)
message = message.encode()
cryptedMessage = rsa.encrypt(message, publicKey)
print('After encrypted:\n',cryptedMessage)
message = rsa.decrypt(cryptedMessage, privateKey)
message = message.decode()
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<u-b\x10\xfc\xdb\xf0\xeb\xcc\x91[\xfdv\\\xbfI\xe3\x1f$O\xe2\xd5\x1a\xb7Q\x87\xcf\xec\x10\xce\x8f\xa5\x16Q\x12\x93\xc0\xae\x9fm\xae\x00\xeb\x97@\xbb\x022\x12L\x99\xd8\nF\x8eP\x91\x8a\xf5\xc5\xa2t;9K\\o\xfc. .\xe7A^_F\xe0\xeek\x9cp\xc2\xe0\xfbB\xe4\x06\xbaU\xf5\x04\xb7\xd2;`\x94\xb67\xe5\xaa\x08G\xed\x1a\x83\xec3\xdbC\x1d\x18~\x18\xbcM\x97\xd7\xcfh1D\x05\x12"
After decrypted: sanxi Now is better than never.