RSA encryption and decryption in Python

前端 未结 6 1892
别那么骄傲
别那么骄傲 2020-11-28 06:35

I need help using RSA encryption and decryption in Python.

I am creating a private/public key pair, encrypting a message with keys and writing message to a file. Th

6条回答
  •  -上瘾入骨i
    2020-11-28 07:11

    Here is my implementation for python 3 and pycrypto

    from Crypto.PublicKey import RSA
    key = RSA.generate(4096)
    f = open('/home/john/Desktop/my_rsa_public.pem', 'wb')
    f.write(key.publickey().exportKey('PEM'))
    f.close()
    f = open('/home/john/Desktop/my_rsa_private.pem', 'wb')
    f.write(key.exportKey('PEM'))
    f.close()
    
    f = open('/home/john/Desktop/my_rsa_public.pem', 'rb')
    f1 = open('/home/john/Desktop/my_rsa_private.pem', 'rb')
    key = RSA.importKey(f.read())
    key1 = RSA.importKey(f1.read())
    
    x = key.encrypt(b"ffffdffffd",32)
    
    print(x)
    z = key1.decrypt(x)
    print(z)
    

提交回复
热议问题