thanks @user9527 vote up for you
that solved my problem
my env: win10x64 python3.6.4 pycrypto2.6.1
here's my code, encrypt end decrypt, the key was from someone's blog.(if U occured with "ValueError: RSA key format is not supported", check the key format, it should be warpped with some thing like "-----BEGIN XXXX KEY-----")
pubkey = """-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----"""
prvkey = """-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----"""
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
msg = "test"
print("raw msg->", msg)
keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
#print(cipher.encrypt.__doc__)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)
keyPriv = RSA.importKey(prvkey) # import the private key
cipher = Cipher_PKCS1_v1_5.new(keyPriv)
#print(cipher.decrypt.__doc__)
decrypt_text = cipher.decrypt(cipher_text, None).decode()
print("decrypted msg->", decrypt_text)
assert msg == decrypt_text # check that
print("test passed")
the output:
raw msg-> test
cipher text-> b'\xb0]\x1f@B\x8b\xb5\xbf\x891:\t4D\x80$\xc0y\xaa\xb4\x86t/|\xeaM%\xf06\x14,\x9e?\x86R\x83\xd72\xe5\xfdsr:\x99\xe7v\xd9]&\xbc\x85\xd3\x16\x80\x19q\xe7\xb1\x89\xff/\x12\xe5\xb3\x9cu\x1f\x04x\xa5\xdfl\xcd\xae_\xba\x1b\x97\x9fa\xcf9O\xbfB\xf6\xd1N\xf5|<\xbf^\x84R\xecSo\x9a*\xf7\x8d\x8e\xbe0Q\xcd\x14\x13\xf98x\xe7\xd8x\x19\xaf\x98\xefu\xa8\xb1\xd3\xfa\xf2N\xca\xb5'
decrypted msg-> test
test passed