Last active
October 22, 2024 05:01
-
-
Save luizwbr/07a0d46ff0e1caca3d3173e30fde4b4f to your computer and use it in GitHub Desktop.
Encrypt and Decrypt using 128 for Nodejs or Javascript that actually works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// None of the tutorals could help me, so I adapted a few codes. | |
// What I really want is make a full duplex way between NodeJS and Java, using encrypt/decrypt with AES 128 bit | |
// 1 - you must have to generate the key | |
openssl enc -aes-128-cbc -k secret -P -md sha1 | |
// key examples: | |
// DCDD74627CD60252E35DFBA91A4556AA | |
// 2CB24CFDB3F2520A5809EB4851168162 | |
// 468CA14CA44C82B8264F61D42E0E9FA1 | |
// 2 - NodejS | |
const crypto = require('crypto'); | |
const INITIALIZATION_VECTOR = '0000000000000000'; | |
class Crypt { | |
static decrypt128(data, key) { | |
const cipher = crypto.createDecipheriv('aes-128-cbc', Buffer.from(key, 'hex'), Buffer.from(INITIALIZATION_VECTOR)); | |
return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8'); | |
} | |
static encrypt128(data, key) { | |
const cipher = crypto.createCipheriv("aes-128-cbc", Buffer.from(key, "hex"), Buffer.from(INITIALIZATION_VECTOR)); | |
return cipher.update(data, "utf8", "hex") + cipher.final("hex"); | |
}; | |
} | |
const key = "YOURKEY"; | |
const data = 'Iguassu Falls is amazing'; | |
const cipher = Crypt.encrypt128(data, key); | |
const decipher = Crypt.decrypt128(cipher, key); | |
console.log(cipher); | |
console.log(decipher); | |
// 3 - Java | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.xml.bind.DatatypeConverter; | |
public class AESEncryption { | |
private static byte[] iv = "0000000000000000".getBytes(); | |
public static String encrypt128(String content, String key) throws Exception { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"), new IvParameterSpec(iv)); | |
byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8")); | |
return DatatypeConverter.printHexBinary(encrypted); | |
} | |
public static String decrypt128(String encrypted, String key) throws Exception { | |
Cipher dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
dcipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(DatatypeConverter.parseHexBinary(key), "AES"), new IvParameterSpec(iv)); | |
byte[] clearbyte = dcipher.doFinal(DatatypeConverter | |
.parseHexBinary(encrypted)); | |
return new String(clearbyte); | |
} | |
public static void main(String[] args) throws Exception { | |
String data = "Iguassu Falls is amazing"; | |
String key = "YOURKEY"; | |
String encriptedText = AESEncryption.encrypt128(data, key); | |
System.out.println(encriptedText); | |
System.out.println(AESEncryption.decrypt128(encriptedText, key)); | |
} | |
} |
Hi,
I have one question. In javascript part the encodeKey value is never used.same comment
Yes, you are right. I fixed the code and updated a litle bit .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
same comment