Skip to content

Instantly share code, notes, and snippets.

@thiagosouza
Last active February 20, 2024 15:52
Show Gist options
  • Save thiagosouza/8e4c15921b4c12361235932d81df9546 to your computer and use it in GitHub Desktop.
Save thiagosouza/8e4c15921b4c12361235932d81df9546 to your computer and use it in GitHub Desktop.
[Cryptography cli nodejs] #openssl #encrypt #decrypt #cli #nodejs
openssl enc -aes-256-cbc -in /etc/cryptocurrencies.txt -out services.dat
openssl aes-256-cbc -d -in services.dat -out services.teste
#erase files with wipe
#https://www.shellhacks.com/encrypt-decrypt-file-password-openssl/
$ openssl enc -aes-256-cbc -d -in file.txt.enc -out file.txt -k PASS
# generate a x509 crt certificate
openssl req -x509 -newkey rsa:2048 -keyout ./private.pem -out ./mycert.pem -nodes -days 365
openssl req -x509 -newkey rsa:2048 -keyout ./private.key -out ./mycert.crt -nodes -days 365

How to use the crypto module by Josh Holbrook jesusabdullah on Friday, Aug 26 2011 https://docs.nodejitsu.com/articles/cryptography/how-to-use-crypto-module/

https://nodejs.org/api/crypto.html

require("crypto")
  .createHash("md5")
  .update("Man oh man do I love node!")
  .digest("hex");
require("crypto").createHmac("md5", "password")
  .update("If you love node so much why don't you marry it?")
  .digest("hex");
#!/usr/bin/env node

var crypto = require("crypto"),
    argv = require("optimist").argv;

if (argv.e && argv.password) {
    var cipher = crypto.createCipher("aes192", argv.password),
        msg = [];

    argv._.forEach( function (phrase) {
        msg.push(cipher.update(phrase, "binary", "hex"));
    });

    msg.push(cipher.final("hex"));
    console.log(msg.join(""));

} else if (argv.d && argv.password) {
    var decipher = crypto.createDecipher("aes192", argv.password),
        msg = [];

    argv._.forEach( function (phrase) {
        msg.push(decipher.update(phrase, "hex", "binary"));
    });

    msg.push(decipher.final("binary"));
    console.log(msg.join(""));   
}
./secretmsg.js -e --password="popcorn" "My treasure is buried behind Carl's Jr. on Telegraph."
6df66752b24f0886f8a6c55e56977788c2090bb657ff3bd645097f8abe11099963fb3bd9627986c60fa7e5120d8fead928cff620b37e3e79be8de519f490527a
Now, if I gave somebody the same script, my encoded message and the password, they can decode the message and find out where I buried my treasure:

./secretmsg.js -d --password="popcorn" 6df66752b24f0886f8a6c55e56977788c2090bb657ff3bd645097f8abe11099963fb3bd9627986c60fa7e5120d8fead928cff620b37e3e79be8de519f490527a
My treasure is buried behind Carl's Jr. on Telegraph.

#encrypt
openssl enc -aes256 -base64 -in secretfile.txt -out secretfile.txt.enc
#remove original file (or move into /tmp folder)
mv secretfile.txt /tmp/
#decrypt
openssl enc -d -aes256 -base64 -in secretfile.txt.enc -out secretfile.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment