11

I have a string, which is actually password. I want to encrypt the string and want to store the encrypted result in a parameter file. Next during execution of a script that encrypted string will be picked up and in run time that will be decrypt. So I want to know how to encrypt and decrypt a string/text in linux environment?

Koushik Chandra
  • 1,451
  • 11
  • 37
  • 67
  • see http://stackoverflow.com/questions/16056135/how-to-use-openssl-to-encrypt-decrypt-files ? – OznOg Nov 15 '15 at 10:26
  • The only concern is it is asking for password during encription and decryption which I don't want. Can we set it up this password less – Koushik Chandra Nov 15 '15 at 11:10

3 Answers3

27

This isn't quite what was being asked for here but it shows a simple way to do this without a password prompt

encode.sh

#!/usr/bin/env bash

echo $1 | openssl aes-256-cbc -a -salt -pass pass:somepassword

decode.sh

#!/usr/bin/env bash

echo $1 | openssl aes-256-cbc -d -a -pass pass:somepassword

make files executable

chmod +x encode.sh decode.sh

encode example

./encode.sh "this is my test string"
# => U2FsdGVkX18fjSHGEzTXU08q+GG2I4xrV+GGtfDLg+T3vxpllUc/IHnRWLgoUl9q

decode example

./decode.sh U2FsdGVkX18fjSHGEzTXU08q+GG2I4xrV+GGtfDLg+T3vxpllUc/IHnRWLgoUl9q
# => this is my test string
casonadams
  • 595
  • 5
  • 6
2

openssl can do it better.

encode:

$ secret=$(echo "this is a secret." | openssl enc -e -des3 -base64 -pass pass:mypasswd -pbkdf2)

decode:

$ echo "${secret}" | openssl enc -d -des3 -base64 -pass pass:mypasswd -pbkdf2

tips:

  • you can remove arg -pass and intput password according to the prompt.
zhanw15
  • 21
  • 2
-14

You can use 'base64' command to encrypt and decrypt the string e.g.

$ echo "mypassword" |base64 > env.conf

For extraction you can do following

$ MyPass=`cat $PRG_HOME/env.conf | base64 --decode`

Hope this helps

  • 18
    This is not encryption - it's obfuscation at best. There is no secret/key protecting the decoding of the data. – taifwa Jul 25 '18 at 08:51
  • encryption and encoding are two different use cases, encryption is about data security and encoding is about data transport, here base64 is used to encode data which is one of best ways to transport binary data – naren2605 Jul 23 '21 at 05:20