用javascript加密并用Java解密

时间:2019-10-30 08:53:22

标签: javascript java reactjs spring-boot react-native

我在我的本机应用程序中使用AES加密,如下所示:

import CryptoJS from 'crypto-js' ;
encryptFun() {
    var data = "123456";
    var key  = CryptoJS.enc.Latin1.parse('1234567812345678');
    var iv   = CryptoJS.enc.Latin1.parse('1234567812345678');  
    var encrypted = CryptoJS.AES.encrypt(
      data,
      key,
      {iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding
    });
    console.log('encrypted: ' + encrypted) ;
    var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
    console.log('decrypted: '+decrypted.toString(CryptoJS.enc.Utf8));
  }

输出=加密:aK7 + UX24ttBgfTnAndz9aQ ==

以下是我在后端使用Java进行解密的代码

 public static String desEncrypt() throws Exception {

        try
        {
            String data = "aK7+UX24ttBgfTnAndz9aQ==" ;
            String key = "1234567812345678";
            String iv = "1234567812345678";

            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

output = decryted:= 123456 [] [] [] [] [] [] [] [] [] [] []

Iam像上面一样放到16位。 我想要的输出应该只有123456。

2 个答案:

答案 0 :(得分:1)

我建议您使用java.util.Base64进行解码。以下正确解决。我还建议在return originalString中使用trim,看看是否可行。

public class Decrypt {

    public static void main(String[] args) {
        try
        {
            String data = "aK7+UX24ttBgfTnAndz9aQ==" ;
            String key = "1234567812345678";
            String iv = "1234567812345678";

            Decoder decoder = Base64.getDecoder();   
             byte[] encrypted1 = decoder.decode(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            System.out.println(originalString.trim());
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

当您在JS中填充zeros时,无法使用NoPadding进行解密,解决方案是在数组或字符串上进行操作

  • 修剪数组

    int i = original .length - 1;
    while (i >= 0 && original [i] == 0) {
        --i;
    }
    return new String(Arrays.copyOf(original , i + 1));
    
  • 修剪字符串

    return new String(original).trim();
    
  • 手动删除zero

    return new String(original).replace("\0", "");
    

或者,似乎Java中没有实现ZeroPAddingCipher Documentation),请看一下建议使用CryptoJS.pad.Pkcs

How Encrypt with AES CBC Zero Padding in Javascript and decrypt with Java