How to Generate, Encrypt, and Decrypt Random Passwords on Linux with pwgen and OpenSSL

This guide shows how to create random passwords using pwgen, generate an RSA key with OpenSSL, then encrypt and decrypt those passwords via command‑line tools, including example commands and sample outputs, while emphasizing security best practices for key handling.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Generate, Encrypt, and Decrypt Random Passwords on Linux with pwgen and OpenSSL

Generate Random Passwords

Use pwgen to produce random strings containing uppercase, lowercase letters and digits. The following command creates 20 passwords, each 10 characters long: $ pwgen -1 -s 10 20 The output consists of one password per line, for example:

Rav0iusaPh
yoo1IeNo6G
oh d0oiThao
eit8Aeje8V
Chah5naoXa
oob0coh2Ph
Phei7oong2
EiQuai2shu
iM6ahne2ju
Ohd9ahteez
Qua4ruFohc
Jeeva5dofa
Ush5phohqu
ieNgieJo0m
Pheiphye8S
yohxie0Doh
Zei7ooThie
mohd4Aethi
kae9aiXaiP
eiZ3seeHah

Encrypt Passwords

First generate an RSA private key with OpenSSL:

$ openssl genpkey -algorithm RSA -out private.key

This creates private.key, which can be used for encryption and decryption. To encrypt a file password.txt containing the passwords, run:

$ openssl pkeyutl -in password.txt -out password.enc -encrypt -inkey private.key

The result is the encrypted file password.enc.

Decrypt Passwords

Use the same private key to decrypt:

$ openssl pkeyutl -in password.enc -out password.txt -decrypt -inkey private.key

The decrypted content is written back to password.txt.

Security Considerations

Always choose strong passwords for the generated keys and store the private key in a secure location to maintain confidentiality.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

encryptionOpenSSLdecryptionpassword generationpwgen
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.