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.
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
eiZ3seeHahEncrypt Passwords
First generate an RSA private key with OpenSSL:
$ openssl genpkey -algorithm RSA -out private.keyThis 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.keyThe 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.keyThe 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
