Java AES/ECB/PKCS5Padding encryption to crypto-js decryption












0














Backend Using Below Java code for the AES Encryption.



import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.spec.SecretKeySpec;

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



/**
* This example program shows how AES encryption and decryption can be done in Java.
* Please note that secret key and encrypted text is unreadable binary and hence
* in the following program we display it in hexadecimal format of the underlying bytes.
* @author Jayson
*/
public class AESEncryption {

/**
* 1. Generate a plain text for encryption
* 2. Get a secret key (printed in hexadecimal form). In actual use this must
* by encrypted and kept safe. The same key is required for decryption.
* 3.
*/
public static void main(String args) throws Exception {


String plainText = "rajaram";
String keyText ="test123";
SecretKey secKey = getSecretEncryptionKey(keyText);
System.out.println("secKey:" + secKey);

byte cipherText = encryptText(plainText, secKey);
String decryptedText = decryptText(cipherText, secKey);

System.out.println("Original Text:>>>> " + plainText);
System.out.println("AES Key (Hex Form):>>>> "+bytesToHex(secKey.getEncoded()));
System.out.println("Encrypted Text (Hex Form):>>>> "+bytesToHex(cipherText));
System.out.println("Descrypted Text:>>>> "+decryptedText);

}



public static SecretKey getSecretEncryptionKey(String keyText) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(getKey(keyText), "AES");
return keySpec;
}


public static byte getKey(String keyStr) {
byte key = null;
try {
key = (keyStr).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
System.out.println("SHA-1 key" + key);
key = Arrays.copyOf(key, 16);
System.out.println("copyOf SHA-1 16 key" + key);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("getKey" + key);
return key;
}

/**
* Encrypts plainText in AES using the secret key
* @param plainText
* @param secKey
* @return
* @throws Exception
*/
public static byte encryptText(String plainText,SecretKey secKey) throws Exception{
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");

System.out.println("ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE);
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
System.out.println("plain Text getBytes:" + plainText.getBytes());
byte byteCipherText = aesCipher.doFinal(plainText.getBytes());
System.out.println("byte Cipher Text:" + byteCipherText);
return byteCipherText;
}

/**
* Decrypts encrypted byte array using the key used for encryption.
* @param byteCipherText
* @param secKey
* @return
* @throws Exception
*/
public static String decryptText(byte byteCipherText, SecretKey secKey) throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}

/**
* Convert a binary byte array into readable hex form
* @param hash
* @return
*/
private static String bytesToHex(byte hash) {
return DatatypeConverter.printHexBinary(hash);
}
}


Above Clas O/P:



getKey[B@6d06d69c
secKey:javax.crypto.spec.SecretKeySpec@fffe8c0a
ENCRYPT_MODE:1
plain Text getBytes:[B@34340fab
byte Cipher Text:[B@2aafb23c
Original Text:>>>> rajaram
AES Key (Hex Form):>>>> 7288EDD0FC3FFCBE93A0CF06E3568E28
Encrypted Text (Hex Form):>>>> 8E441411D9890BED64BD7931DE3230C3
Descrypted Text:>>>> rajaram


But I am unable to decryption using crypto-js.



Crypto-js code example for the frondend:



var bytes = CryptoJS.AES.decrypt("8E441411D9890BED64BD7931DE3230C3", "test123", { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

var plaintext = bytes.toString(CryptoJS.enc.Utf8);
alert(plaintext);


JS fiddle link : http://jsfiddle.net/baxfk6tw/



Anyone, crypto-js how to add SHA-1 16 key with AES/ECB/PKCS5Padding, Please suggest me how to resolve this issue.










share|improve this question
























  • I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
    – Federico klez Culloca
    Nov 20 at 10:43












  • Java class using for the AES encryption, CryptoJS using for decryption!.
    – user10679480
    Nov 20 at 10:48










  • Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
    – Federico klez Culloca
    Nov 20 at 10:49










  • I am getting empty value while decrypting
    – user10679480
    Nov 20 at 10:50










  • @rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
    – Raja Rama Mohan Thavalam
    Nov 21 at 6:04


















0














Backend Using Below Java code for the AES Encryption.



import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.spec.SecretKeySpec;

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



/**
* This example program shows how AES encryption and decryption can be done in Java.
* Please note that secret key and encrypted text is unreadable binary and hence
* in the following program we display it in hexadecimal format of the underlying bytes.
* @author Jayson
*/
public class AESEncryption {

/**
* 1. Generate a plain text for encryption
* 2. Get a secret key (printed in hexadecimal form). In actual use this must
* by encrypted and kept safe. The same key is required for decryption.
* 3.
*/
public static void main(String args) throws Exception {


String plainText = "rajaram";
String keyText ="test123";
SecretKey secKey = getSecretEncryptionKey(keyText);
System.out.println("secKey:" + secKey);

byte cipherText = encryptText(plainText, secKey);
String decryptedText = decryptText(cipherText, secKey);

System.out.println("Original Text:>>>> " + plainText);
System.out.println("AES Key (Hex Form):>>>> "+bytesToHex(secKey.getEncoded()));
System.out.println("Encrypted Text (Hex Form):>>>> "+bytesToHex(cipherText));
System.out.println("Descrypted Text:>>>> "+decryptedText);

}



public static SecretKey getSecretEncryptionKey(String keyText) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(getKey(keyText), "AES");
return keySpec;
}


public static byte getKey(String keyStr) {
byte key = null;
try {
key = (keyStr).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
System.out.println("SHA-1 key" + key);
key = Arrays.copyOf(key, 16);
System.out.println("copyOf SHA-1 16 key" + key);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("getKey" + key);
return key;
}

/**
* Encrypts plainText in AES using the secret key
* @param plainText
* @param secKey
* @return
* @throws Exception
*/
public static byte encryptText(String plainText,SecretKey secKey) throws Exception{
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");

System.out.println("ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE);
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
System.out.println("plain Text getBytes:" + plainText.getBytes());
byte byteCipherText = aesCipher.doFinal(plainText.getBytes());
System.out.println("byte Cipher Text:" + byteCipherText);
return byteCipherText;
}

/**
* Decrypts encrypted byte array using the key used for encryption.
* @param byteCipherText
* @param secKey
* @return
* @throws Exception
*/
public static String decryptText(byte byteCipherText, SecretKey secKey) throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}

/**
* Convert a binary byte array into readable hex form
* @param hash
* @return
*/
private static String bytesToHex(byte hash) {
return DatatypeConverter.printHexBinary(hash);
}
}


Above Clas O/P:



getKey[B@6d06d69c
secKey:javax.crypto.spec.SecretKeySpec@fffe8c0a
ENCRYPT_MODE:1
plain Text getBytes:[B@34340fab
byte Cipher Text:[B@2aafb23c
Original Text:>>>> rajaram
AES Key (Hex Form):>>>> 7288EDD0FC3FFCBE93A0CF06E3568E28
Encrypted Text (Hex Form):>>>> 8E441411D9890BED64BD7931DE3230C3
Descrypted Text:>>>> rajaram


But I am unable to decryption using crypto-js.



Crypto-js code example for the frondend:



var bytes = CryptoJS.AES.decrypt("8E441411D9890BED64BD7931DE3230C3", "test123", { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

var plaintext = bytes.toString(CryptoJS.enc.Utf8);
alert(plaintext);


JS fiddle link : http://jsfiddle.net/baxfk6tw/



Anyone, crypto-js how to add SHA-1 16 key with AES/ECB/PKCS5Padding, Please suggest me how to resolve this issue.










share|improve this question
























  • I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
    – Federico klez Culloca
    Nov 20 at 10:43












  • Java class using for the AES encryption, CryptoJS using for decryption!.
    – user10679480
    Nov 20 at 10:48










  • Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
    – Federico klez Culloca
    Nov 20 at 10:49










  • I am getting empty value while decrypting
    – user10679480
    Nov 20 at 10:50










  • @rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
    – Raja Rama Mohan Thavalam
    Nov 21 at 6:04
















0












0








0


0





Backend Using Below Java code for the AES Encryption.



import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.spec.SecretKeySpec;

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



/**
* This example program shows how AES encryption and decryption can be done in Java.
* Please note that secret key and encrypted text is unreadable binary and hence
* in the following program we display it in hexadecimal format of the underlying bytes.
* @author Jayson
*/
public class AESEncryption {

/**
* 1. Generate a plain text for encryption
* 2. Get a secret key (printed in hexadecimal form). In actual use this must
* by encrypted and kept safe. The same key is required for decryption.
* 3.
*/
public static void main(String args) throws Exception {


String plainText = "rajaram";
String keyText ="test123";
SecretKey secKey = getSecretEncryptionKey(keyText);
System.out.println("secKey:" + secKey);

byte cipherText = encryptText(plainText, secKey);
String decryptedText = decryptText(cipherText, secKey);

System.out.println("Original Text:>>>> " + plainText);
System.out.println("AES Key (Hex Form):>>>> "+bytesToHex(secKey.getEncoded()));
System.out.println("Encrypted Text (Hex Form):>>>> "+bytesToHex(cipherText));
System.out.println("Descrypted Text:>>>> "+decryptedText);

}



public static SecretKey getSecretEncryptionKey(String keyText) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(getKey(keyText), "AES");
return keySpec;
}


public static byte getKey(String keyStr) {
byte key = null;
try {
key = (keyStr).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
System.out.println("SHA-1 key" + key);
key = Arrays.copyOf(key, 16);
System.out.println("copyOf SHA-1 16 key" + key);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("getKey" + key);
return key;
}

/**
* Encrypts plainText in AES using the secret key
* @param plainText
* @param secKey
* @return
* @throws Exception
*/
public static byte encryptText(String plainText,SecretKey secKey) throws Exception{
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");

System.out.println("ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE);
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
System.out.println("plain Text getBytes:" + plainText.getBytes());
byte byteCipherText = aesCipher.doFinal(plainText.getBytes());
System.out.println("byte Cipher Text:" + byteCipherText);
return byteCipherText;
}

/**
* Decrypts encrypted byte array using the key used for encryption.
* @param byteCipherText
* @param secKey
* @return
* @throws Exception
*/
public static String decryptText(byte byteCipherText, SecretKey secKey) throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}

/**
* Convert a binary byte array into readable hex form
* @param hash
* @return
*/
private static String bytesToHex(byte hash) {
return DatatypeConverter.printHexBinary(hash);
}
}


Above Clas O/P:



getKey[B@6d06d69c
secKey:javax.crypto.spec.SecretKeySpec@fffe8c0a
ENCRYPT_MODE:1
plain Text getBytes:[B@34340fab
byte Cipher Text:[B@2aafb23c
Original Text:>>>> rajaram
AES Key (Hex Form):>>>> 7288EDD0FC3FFCBE93A0CF06E3568E28
Encrypted Text (Hex Form):>>>> 8E441411D9890BED64BD7931DE3230C3
Descrypted Text:>>>> rajaram


But I am unable to decryption using crypto-js.



Crypto-js code example for the frondend:



var bytes = CryptoJS.AES.decrypt("8E441411D9890BED64BD7931DE3230C3", "test123", { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

var plaintext = bytes.toString(CryptoJS.enc.Utf8);
alert(plaintext);


JS fiddle link : http://jsfiddle.net/baxfk6tw/



Anyone, crypto-js how to add SHA-1 16 key with AES/ECB/PKCS5Padding, Please suggest me how to resolve this issue.










share|improve this question















Backend Using Below Java code for the AES Encryption.



import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.spec.SecretKeySpec;

import java.security.MessageDigest;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



/**
* This example program shows how AES encryption and decryption can be done in Java.
* Please note that secret key and encrypted text is unreadable binary and hence
* in the following program we display it in hexadecimal format of the underlying bytes.
* @author Jayson
*/
public class AESEncryption {

/**
* 1. Generate a plain text for encryption
* 2. Get a secret key (printed in hexadecimal form). In actual use this must
* by encrypted and kept safe. The same key is required for decryption.
* 3.
*/
public static void main(String args) throws Exception {


String plainText = "rajaram";
String keyText ="test123";
SecretKey secKey = getSecretEncryptionKey(keyText);
System.out.println("secKey:" + secKey);

byte cipherText = encryptText(plainText, secKey);
String decryptedText = decryptText(cipherText, secKey);

System.out.println("Original Text:>>>> " + plainText);
System.out.println("AES Key (Hex Form):>>>> "+bytesToHex(secKey.getEncoded()));
System.out.println("Encrypted Text (Hex Form):>>>> "+bytesToHex(cipherText));
System.out.println("Descrypted Text:>>>> "+decryptedText);

}



public static SecretKey getSecretEncryptionKey(String keyText) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(getKey(keyText), "AES");
return keySpec;
}


public static byte getKey(String keyStr) {
byte key = null;
try {
key = (keyStr).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
System.out.println("SHA-1 key" + key);
key = Arrays.copyOf(key, 16);
System.out.println("copyOf SHA-1 16 key" + key);
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("getKey" + key);
return key;
}

/**
* Encrypts plainText in AES using the secret key
* @param plainText
* @param secKey
* @return
* @throws Exception
*/
public static byte encryptText(String plainText,SecretKey secKey) throws Exception{
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");

System.out.println("ENCRYPT_MODE:" + Cipher.ENCRYPT_MODE);
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
System.out.println("plain Text getBytes:" + plainText.getBytes());
byte byteCipherText = aesCipher.doFinal(plainText.getBytes());
System.out.println("byte Cipher Text:" + byteCipherText);
return byteCipherText;
}

/**
* Decrypts encrypted byte array using the key used for encryption.
* @param byteCipherText
* @param secKey
* @return
* @throws Exception
*/
public static String decryptText(byte byteCipherText, SecretKey secKey) throws Exception {
// AES defaults to AES/ECB/PKCS5Padding in Java 7
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte bytePlainText = aesCipher.doFinal(byteCipherText);
return new String(bytePlainText);
}

/**
* Convert a binary byte array into readable hex form
* @param hash
* @return
*/
private static String bytesToHex(byte hash) {
return DatatypeConverter.printHexBinary(hash);
}
}


Above Clas O/P:



getKey[B@6d06d69c
secKey:javax.crypto.spec.SecretKeySpec@fffe8c0a
ENCRYPT_MODE:1
plain Text getBytes:[B@34340fab
byte Cipher Text:[B@2aafb23c
Original Text:>>>> rajaram
AES Key (Hex Form):>>>> 7288EDD0FC3FFCBE93A0CF06E3568E28
Encrypted Text (Hex Form):>>>> 8E441411D9890BED64BD7931DE3230C3
Descrypted Text:>>>> rajaram


But I am unable to decryption using crypto-js.



Crypto-js code example for the frondend:



var bytes = CryptoJS.AES.decrypt("8E441411D9890BED64BD7931DE3230C3", "test123", { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });

var plaintext = bytes.toString(CryptoJS.enc.Utf8);
alert(plaintext);


JS fiddle link : http://jsfiddle.net/baxfk6tw/



Anyone, crypto-js how to add SHA-1 16 key with AES/ECB/PKCS5Padding, Please suggest me how to resolve this issue.







javascript java cryptography aes cryptojs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 10:43

























asked Nov 20 at 10:41









user10679480

42




42












  • I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
    – Federico klez Culloca
    Nov 20 at 10:43












  • Java class using for the AES encryption, CryptoJS using for decryption!.
    – user10679480
    Nov 20 at 10:48










  • Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
    – Federico klez Culloca
    Nov 20 at 10:49










  • I am getting empty value while decrypting
    – user10679480
    Nov 20 at 10:50










  • @rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
    – Raja Rama Mohan Thavalam
    Nov 21 at 6:04




















  • I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
    – Federico klez Culloca
    Nov 20 at 10:43












  • Java class using for the AES encryption, CryptoJS using for decryption!.
    – user10679480
    Nov 20 at 10:48










  • Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
    – Federico klez Culloca
    Nov 20 at 10:49










  • I am getting empty value while decrypting
    – user10679480
    Nov 20 at 10:50










  • @rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
    – Raja Rama Mohan Thavalam
    Nov 21 at 6:04


















I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
– Federico klez Culloca
Nov 20 at 10:43






I'm not sure what the link is between the Java class and the JavaScript snippet. Can you please explain?
– Federico klez Culloca
Nov 20 at 10:43














Java class using for the AES encryption, CryptoJS using for decryption!.
– user10679480
Nov 20 at 10:48




Java class using for the AES encryption, CryptoJS using for decryption!.
– user10679480
Nov 20 at 10:48












Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
– Federico klez Culloca
Nov 20 at 10:49




Ok, then what does "But I am unable to decryption using crypto-js." mean? You get an error? Wrong output? Please, include some more detail in your question.
– Federico klez Culloca
Nov 20 at 10:49












I am getting empty value while decrypting
– user10679480
Nov 20 at 10:50




I am getting empty value while decrypting
– user10679480
Nov 20 at 10:50












@rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
– Raja Rama Mohan Thavalam
Nov 21 at 6:04






@rustyx can u please check and update the fiddle jsfiddle.net/rajaramtt/rbh0wkej
– Raja Rama Mohan Thavalam
Nov 21 at 6:04














1 Answer
1






active

oldest

votes


















1














You Java version does the following:




  • Hash the password once with SHA1, then take the first 16 bytes as the key.

  • Encrypt plaintext UTF-8 bytes with AES-128, ECB mode, PKCS7 padding.

  • Convert ciphertext to Hex.


So to decrypt it with CryptoJS you need to repeat the same steps:






var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

var decrypted = CryptoJS.AES.decrypt({
ciphertext: ciphertext
}, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});

var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

$('#result').text(plaintext);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

<h2 id="result">







For reference, encryption using CryptoJS might look like this:






var plaintext = "rajaram";
var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});

var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

$('#encrypted').text(ciphertext);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

<h2 id="encrypted">








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391217%2fjava-aes-ecb-pkcs5padding-encryption-to-crypto-js-decryption%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You Java version does the following:




    • Hash the password once with SHA1, then take the first 16 bytes as the key.

    • Encrypt plaintext UTF-8 bytes with AES-128, ECB mode, PKCS7 padding.

    • Convert ciphertext to Hex.


    So to decrypt it with CryptoJS you need to repeat the same steps:






    var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
    var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
    var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

    var decrypted = CryptoJS.AES.decrypt({
    ciphertext: ciphertext
    }, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
    });

    var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

    $('#result').text(plaintext);

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

    <h2 id="result">







    For reference, encryption using CryptoJS might look like this:






    var plaintext = "rajaram";
    var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
    var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

    var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
    });

    var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

    $('#encrypted').text(ciphertext);

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

    <h2 id="encrypted">








    share|improve this answer




























      1














      You Java version does the following:




      • Hash the password once with SHA1, then take the first 16 bytes as the key.

      • Encrypt plaintext UTF-8 bytes with AES-128, ECB mode, PKCS7 padding.

      • Convert ciphertext to Hex.


      So to decrypt it with CryptoJS you need to repeat the same steps:






      var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
      var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
      var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

      var decrypted = CryptoJS.AES.decrypt({
      ciphertext: ciphertext
      }, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
      });

      var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

      $('#result').text(plaintext);

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

      <h2 id="result">







      For reference, encryption using CryptoJS might look like this:






      var plaintext = "rajaram";
      var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
      var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

      var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
      });

      var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

      $('#encrypted').text(ciphertext);

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

      <h2 id="encrypted">








      share|improve this answer


























        1












        1








        1






        You Java version does the following:




        • Hash the password once with SHA1, then take the first 16 bytes as the key.

        • Encrypt plaintext UTF-8 bytes with AES-128, ECB mode, PKCS7 padding.

        • Convert ciphertext to Hex.


        So to decrypt it with CryptoJS you need to repeat the same steps:






        var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var decrypted = CryptoJS.AES.decrypt({
        ciphertext: ciphertext
        }, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

        $('#result').text(plaintext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="result">







        For reference, encryption using CryptoJS might look like this:






        var plaintext = "rajaram";
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

        $('#encrypted').text(ciphertext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="encrypted">








        share|improve this answer














        You Java version does the following:




        • Hash the password once with SHA1, then take the first 16 bytes as the key.

        • Encrypt plaintext UTF-8 bytes with AES-128, ECB mode, PKCS7 padding.

        • Convert ciphertext to Hex.


        So to decrypt it with CryptoJS you need to repeat the same steps:






        var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var decrypted = CryptoJS.AES.decrypt({
        ciphertext: ciphertext
        }, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

        $('#result').text(plaintext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="result">







        For reference, encryption using CryptoJS might look like this:






        var plaintext = "rajaram";
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

        $('#encrypted').text(ciphertext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="encrypted">








        var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var decrypted = CryptoJS.AES.decrypt({
        ciphertext: ciphertext
        }, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

        $('#result').text(plaintext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="result">





        var ciphertext = CryptoJS.enc.Hex.parse("8E441411D9890BED64BD7931DE3230C3");
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var decrypted = CryptoJS.AES.decrypt({
        ciphertext: ciphertext
        }, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var plaintext = decrypted.toString(CryptoJS.enc.Utf8);

        $('#result').text(plaintext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="result">





        var plaintext = "rajaram";
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

        $('#encrypted').text(ciphertext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="encrypted">





        var plaintext = "rajaram";
        var pwhash = CryptoJS.SHA1(CryptoJS.enc.Utf8.parse("test123"));
        var key = CryptoJS.enc.Hex.parse(pwhash.toString(CryptoJS.enc.Hex).substr(0, 32));

        var encrypted = CryptoJS.AES.encrypt(plaintext, key, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
        });

        var ciphertext = encrypted.ciphertext.toString(CryptoJS.enc.Hex);

        $('#encrypted').text(ciphertext);

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js"></script>

        <h2 id="encrypted">






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 12:07

























        answered Nov 21 at 9:28









        rustyx

        28.5k696136




        28.5k696136






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391217%2fjava-aes-ecb-pkcs5padding-encryption-to-crypto-js-decryption%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin