I have a Migrate.ksh file, in that some passwords are available, I would like to encrypt this file in the Unix. The Passwords file being used in another script. Can you please let me know the process and script to encrypt?


1 Answer 1


If you have gpg on your machine:

Encrypting the file

gpg --gen-key
<choose 1>
<enter 2048 bits>
<enter 0 -- key does not expire>
<y> <enter>
"enter a user id for later like admin or your user name of choice"
<enter> o <enter>

then type a password accept, enter it again -- now you have a new key.

Then run

gpg -e Migrate.ksh 

and use user id from earlier Now ls should show you the new encrypted file Migrate.ksh.gpg

If you have any trouble man gpg or gpg -h can help.

Decrypting the file

gpg -d FileNameEncrypted.gpg -o Newfile
  • Hello Michael. I felt really breathless after reading that. Would you like some full stops? I've got a few spare in my pocket :-O If would also be somewhat more readable if you formatted the code. Code inline is marked out with backticks. Code blocks are indented four spaces and have a blank line before and after.
    – roaima
    Oct 31, 2018 at 13:19
  • While RSA is still the default choice for keys in gpg, Curve25519 keys are much shorter with decreased encryption and decryption times.... You can generate a Curve25519 key using the gpg --expert --full-gen-key command and select option 9 ECC and ECC Oct 31, 2018 at 13:49
  • Is there any benefit to creating a keypair over a password / symmetric ciper, in this single computer situation? You still have to remember a password with the keypair, plus they can be deleted/corrupted like a keyfile
    – Xen2050
    Oct 31, 2018 at 14:32
  • @Xen2050 ... There are various use cases for keys vs. symmetric ... A particularly interesting use case for keys is to have a passphrase-less key loaded onto a physical external device, such as a smart card. This would enable seamless and transparent decryption of files and messages as long as the physical device is connected. In general though, passphrase-less keys are discouraged... There are important differences between passphrases and passwords. Passphrases are not transmitted across a network, while passwords are transmitted across a network. Oct 31, 2018 at 15:03
  • @RubberStamp Just seems like overkill in this situation to have a password to a key, without using the other benefits of public key cryptography. Haven't heard of the passphrase - password difference, aside from actually using more than one word in a passphrase, but password is a little more commonly known (& easier to type ;-)
    – Xen2050
    Oct 31, 2018 at 15:09

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