Say I have previously created a private/public key combination, and decided at the time to not protect the private key with a password. If I later decide to "beef up" security and use a password-protected private key instead, would I need to generate a new private/public key pair, or can I simply add a password to my existing private key?

Is the opposite possible as well, can I "remove" a password from an existing private key?

Of course you can add/remove a passphrase at a later time.

  • add one (assuming it was an rsa key, else use dsa)

    openssl rsa -aes256 -in your.key -out your.encrypted.key
    mv your.encrypted.key your.key
    chmod 600 your.key
    

    the -aes256 tells openssl to encrypt the key with AES256.

    As ArianFaurtosh has correctly pointed out: For the encryption algorithm you can use aes128, aes192, aes256, camellia128, camellia192, camellia256, des (which you definitely should avoid), des3 or idea

  • remove it

    openssl rsa -in your.key -out your.open.key
    

    you will be asked for your passphrase one last time
    by omitting the -aes256 you tell openssl to not encrypt the output.

    mv your.open.key your.key
    chmod 600 your.key
    
  • 3
    don't forget to chmod 600 your newly encrypted key – Jens Timmerman Dec 14 '17 at 11:05

When a private is "protected by a password", it merely means that the key bytes, as stored somewhere, are encrypted with a password-derived symmetric key. A private key is readily encodable as a sequence of bytes, and can be copied, encrypted and decrypted just like any file. The important point here is that the password is all about storage: when the private key is to be used (e.g. to sign something), then it is first decrypted in the RAM of some computer, which then proceeds to use the non-encrypted private key. Correspondingly, there is nothing special in a RSA key pair which would make it suitable or unsuitable for password protection. Password protection is really an orthogonal issue.

Of course, if a private key has ever been stored on some physical medium (say, a hard disk) without any extra protection, then it may have left exploitable traces there. Details depend a lot on what system is actually used for private key storage. For instance, Windows systems use DPAPI for storing user's private keys, and DPAPI makes some extra efforts at not letting stored keys leak (whether these efforts are successful remains to be proven).

  • 1
    May I suggest you explain the meaning of "orthogonal" in this case? – guntbert Jun 1 '14 at 16:57
  • 3
    @guntbert, in this context 'orthogonal' could be defined as "related but separate". Ie. the issues of certificate validity and certificate security are related (in that they both affect the security and functioning of the system) but they're distinct problems and their solutions don't directly interact. – Molomby Mar 22 '16 at 3:52
  • Your machine might be compromised in such a way that an attacker can read all your files on your mounted encrypted harddisk, but can't read your ram. In this threath model encrypting your private key gives you extra security. – Jens Timmerman Dec 14 '17 at 11:08

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.