PyCrypto does not have a function that can manage the RSA passphrase.
Instead, you can use ezPyCrypto ( homepage ), which is built on top of the PyCrypto module. It has a simpler interface and allows you to:
- Generate, export, and import public and private keys
- Easy to encrypt and decrypt strings
- It is possible to create encrypted data in the form of text, convenient for e-mail.
- Sign and verify the lines (including documents)
- Protect your private key with a passphrase
- Create "streams" to send data through secure sockets
- Choose any public key size you like (2048 bit recommended)
- Choose between RSA and ElGamal for the public key, as well as IDEA, DES3, Blowfish, ARC4, IDEA for the session key.
- Rest in safety, with 256-bit session keys and protection against common RSA and ElGamal attacks, which will painfully upset anyone who wants to violate your privacy.
Application:
""" example7.py Demonstrate the use of passphrases with private keys """ import ezPyCrypto mysecret = "Don't look at this!!!" raw = "Here is a string to encrypt" # Create a key object k = ezPyCrypto.key(passphrase=mysecret) # Export public/private key publicAndPrivateKey = k.exportKeyPrivate() # Encrypt against this keypair enc = k.encString(raw) # Create a new key object, and import keys (with passphrase) k1 = ezPyCrypto.key(publicAndPrivateKey, passphrase=mysecret) # Decrypt text dec = k.decString(enc) # test if dec == raw: print "Successful decryption using correct passphrase" else: print "Failed somewhere" print "Trying now with a bad passphrase" try: k2 = ezPyCrypto.key(publicAndPrivateKey, passphrase="cracking attempt") except ezPyCrypto.CryptoKeyError: print "Oops - our feeble cracking attempt failed (which is a good thing)." else: print "Cracking attempt succeeded - we're not safe" # We're in - let plunder dec2 = k2.decString(enc)
Build it
If you look at the source of ezCryptoPy, you will see that the key is actually encrypted / decrypted using the BlueFish algorithm:
# decrypt against passphrase blksiz = 8 # lazy of me # create temporary symmetric cipher object for passphrase - #hardwire to Blowfish ppCipher = Blowfish.new(passphrase, Blowfish.MODE_CFB, self._passIV[0:blksiz]) enclen = len(keyobj) decpriv = '' i = 0 while i < enclen: decbit = ppCipher.decrypt(keyobj[i:i+blksiz]) decpriv += decbit i += blksiz keyobj = decpriv[0:size]
This means that you can write your own passphrase processor using the previous code example without installing ezPyCrypto. Here you can find many code examples on how to do it yourself: Search for a zero word
My first and alternative solution:
You can use the python function exec () and the command line function "ssh-keygen" ( doc ):
ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile] .
timgluz
source share