使用 OpenSSL 工具撰写 Bash 脚本进行密码明文的加密与解密

使用 OpenSSL 工具进行密码明文的加密与解密

Written By: Xinyao Tian

简介

本文档描述了使用 OpenSSL 工具在 Bash 脚本中对密码进行加密和解密的简单方式。

BASE64 的加密与解密脚本

使用 Base64 算法进行密码的加密

脚本名称为 encryptPasswd.sh, 脚本内容如下:

bash 复制代码
#!/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

使用 Base64 算法进行密码的解密

脚本名称为 decryptPasswd.sh, 脚本内容如下:

bash 复制代码
#!/bin/bash
# Script developed by Xinyao Tian on 2023/08/10

echo "INFO: Decrypting encryped password through $0"

passwd_encrypted=$1

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

echo "INFO: Decrypted password is:"

echo $passwd_plaintext

使用方法

检视目录中的脚本:

[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

加密使用方法如下:

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

解密使用方法如下:

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

BASE64-withPassphrase 的加密与解密脚本

使用 BASE64-withPassphrase 算法进行密码的加密

脚本名称为 encryptPasswdWithKey.sh, 脚本内容如下:

bash 复制代码
#!/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

使用 Base64 算法进行密码的解密

脚本名称为 decryptPasswdWithKey.sh, 脚本内容如下:

bash 复制代码
#!/bin/bash

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

echo "INFO: Decrypting encryped 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

使用方法

检视目录中的脚本:

[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

加密使用方法如下:

[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

解密使用方法如下:

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

BASE64-withFixedPassphrase 的加密与解密脚本

使用 BASE64-withFixedPassphrase 算法进行密码的加密

脚本名称为 encryptPasswdWithFixedKey.sh, 脚本内容如下:

bash 复制代码
#!/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

使用 Base64 算法进行密码的解密

脚本名称为 decryptPasswdWithFixedKey.sh, 脚本内容如下:

bash 复制代码
#!/bin/bash

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

echo "INFO: Decrypting encryped 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

使用方法

检视目录中的脚本:

[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

加密使用方法如下:

[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

解密使用方法如下:

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

AES256CBC-withFixedPassphrase 的加密与解密脚本

使用 AES256CBC-withFixedPassphrase 算法进行密码的加密

脚本名称为 encryptAES256.sh, 脚本内容如下:

bash 复制代码
#!/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

使用 Base64 算法进行密码的解密

脚本名称为 decryptAES256.sh, 脚本内容如下:

bash 复制代码
#!/bin/bash

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

echo "INFO: Decrypting encryped 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

使用方法

检视目录中的脚本:

[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

加密使用方法如下:

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

解密使用方法如下:

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

References

相关推荐
涵信15 小时前
Windows11 安装 Ubuntu-20.04,同时安装配置 zsh shell,配置 git 别名(alias),大大提高开发效率
linux·git·ubuntu·bash
云计算DevOps-韩老师19 小时前
【网络云计算】2024第52周-每日【2024/12/23】小测-理论&实操
linux·运维·服务器·开发语言·bash
qq_433618441 天前
shell 编程(二)
开发语言·bash·shell
weixin_437398211 天前
Linux扩展——shell编程
linux·运维·服务器·bash
yylの博客2 天前
Windows通过git-bash安装zsh
windows·git·bash·zsh
AI小白龙4 天前
【bash】linux中打包某个可执行文件及其依赖文件
linux·chrome·bash
YRr YRr5 天前
详解ROS环境配置:setup.bash 文件的功能与操作
开发语言·bash
云计算DevOps-韩老师5 天前
【网络云计算】2024第51周-每日【2024/12/18】小测-理论-写10个关于bash运算符和表达式的例子-解析
开发语言·bash
云计算DevOps-韩老师7 天前
【网络云计算】2024第50周-每日【2024/12/13】小测-理论-写10个Bash Shell脚本-解析
运维·网络·云计算·vim·bash·负载均衡·perl
云计算DevOps-韩老师7 天前
【网络云计算】2024第50周-每日【2024/12/14】小测-理论和实操-写5个Bash Shell脚本的if语句和交换机路由器的知识
linux·运维·服务器·网络·kubernetes·云计算·bash