How to generate Diffie Hellman key pair in Node.JS

Super easy using crypto library. You can create a file called DiffieHellmanKeyPairGeneration.js for example and paste this code inside: console.log(‘\n- — ( Diffie Hellman Key Pair Generator ) — -‘); var crypto = require(‘crypto’); var bitSize = 2048; var dh = crypto.createDiffieHellman(bitSize); dh.generateKeys(); console.log(‘Private Key:\n’ + dh.getPrivateKey(‘base64’)); console.log(‘\n’); console.log(‘Public Key:\n’ + dh.getPublicKey(‘base64’)); console.log(‘— End —‘); Then run it, wait…wait…wait…wait…wait…wait…, and you …

How to list supported Ciphers, Hashes, and Curves for Crypto in Node.JS

Not a complicated task. Here is the code: var crypto = require(‘crypto’); console.log(); console.log(‘[ SUPPORTED CIPHERS ]\n’) console.log(crypto.getCiphers()); console.log(); console.log(‘[ SUPPORTED HASHES ]\n’) console.log(crypto.getHashes()); console.log(); console.log(‘[ SUPPORTED CURVES ]\n’) console.log(crypto.getCurves()); Your output should be similar to this: E:\Codes\NodeJS\Cryptography>node listAllAlgorithms.js [ SUPPORTED CIPHERS ] [ ‘CAST-cbc’, ‘aes-128-cbc’, ‘aes-128-cbc-hmac-sha1’, ‘aes-128-cbc-hmac-sha256’, ‘aes-128-ccm’, ‘aes-128-cfb’, ‘aes-128-cfb1’, ‘aes-128-cfb8’, ‘aes-128-ctr’, ‘aes-128-ecb’, ‘aes-128-gcm’, ‘aes-128-ofb’, ‘aes-128-xts’, ‘aes-192-cbc’, ‘aes-192-ccm’, ‘aes-192-cfb’, …

How to Encrypt / Decrypt with AES in Node.JS

Easy as pie! var crypto = require(‘crypto’); var AES128 = “aes128”; var AES192 = “aes192”; var AES256 = “aes256”; var password = ‘Austin’; var plainText = ‘Texas to the bone!’; // ———————————————— var cipher = crypto.createCipher(AES128, password); var decipher = crypto.createDecipher(AES128, password); console.log(‘AES-128:’); // Encrypting with AES128 var encText = cipher.update(plainText, ‘utf8’, ‘hex’); encText += cipher.final(‘hex’); console.log(encText); // Decrypting with …

How to Encode / Decode to / from base64 in Node.JS

Today I ran into this basic problem. A quick search on google gave me the answer, I tested it, it worked, and the rest is history. Here is the code. let plainText = “Hello World!”; console.log(“Plaint text = ” + plainText); // Encoding to base64 let base64 = new Buffer(plainText).toString(‘base64’); console.log(“Encoded to base64 = ” + base64); // Decoding from …

How to Encrypt / Decrypt with AES in Java 1.8

Here I have created a class that does it in two different ways, the first one is, when you create the key, and the other is when you are given the key, valid key sizes are 128, 192, and 256 for AES algorithm. There is some steps to follow before executing the code provied, if you don’t probably this class …

How to fold / unfold all regions in Visual Studio Code

With JSON files of around 900 lines, this option is very useful to have an organized view. I highly recommend to check the links, there is a lot of things you can do with Visual Studio Code, my version is 1.10.2, and the following shortcuts are working. Ctrl+K Ctrl+0 Fold (collapse) all regions Ctrl+K Ctrl+J Unfold (uncollapse) all regions Sources …

How to generate X.509 certificate in Java 1.8

Here is how to do it, the simplest way, to generate the base64 string. No complications with KeyStores or things like that. I must say that I found this code in several places over the Internet. When I tried to run it I had some exceptions, I solved them, at the time of this writing, the following code works with …

How to generate SHA256HMAC in Node.JS

Today I had to do this for my job (hash some info) and I think is a good opportunity to share the knowledge. Here is the code, you can simply copy it and paste it in a file called SHA256Hmac.js and run it. let crypto; try {     crypto = require(‘crypto’); } catch (err) {     console.log(‘crypto support is disabled!’); …

How to convert byte array to hex string in Java 1.8

If you like cryptography you might know that sometimes you have a byte array which you wish to convert to hexadecimal string. In java, to this day, I haven’t found yet a native class that does it, I’ve spent several minutes on the Internet looking for something like the Base64 class. Turns out Apache commons has a class called Hex.java …

How to generate RSA key pair in Java 1.8

Recently I was wondering how to generate a private key in Java for a personal project. I found out how to generate the actual key pair with RSA algorithm. Here is the java code for that, a simple class that you can run to check how things work. import java.security.KeyPairGenerator; import java.security.KeyPair; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; public class …