导出使用 python-rsa 加密的邮件
Export message that encrypted with python-rsa
我使用此代码创建 public 和私钥:
import rsa
(pubkey, privkey) = rsa.newkeys(512)
然后转换为PEM格式:
exppub =pubkey.save_pkcs1(format='PEM')
exppriv = privkey.save_pkcs1(format='PEM')
当我用这个密钥加密消息时:
message = 'hello Bob!'
crypto = rsa.encrypt(message, pubkey)
得到的密文是这样的:
"@\xc4\xb2\x14r\xf1x\xb8\xb2\t;\x9a:\x1dl\x11\xe2\x10\xa9E\xee\x8b\xac\xd0\xd3Y\xfb}\xd9@\xdd\x0c\xa5\xd2\xfc1\xd6\x06\xf0\xb8\x944\xe1\xc2r\xe5anyq\xac\xdfh\xeb\x10\x80\x98\xa1\xee'\xe6hpi"
我知道它应该是这样的:
SEcPB1mYNrfeE4zP4RI3z2K4Rh9HDNfPhuF28IyxHFjEOJ9Z+1zdIwPF0jsJGQDJyKpAju7dcYueHHXXeH8d+w==
如何更改此格式?
您可能会注意到,每次生成加密消息时都会发生变化:
>>> import rsa
>>>
>>> (pubkey, privkey) = rsa.newkeys(512)
>>> message = 'hello Bob!'
>>> for _ in range(5):
... crypto = rsa.encrypt(message, pubkey)
... print "Ciphertext: %r" % crypto
... assert rsa.decrypt(crypto, privkey) == message
...
Ciphertext: "eYE\xab\x1c^z\xf2\x8f\xf7\xbc\xea\x06\x86e!'\xd9\x17=ElrU!\xfc\xb1l0\x0eU\x83\xa6j\x00\xe7\t\x1f8C\xefBA\x15\xf7\xd9\x9a\xfc\x04nw\xeb\xce\xa0\xbeCg\x85t\x99N`\xee\xe1"
Ciphertext: '\x87\x00\xbcg\x08\xed\xe6\x8e\xb5\xb7\x8dS\xb0\x8f\xb86\xd4\xe5%\x01\x8b\x82\xe3\xc1o\x8d{\xc7\x08\xed\x8a\x02\xd6TTh\x84yP\x9co\xde\x80Wkj\xc7\xb8\xba>\xe8?\x93\xd5\\x1e\xdc\x14\xa83\x8bY+\xdc'
Ciphertext: 'K\x15\x90\xd9\x91\xf2W\x1ae_\xf1\xf2m\x1f\x034\x95\xec\xc4w\xf2\xff{\xc2\r\x97\x95\x0e\xf3\x91\xd1P1\x90\xee\xe2\xe9\xba\x8d\x8f\xa1I\x06G\xb2\x0e\xbdd\xa3\xc9/\x9f(\x95\x04\xcc\x16\x1c\xee\x86\xf2\xe1c-'
Ciphertext: "\t?v\xfe\xf6\xfdI\xd7\xae\xa4\x01o\x9bH@.\x17\xc2\xdbaQ:\xaf?\x14\x96\x03'\xea\x0b:\x9d\x80\xae\xebj&\xf8\x9bT \xf9\x8bL+^\xc2\xff?\xb6\xac\x0f\x07#\x19\xd1\xa7\x80z\x1f3*5j"
Ciphertext: '\x1d]\x04\x83&\xd2\x0c\x1a\xde\xbb\xa9L\xbb\xae\xa3t\xf3H~\xdd\x14\x1a\xd2\xe5V*z\x1f1V\xebl\xc4\x06,\xb8y:w\xe4X\x80\xa9\r\xcd0\xc0Ia}\xe1\xb0\xbd\xb5p\xbf\xaa\xce\x13\xca\xa3k_D'
所以我不希望它总是一样。此外,您预期的字符串实际上是 base64 编码的,这是另一个差异来源:
>>> 'SEcPB1mYNrfeE4zP4RI3z2K4Rh9HDNfPhuF28IyxHFjEOJ9Z+1zdIwPF0jsJGQDJyKpAju7dcYueHHXXeH8d+w=='.decode('base64')
'HG\x0f\x07Y\x986\xb7\xde\x13\x8c\xcf\xe1\x127\xcfb\xb8F\x1fG\x0c\xd7\xcf\x86\xe1v\xf0\x8c\xb1\x1cX\xc48\x9fY\xfb\\xdd#\x03\xc5\xd2;\t\x19\x00\xc9\xc8\xaa@\x8e\xee\xddq\x8b\x9e\x1cu\xd7x\x7f\x1d\xfb'
我使用此代码创建 public 和私钥:
import rsa
(pubkey, privkey) = rsa.newkeys(512)
然后转换为PEM格式:
exppub =pubkey.save_pkcs1(format='PEM')
exppriv = privkey.save_pkcs1(format='PEM')
当我用这个密钥加密消息时:
message = 'hello Bob!'
crypto = rsa.encrypt(message, pubkey)
得到的密文是这样的:
"@\xc4\xb2\x14r\xf1x\xb8\xb2\t;\x9a:\x1dl\x11\xe2\x10\xa9E\xee\x8b\xac\xd0\xd3Y\xfb}\xd9@\xdd\x0c\xa5\xd2\xfc1\xd6\x06\xf0\xb8\x944\xe1\xc2r\xe5anyq\xac\xdfh\xeb\x10\x80\x98\xa1\xee'\xe6hpi"
我知道它应该是这样的:
SEcPB1mYNrfeE4zP4RI3z2K4Rh9HDNfPhuF28IyxHFjEOJ9Z+1zdIwPF0jsJGQDJyKpAju7dcYueHHXXeH8d+w==
如何更改此格式?
您可能会注意到,每次生成加密消息时都会发生变化:
>>> import rsa
>>>
>>> (pubkey, privkey) = rsa.newkeys(512)
>>> message = 'hello Bob!'
>>> for _ in range(5):
... crypto = rsa.encrypt(message, pubkey)
... print "Ciphertext: %r" % crypto
... assert rsa.decrypt(crypto, privkey) == message
...
Ciphertext: "eYE\xab\x1c^z\xf2\x8f\xf7\xbc\xea\x06\x86e!'\xd9\x17=ElrU!\xfc\xb1l0\x0eU\x83\xa6j\x00\xe7\t\x1f8C\xefBA\x15\xf7\xd9\x9a\xfc\x04nw\xeb\xce\xa0\xbeCg\x85t\x99N`\xee\xe1"
Ciphertext: '\x87\x00\xbcg\x08\xed\xe6\x8e\xb5\xb7\x8dS\xb0\x8f\xb86\xd4\xe5%\x01\x8b\x82\xe3\xc1o\x8d{\xc7\x08\xed\x8a\x02\xd6TTh\x84yP\x9co\xde\x80Wkj\xc7\xb8\xba>\xe8?\x93\xd5\\x1e\xdc\x14\xa83\x8bY+\xdc'
Ciphertext: 'K\x15\x90\xd9\x91\xf2W\x1ae_\xf1\xf2m\x1f\x034\x95\xec\xc4w\xf2\xff{\xc2\r\x97\x95\x0e\xf3\x91\xd1P1\x90\xee\xe2\xe9\xba\x8d\x8f\xa1I\x06G\xb2\x0e\xbdd\xa3\xc9/\x9f(\x95\x04\xcc\x16\x1c\xee\x86\xf2\xe1c-'
Ciphertext: "\t?v\xfe\xf6\xfdI\xd7\xae\xa4\x01o\x9bH@.\x17\xc2\xdbaQ:\xaf?\x14\x96\x03'\xea\x0b:\x9d\x80\xae\xebj&\xf8\x9bT \xf9\x8bL+^\xc2\xff?\xb6\xac\x0f\x07#\x19\xd1\xa7\x80z\x1f3*5j"
Ciphertext: '\x1d]\x04\x83&\xd2\x0c\x1a\xde\xbb\xa9L\xbb\xae\xa3t\xf3H~\xdd\x14\x1a\xd2\xe5V*z\x1f1V\xebl\xc4\x06,\xb8y:w\xe4X\x80\xa9\r\xcd0\xc0Ia}\xe1\xb0\xbd\xb5p\xbf\xaa\xce\x13\xca\xa3k_D'
所以我不希望它总是一样。此外,您预期的字符串实际上是 base64 编码的,这是另一个差异来源:
>>> 'SEcPB1mYNrfeE4zP4RI3z2K4Rh9HDNfPhuF28IyxHFjEOJ9Z+1zdIwPF0jsJGQDJyKpAju7dcYueHHXXeH8d+w=='.decode('base64')
'HG\x0f\x07Y\x986\xb7\xde\x13\x8c\xcf\xe1\x127\xcfb\xb8F\x1fG\x0c\xd7\xcf\x86\xe1v\xf0\x8c\xb1\x1cX\xc48\x9fY\xfb\\xdd#\x03\xc5\xd2;\t\x19\x00\xc9\xc8\xaa@\x8e\xee\xddq\x8b\x9e\x1cu\xd7x\x7f\x1d\xfb'