Created
July 14, 2012 14:31
-
-
Save kennydude/3111592 to your computer and use it in GitHub Desktop.
Encrypt from Java and decrypt on Node.js
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
// Encrypt where jo is input, and query is output and ENCRPYTION_KEy is key | |
byte[] input = jo.toString().getBytes("utf-8"); | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
byte[] thedigest = md.digest(ENCRYPTION_KEY.getBytes("UTF-8")); | |
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding"); | |
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, skc); | |
byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; | |
int ctLength = cipher.update(input, 0, input.length, cipherText, 0); | |
ctLength += cipher.doFinal(cipherText, ctLength); | |
String query = Base64.encodeToString(cipherText, Base64.DEFAULT); |
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
var decipher = crypto.createDecipher('aes-128-ecb', encryption_key); | |
chunks = [] | |
chunks.push( decipher.update( new Buffer(fullBuffer, "base64").toString("binary")) ); | |
chunks.push( decipher.final('binary') ); | |
var txt = chunks.join(""); | |
txt = new Buffer(txt, "binary").toString("utf-8"); | |
// where encryption_key = key, fullBuffer is the input and txt is output |
Java Encryption Code(https://repl.it/@ShehrazKhan/Encrypt-in-Java-Decrypt-in-Nodejs)
public static String encrypt(String Data, String secret) throws Exception {
Key key = generateKey(secret);
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = Base64.getEncoder().encodeToString(encVal);
return encryptedValue;
}
Node Js(https://codesandbox.io/s/decrypt-aes-in-node-encrypted-in-java-vph6w)
var encryptedBase64Key = "aXRzaG91bGRiZTE2Y2hhcg==";
var parsedBase64Key = CryptoJS.enc.Base64.parse(encryptedBase64Key);
var encryptedCipherText = "wbsl6Tegfc7mexCHUYPS+dON3BWfgU+tD1nkiHiZ4LyrHlUXlWsfeInWMDaZO39z"; // or encryptedData;
var decryptedData = CryptoJS.AES.decrypt(
encryptedCipherText,
parsedBase64Key,
{
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}
);
// console.log( “DecryptedData = “ + decryptedData );
// this is the decrypted data as a string
var decryptedText = decryptedData.toString(CryptoJS.enc.Utf8);
setDecryptedCipherText(decryptedText);
console.log("DecryptedText = " + decryptedText);
I've this error
Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
@monossido use this one instead
var decipher = crypto.createDecipher('aes-128-ecb', key); return decipher.update(encrypted, 'base64', 'utf8') + decipher. Final('utf8');
I am getting this error
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any example of encrypting at Node and decrypting at .Net or Java using BouncyCastle??