I have been working on cryptoJs for a week now but the issue I am facing is with Javascript. I am a newbie in Javascript and I really need your help.
I have the following cryptojs based java encryption/decryption codes which works perfectly fine but I am trying to have the Javascript equivalent.
I was getting different encryption.
in java I got: 2BECE44E5375C690FE5785CA9C4814868D620F600AFFA14584B2B7E336742AEF9D52D4102962AD81E9A1267ED48A466B2B739E71FB3433CE9F4ED7661BFA3504C7BA70583E2BC315A1EEE89167F06309D6B3D1CF69DC1028F1396E58150483BF
While in javascript I got: 2a478540c889da6bede186c21c7133d1
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.security.sasl.SaslException;
import javax.security.*;
public class AESCrypter {
private String iv = "9%Ysr^xyBTrPr!$Y";
private String secretkey = "GnxUFjKw9(bk7Ay$";
private IvParameterSpec ivspec;
private SecretKeySpec keyspec;
private Cipher cipher;
public static void main(String args[]) throws UnsupportedEncodingException, Exception {
AESCrypter crypta = new AESCrypter();
//String toenc = "1";
String toenc = " “srcAccount”:\"0011295527\",\r\n" +
"“requestId\":\"12345678901234567890\",\r\n" +
"“pin”:\"3019\"";
String enc = "";
String dec = "";
// encrypt the value
//enc = crypta.encrypt(toenc);
enc = crypta.encrypt(toenc);
// encrypted value print out
System.out.println(" ENCED : " + enc);
// decrypt the value
dec = crypta.decrypt(enc);
// decrypted value print out
System.out.println(" DECED : " + dec);
}
public AESCrypter(String keyz, String ivStr) {
ivspec = new IvParameterSpec(ivStr.getBytes());
keyspec = new SecretKeySpec(keyz.getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
public AESCrypter()
{
ivspec = new IvParameterSpec(iv.getBytes());
keyspec = new SecretKeySpec(secretkey.getBytes(), "AES");
System.out.println("this ivspec = " + ivspec);
try
{
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (NoSuchPaddingException e)
{
e.printStackTrace();
}}
public String encrypt(String text) throws SecurityException {
System.out.println("text = " + text);
if (text == null || text.length() == 0) {
throw new SecurityException("Empty string");
}
byte[] encrypted = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
encrypted = cipher.doFinal(text.getBytes("UTF-8"));
} catch (Exception e) {
throw new SecurityException("[encrypt] " + e.getMessage());
}
return bytesToHex(encrypted);
}
public String decrypt(String code) throws SecurityException, UnsupportedEncodingException {
if (code == null || code.length() == 0) {
throw new SecurityException("Empty string");
}
byte[] decrypted = null;
try {
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
decrypted = cipher.doFinal(hexToBytes(code));
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException("[decrypt] " + e.getMessage());
}
return new String(decrypted, "UTF-8");
}
public static String bytesToHex(byte[] data)
{
if (data==null) {
return null;
}
int len = data.length;
String str = "";
for (int i=0; i<len; i++)
{
if ((data[i]&0xFF)<16) {
str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
}
else {
str = str + java.lang.Integer.toHexString(data[i]&0xFF);
}}return str;
}
public static byte[] hexToBytes(String str) {
if (str == null) {
return null;
} else if (str.length() < 2) {
return null;
} else {
int len = str.length() / 2;
byte[] buffer = new byte[len];
for (int i = 0; i < len; i++) {
buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
}
return buffer;
}
}
}
Trying to solve it in Javascript I did the following:
let iv = "tzsee5eWyBlpTHSn";
let secret_key = "IhrhcWT@HsH!l6#M";
let body = {"srcAccount" : user_login,
"requestId" : "12345678901234567890",
"pin" : user_password};
body = CryptoJS.enc.Utf8.parse(body);
secret_key = CryptoJS.enc.Latin1.parse(secret_key);
iv = CryptoJS.enc.Latin1.parse(iv);
var encrypted = CryptoJS.AES.encrypt(body, secret_key, { iv: iv,
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
});
var encrypted = CryptoJS.enc.Hex.parse(iencrypted.ciphertext.toString());
console.log(" Encrypted " +encrypted);
Could someone please point me to the right direction.
CryptoJS.pad.NoPadding? That's wrong since you're using PKCS#5 (=PKCS#7) padding. Also use the same IV and key for both.