如何在Java中获得与JavaScript中的encryptJs相等的功能

huangapple go评论74阅读模式
英文:

How to get java equivalent of encryptJs in javascript

问题

我已经在cryptoJs上工作了一个星期,但我现在遇到的问题与Javascript有关。我是Javascript的新手,真的需要你的帮助。

我有以下基于cryptojs的Java加密/解密代码,它在Java中运行得非常好,但我正在尝试找到相应的Javascript代码。

在Java中,我得到了不同的加密结果。

在Java中,我得到了: 2BECE44E5375C690FE5785CA9C4814868D620F600AFFA14584B2B7E336742AEF9D52D4102962AD81E9A1267ED48A466B2B739E71FB3433CE9F4ED7661BFA3504C7BA70583E2BC315A1EEE89167F06309D6B3D1CF69DC1028F1396E58150483BF

而在Javascript中,我得到了: 2a478540c889da6bede186c21c7133d1

以下是我在Javascript中的尝试:

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(encrypted.ciphertext.toString());
console.log(" Encrypted  " + encrypted);

请问是否可以指导我朝着正确的方向前进。

英文:

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.

答案1

得分: 2

因为您只要求代码部分的翻译,我会为您提供已经翻译好的代码部分,如下所示:

Java 代码:

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

public class Main {
    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 {
        Main crypta = new Main();
        String toenc = "The quick brown fox jumps over the lazy dog";
        String enc = "";
        String dec = "";
        enc = crypta.encrypt(toenc);
        System.out.println(" ENCED : " + enc);
        dec = crypta.decrypt(enc);
        System.out.println(" DECED : " + dec);
    }

    // ... (其余部分省略,与原文一致)
}

JavaScript 代码:

let iv = '9%Ysr^xyBTrPr!$Y';
let secret_key = 'GnxUFjKw9(bk7Ay$';
let plaintext = 'The quick brown fox jumps over the lazy dog';
console.log('plaintext: ', plaintext);

plaintext = CryptoJS.enc.Utf8.parse(plaintext);
secret_key = CryptoJS.enc.Utf8.parse(secret_key);
iv = CryptoJS.enc.Utf8.parse(iv);

var ciphertext = CryptoJS.AES.encrypt(plaintext, secret_key, {
    iv: iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
});

console.log('ciphertext: ', ciphertext.ciphertext.toString());

var decrypted = CryptoJS.AES.decrypt(ciphertext, secret_key,{
    iv: iv,
    padding: CryptoJS.pad.Pkcs7,
    mode: CryptoJS.mode.CBC,
});

console.log('decrypted (hex): ', decrypted.toString());
console.log('decrypted (str): ', CryptoJS.enc.Utf8.stringify(decrypted).toString());

请注意,以上是已经翻译好的代码部分,与原文保持一致。如果您有任何其他问题或需求,请随时提问。

英文:

As you are using different key and iv values in Java and Javascript it is ovious that you will different results
in Java and Javascript, as you already were told from @Artjom B.

As well your "message to encrypt" ("toenc" or "body") differs as well and won't get the same result.

I setup a simplified Java program and adjusted some missing charset encodings. For simplicity I used just a simple string to test the en- and decryption on both sides - I leave it up to you to find an identical solution for your "message to encrypt" on Java and Javascript.

The Javascript program is using CryptoJS for encryption and gets the same input values for key, iv and plaintext.

Both programs give the same encrypted value in hex:

525106e181b2ad0b507439f54d690dfaa6e22ae2c96cde79c532a2b868d5e824637b91cc59854592343c26c39f83f452

Please note: both programs do not have any exception handling and are for educational purpose only.

For a live test I setup two "fiddles" where you can test the programs with your own values:

Java: https://paiza.io/projects/e/tMzKTcUrBFCgvrTKf4hVow
Javascript: https://playcode.io/672028/

Java code:

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

public class Main {
    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 {
        Main crypta = new Main();
        //String toenc = "1";
        /*
        String toenc = " “srcAccount”:\"0011295527\",\r\n" +
                "“requestId\":\"12345678901234567890\",\r\n" +
                "“pin”:\"3019\"";
        */
        String toenc = "The quick brown fox jumps over the lazy dog";
        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 Main(String keyz, String ivStr) {
        ivspec = new IvParameterSpec(ivStr.getBytes(StandardCharsets.UTF_8));
        keyspec = new SecretKeySpec(keyz.getBytes(StandardCharsets.UTF_8), "AES");
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public Main()
    {
        ivspec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
        keyspec = new SecretKeySpec(secretkey.getBytes(StandardCharsets.UTF_8), "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(StandardCharsets.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, StandardCharsets.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" + Integer.toHexString(data[i]&0xFF);
            }
            else {
                str = str + 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;
        }
    }
}

Javascript code:

let iv = '9%Ysr^xyBTrPr!$Y';
let secret_key = 'GnxUFjKw9(bk7Ay$';
let plaintext = 'The quick brown fox jumps over the lazy dog';
console.log('plaintext: ', plaintext);

plaintext = CryptoJS.enc.Utf8.parse(plaintext);
secret_key = CryptoJS.enc.Utf8.parse(secret_key);
iv = CryptoJS.enc.Utf8.parse(iv);

var ciphertext = CryptoJS.AES.encrypt(plaintext, secret_key, {
  iv: iv,
  padding: CryptoJS.pad.Pkcs7,
  mode: CryptoJS.mode.CBC,
});

console.log('ciphertext: ', ciphertext.ciphertext.toString());

var decrypted = CryptoJS.AES.decrypt(ciphertext, secret_key,{
  iv: iv,
  padding: CryptoJS.pad.Pkcs7,
  mode: CryptoJS.mode.CBC,
});

console.log('decrypted (hex): ', decrypted.toString());
console.log('decrypted (str): ', CryptoJS.enc.Utf8.stringify(decrypted).toString());

huangapple
  • 本文由 发表于 2020年9月16日 20:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定