Use the OpenSSL tool to write Bash scripts to encrypt and decrypt password plaintext

Use OpenSSL tools to encrypt and decrypt password plaintext

Written By: Xinyao Tian

Introduction

This document describes a simple way to encrypt and decrypt passwords in a Bash script using the OpenSSL tool.

BASE64 encryption and decryption script

Use Base64 algorithm to encrypt password

The script name is encryptPasswd.sh, and the script content is as follows:

#!/bin/bash
# Script developed by Xinyao Tian on 2023/08/10

echo "INFO: Encrypting plain text password through $0"

passwd_plaintext=$1

passwd_encrypted=`echo $passwd_plaintext | openssl enc -base64`

echo "INFO: Encrypted password is:"

echo $passwd_encrypted

Use Base64 algorithm to decrypt password

The script name is decryptPasswd.sh, and the script content is as follows:

#!/bin/bash
# Script developed by Xinyao Tian on 2023/08/10

echo "INFO: Decrypting encrypted password through $0"

passwd_encrypted=$1

passwd_plaintext=`echo $passwd_encrypted | openssl enc -base64 -d`

echo "INFO: Decrypted password is:"

echo $passwd_plaintext

How to use

View the script in the directory:

[flinkrt@p0-tkldmp-rc01 ~]$ ls -l
total 8
-rwxr--r-- 1 flinkrt flinkrt 217 Aug 10 14:21 decryptPasswd.sh
-rwxr--r-- 1 flinkrt flinkrt 212 Aug 10 14:19 encryptPasswd.sh

How to use encryption is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./encryptPasswd.sh 123456
INFO: Encrypting plain text password through ./encryptPasswd.sh
INFO: Encrypted password:
MTIzNDU2Cg==

The decryption method is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./decryptPasswd.sh MTIzNDU2Cg==
INFO: Decrypting encrypted password through ./decryptPasswd.sh
INFO: Decrypted password is:
123456

BASE64-withPassphrase encryption and decryption script

Use BASE64-withPassphrase algorithm to encrypt passwords

The script name is encryptPasswdWithKey.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Encrypting plain text password through $0"

passwd_plaintext=$1

passphrase=$2

integrated_passwd="$passphrase$passwd_plaintext"

passwd_encrypted=`echo $integrated_passwd | openssl enc -base64`

echo "INFO: Encrypted password is:"

echo $passwd_encrypted

Use Base64 algorithm to decrypt password

The script name is decryptPasswdWithKey.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Decrypting encrypted password through $0"

passwd_encrypted=$1

integrated_passwd=`echo $passwd_encrypted | openssl enc -base64 -d`

passphrase=$2

lengthOfPassphrase=`echo ${<!-- -->#passphrase}`

passwd_plaintext=`echo ${<!-- -->integrated_passwd: lengthOfPassphrase}`

echo "INFO: Decrypted password is:"

echo $passwd_plaintext

How to use

View the script in the directory:

[flinkrt@p0-tkldmp-rc01 ~]$ ls -l | grep WithKey
-rwxr--r-- 1 flinkrt flinkrt 341 Aug 10 14:56 decryptPasswdWithKey.sh
-rwxr--r-- 1 flinkrt flinkrt 281 Aug 10 14:52 encryptPasswdWithKey.sh

How to use encryption is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./encryptPasswdWithKey.sh 123456 ~HbATOlWRYD%Ja0WcOpQ9,mcK + ~YMLuP
INFO: Encrypting plain text password through ./encryptPasswdWithKey.sh
INFO: Encrypted password is:
fkhiQVRPbFdSWUQlSmEwV2NPcFE5LG1jSyt + WU1MdVAxMjM0NTYK

The decryption method is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./decryptPasswdWithKey.sh fkhiQVRPbFdSWUQlSmEwV2NPcFE5LG1jSyt + WU1MdVAxMjM0NTYK ~HbATOlWRYD%Ja0WcOpQ9,mcK + ~YMLuP
INFO: Decrypting encrypted password through ./decryptPasswdWithKey.sh
INFO: Decrypted password is:
123456

BASE64-withFixedPassphrase encryption and decryption script

Use BASE64-withFixedPassphrase algorithm to encrypt passwords

The script name is encryptPasswdWithFixedKey.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Encrypting plain text password through $0"

passwd_plaintext=$1

passphrase=GMPHwOqsIoCsqaEAYIoSRWEfcfQ2kA52tFXDbtri0I8oW2cLAR

integrated_passwd="$passphrase$passwd_plaintext"

passwd_encrypted=`echo $integrated_passwd | openssl enc -base64`

echo "INFO: Encrypted password is:"

echo $passwd_encrypted

Use Base64 algorithm to decrypt password

The script name is decryptPasswdWithFixedKey.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Decrypting encrypted password through $0"

passwd_encrypted=$1

integrated_passwd=`echo $passwd_encrypted | openssl enc -base64 -d`

passphrase=GMPHwOqsIoCsqaEAYIoSRWEfcfQ2kA52tFXDbtri0I8oW2cLAR

lengthOfPassphrase=`echo ${<!-- -->#passphrase}`

passwd_plaintext=`echo ${<!-- -->integrated_passwd: lengthOfPassphrase}`

echo "INFO: Decrypted password is:"

echo $passwd_plaintext

How to use

View the script in the directory:

[flinkrt@p0-tkldmp-rc01 ~]$ ls -l | grep WithKey
-rwxr--r-- 1 flinkrt flinkrt 341 Aug 10 14:56 decryptPasswdWithKey.sh
-rwxr--r-- 1 flinkrt flinkrt 281 Aug 10 14:52 encryptPasswdWithKey.sh

How to use encryption is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./encryptPasswdWithKey.sh 123456 ~HbATOlWRYD%Ja0WcOpQ9,mcK + ~YMLuP
INFO: Encrypting plain text password through ./encryptPasswdWithKey.sh
INFO: Encrypted password is:
fkhiQVRPbFdSWUQlSmEwV2NPcFE5LG1jSyt + WU1MdVAxMjM0NTYK

The decryption method is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./decryptPasswdWithKey.sh fkhiQVRPbFdSWUQlSmEwV2NPcFE5LG1jSyt + WU1MdVAxMjM0NTYK ~HbATOlWRYD%Ja0WcOpQ9,mcK + ~YMLuP
INFO: Decrypting encrypted password through ./decryptPasswdWithKey.sh
INFO: Decrypted password is:
123456

AES256CBC-withFixedPassphrase encryption and decryption script

Use AES256CBC-withFixedPassphrase algorithm to encrypt passwords

The script name is encryptAES256.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Encrypting plain text password through $0"

passwd_plaintext=$1

passwd_encrypted=`echo -n $passwd_plaintext | openssl enc -e -aes-256-cbc -a -salt -k SEvjsEbM7SHmI9Ow`

echo "INFO: Encrypted password is:"

echo $passwd_encrypted

Use Base64 algorithm to decrypt password

The script name is decryptAES256.sh, and the script content is as follows:

#!/bin/bash

# ------ #
# Script developed by Xinyao Tian on 2023/08/10
# Quick developed for Network Protection Operation 2023
# ------ #

echo "INFO: Decrypting encrypted password through $0"

passwd_encrypted=$1

passwd_plaintext=`echo $passwd_encrypted | openssl aes-256-cbc -a -d -salt -k SEvjsEbM7SHmI9Ow`

echo "INFO: Decrypted password is:"

echo $passwd_plaintext

How to use

View the script in the directory:

[flinkrt@p0-tkldmp-rc01 ~]$ ls -l | grep AES
-rwxr--r-- 1 flinkrt flinkrt 373 Aug 10 16:24 decryptAES256.sh
-rwxr--r-- 1 flinkrt flinkrt 382 Aug 10 16:27 encryptAES256.sh

How to use encryption is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./encryptAES256.sh 123456
INFO: Encrypting plain text password through ./encryptAES256.sh
INFO: Encrypted password is:
U2FsdGVkX18dXFeLgjDD4hnZshk6tYr999gpzgWQ7YU=

The method of decryption is as follows:

[flinkrt@p0-tkldmp-rc01 ~]$ ./decryptAES256.sh U2FsdGVkX18dXFeLgjDD4hnZshk6tYr999gpzgWQ7YU=
INFO: Decrypting encrypted password through ./decryptAES256.sh
INFO: Decrypted password is:
123456

References

  • Using OpenSSL to encrypt messages and files on Linux