如何在 Linux 中生成/加密/解密随机密码


安全是网络世界中主要关注的问题之一。网络安全,也称为信息技术安全,专注于保护计算机、网络、程序和数据免受意外或未经授权的访问、更改或破坏。我们使用密码来保护计算机、电子邮件、云、手机、文档等等。我们都知道选择易于记忆且难以猜测的密码的基本知识。但是,如果用户想要加密或解密密码,那么 Linux 为您提供了许多选项。

本文介绍了“如何在 Linux 中生成/加密/解密随机密码”。

PWGen

PwGen 是一个随机唯一的密码生成器。要安装 PWGen,请使用以下命令:

$ sudo apt-get install pwgen

示例输出应如下所示:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
libgphoto2-port10:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386
libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386
libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386
libhx509-5-heimdal:i386 libice6:i386 libieee1284-3:i386
libjack-jackd2-0:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386
libk5crypto3:i386 libkeyutils1:i386 libkrb5-26-heimdal:i386 libkrb5-3:i386
libkrb5support0:i386 liblcms2-2:i386 libldap-2.4-2:i386 libllvm3.6:i386
libltdl7:i386 libmouse-perl libmpg123-0:i386 libnet-dropbox-api-perl
..........................................

要生成长度等于 10 个字符的随机唯一密码,请使用以下命令:

$ pwgen 5 1

示例输出应如下所示:

$ pwgen 10 1
lah6M

要生成多个字符长度为 50 的随机唯一密码,请使用以下命令:

$ pwgen 50

示例输出应如下所示:

$ pwgen 50
Meepahshoo4ee9kohvezah8fu0Ox0neogh9iechoo9thoocooM
ye7ieliiGhief6ohquahf4leithei7sugh6shu3nooy6ooz7Ae
ieng0shefaizeezoo6ila5Ecee0iu9aaquaiwie8chi1yoh4ah
iiw7Noogh1hi5iga2queepheiCahyaghiesie2yaexeifeifoo
lunu9Aic1iepah4Bei8wenee4aer1aiVaing1hoh7eiphiu1le
kooquifooFai3uK7Gai8Ix9oph8oolah0ohp2iheiChaepudie
caiTiem3ilies0nietetheevei2oatoeloh7iLooPi8Beeneey
esug8ahmait6reepheaBoo8goequ2Bu9oodoo5rahkaeweequa
nie4ahgheida2xiij6hak7ke5lu0ugh1otah7Feir8iefoDaum
noy1chep2da3eu7sheicohZaighai8aem9feec8bohmei2eemu
zu8Aec3Pei6ol2ahteexoc3goQuaisai4upaj5chepha6ahsh4
lah4aedahTee2ki6zou4uphoBoh5Ooyeixaihiu5fuoYah7Tei
ooS6ku2Ia9Eilohchubais7yoxeephohS9Eath9Pequ3aengae
Ooqui3goeko1loh6ooshaikiahi4noxaiDuiP2Ohruroh2ohba
pa3quee7Lagheengae9iR3fooHooXa0Ioth0ieMee2pe2Ain9h
maife3eiLerooh3to0eeGee6ahHo0aeT9Eef7axa7eiY4LaeP2
aech0AiWuoTheeGeno6NoWahgh0shish8quoo3Chu0uoxahmie
paihay0aip3iexaseidah1OhRoo3Eeniemulah8oG7VobeaThi
seequ2akooW6Teij2aico6aivohT3eshaeri0Gohjaigiohixu
pee8eith4Rinodeequoh2Eipowuvool7ohquu8eibai4haih1h

makepasswd

makepasswd 用于根据选择生成给定长度的随机、唯一的密码。要安装 makepasswd,请使用以下命令:

$ sudo apt-get install makepasswd

示例输出应如下所示:

Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
libgphoto2-port10:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386
libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386
libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386
libhx509-5-heimdal:i386 libice6:i386 libieee1284-3:i386
libjack-jackd2-0:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386
libk5crypto3:i386 libkeyutils1:i386 libkrb5-26-heimdal:i386 libkrb5-3:i386
libkrb5support0:i386 liblcms2-2:i386 libldap-2.4-2:i386 libllvm3.6:i386
libltdl7:i386 libmouse-perl libmpg123-0:i386 libnet-dropbox-api-perl
libogg0:i386 libopenal1:i386 liborc-0.4-0:i386 libosmesa6:i386
.........................................

要生成字符长度为 10 的随机密码(默认值为 10),请使用以下命令:

$ makepasswd

示例输出应如下所示:

$ makepasswd
c0CgufUg

要生成字符长度为 50 的随机密码,请使用以下命令:

$ makepasswd --char 40

示例输出应如下所示:

$ makepasswd --char 50
HCvvXipXmc7idBcWHt8UcjcX21xvC9P700NgCAoM

要生成 10 个 20 个字符的随机密码,请使用以下命令:

$ makepasswd --char 20 --count 10

示例输出应如下所示:

$ makepasswd --char 20 --count 10
b8aotAGE83WqpNXb6tms
XBItn0UenWhc4dNixcBD
uE5Km8J8ssHIThhSBWnS
XFcfA50Ew6iD0WxwsuKn
3e4nxDU0F3WtzLE7WNoV
G640DxC3XsrFQRgFtHho
THXKMI65qojzGCVsmje8
LmhK9ScX9uJCE2mDgEYD
p9YU7eWI4CN9cxeiLUGn
rraxYCpGUNjiFvXmNJha

要加密密码,请使用 **mkpasswd** 命令。安装 **mkpasswd** 如下所示:

$ sudo apt-get install whois

示例输出应如下所示:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386
libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386
libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl
libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl
libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl
libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386
libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386
libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386
libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386
libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386
libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386
libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386
......................................

要使用 **mkpasswd** 加密密码,请使用以下命令:

$ mkpasswd tutorialspoint

示例输出应如下所示:

$ mkpasswd tutorialspoint
V8dIs6XzLXB7c

要使用 **aes-256-cbc** 加密加密密码,请使用以下命令:

$ echo Tutorialspoint-is-a-best-online-resouce | openssl enc -aes-256-cbc -a -salt -pass pass:tutorialspoint

示例输出应如下所示:

$ echo Tutorialspoint-is-a-best-online-resouce | openssl enc -aes-256-cbc -a -salt -pass pass:tutorialspoint
U2FsdGVkX19TJ9x2yI18KoP1TzIbyCQhkrK8uShPTm3CtLaZpI4YkOQG3O8RcQnX
wymb1bWnshJXkFeyATgCQw==

要解密上述字符串,请使用以下命令:

$ echo U2FsdGVkX19TJ9x2yI18KoP1TzIbyCQhkrK8uShPTm3CtLaZpI4YkOQG3O8RcQnX | openssl enc -aes-256-cbc -a -d -salt -pass pass:tutorialspoint

示例输出应如下所示:

Tutorialspoint-is-a-best-online-resouce

恭喜!现在,您知道了“如何在 Linux 中生成/加密/解密随机密码”。我们将在下一篇文章中学习更多关于此类命令的信息。请继续关注!

更新于:2019 年 10 月 21 日

89 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告