Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法解密来自java脚本的值_Javascript_Java_Encryption - Fatal编程技术网

Javascript 无法解密来自java脚本的值

Javascript 无法解密来自java脚本的值,javascript,java,encryption,Javascript,Java,Encryption,我在前端使用CryptoJS加密值,但当我尝试在java中解密时,我无法这样做。是否有使用CryptoJS从前端获得的java值需要解密。 任何帮助都将不胜感激 加密代码: value="1234" key="abcdabcd12341234" encryption(value , key){ var enc4 = CryptoJS.AES.encrypt(value,key); console.log(enc4.toString()); public static String d

我在前端使用CryptoJS加密值,但当我尝试在java中解密时,我无法这样做。是否有使用CryptoJS从前端获得的java值需要解密。
任何帮助都将不胜感激

加密代码:

value="1234"
key="abcdabcd12341234"
encryption(value , key){

  var enc4 = CryptoJS.AES.encrypt(value,key);
  console.log(enc4.toString());
public static String decryption(String value, String key) throws Exception {
    try {
    SecretKeySpec secu = new SecretKeySpec(key.getBytes(character),Algorithm);
    String value1 = value.trim();
    byte[] raw = StringToBytes(value1);
    System.out.println(raw);
    Cipher cipher = Cipher.getInstance(Algorithm);
    cipher.init(Cipher.DECRYPT_MODE, secu);
    System.out.println("}}}}}}}}}}}}}}");
    byte[] fina = cipher.doFinal(raw);
    String g = new String(fina,character);
    return g;
    }
    catch(Exception e) {
        System.out.println("dead");
    }
    return "";
}


public static byte[]  StringToBytes(String value1) {
    try {
    String[] strs = value1.split("");

    System.out.println(strs.toString());
    byte[] bytes = new byte[strs.length];
    System.out.println(bytes);
    for(int i=0;i<bytes.length; i++) {
        System.out.println("came to for loop");
         //bytes[i] = (byte) Integer.parseInt(strs[i]);
        //int s = Integer.parseInt(strs[i]);
        //System.out.println("ududfdheifdei" + s);
        bytes[i]=(byte) Integer.parseInt(strs[i]);
         System.out.println("printing decrypted value"+ bytes[i]);
    }
    return bytes;
    }
    catch(Exception e) {
        System.out.println("long dead");
    }
    return new byte[0];
}
解密代码:

value="1234"
key="abcdabcd12341234"
encryption(value , key){

  var enc4 = CryptoJS.AES.encrypt(value,key);
  console.log(enc4.toString());
public static String decryption(String value, String key) throws Exception {
    try {
    SecretKeySpec secu = new SecretKeySpec(key.getBytes(character),Algorithm);
    String value1 = value.trim();
    byte[] raw = StringToBytes(value1);
    System.out.println(raw);
    Cipher cipher = Cipher.getInstance(Algorithm);
    cipher.init(Cipher.DECRYPT_MODE, secu);
    System.out.println("}}}}}}}}}}}}}}");
    byte[] fina = cipher.doFinal(raw);
    String g = new String(fina,character);
    return g;
    }
    catch(Exception e) {
        System.out.println("dead");
    }
    return "";
}


public static byte[]  StringToBytes(String value1) {
    try {
    String[] strs = value1.split("");

    System.out.println(strs.toString());
    byte[] bytes = new byte[strs.length];
    System.out.println(bytes);
    for(int i=0;i<bytes.length; i++) {
        System.out.println("came to for loop");
         //bytes[i] = (byte) Integer.parseInt(strs[i]);
        //int s = Integer.parseInt(strs[i]);
        //System.out.println("ududfdheifdei" + s);
        bytes[i]=(byte) Integer.parseInt(strs[i]);
         System.out.println("printing decrypted value"+ bytes[i]);
    }
    return bytes;
    }
    catch(Exception e) {
        System.out.println("long dead");
    }
    return new byte[0];
}
公共静态字符串解密(字符串值、字符串密钥)引发异常{
试一试{
SecretKeySpec secu=新的SecretKeySpec(key.getBytes(字符),算法);
字符串值1=value.trim();
字节[]原始=字符串字节(值1);
系统输出打印项次(原始);
Cipher Cipher=Cipher.getInstance(算法);
cipher.init(cipher.DECRYPT_模式,secu);
System.out.println(“}}}”);
字节[]fina=cipher.doFinal(原始);
字符串g=新字符串(fina,字符);
返回g;
}
捕获(例外e){
System.out.println(“死”);
}
返回“”;
}
公共静态字节[]StringToBytes(字符串值1){
试一试{
字符串[]strs=value1.split(“”);
System.out.println(strs.toString());
字节[]字节=新字节[strs.length];
System.out.println(字节);

对于(int i=0;i查看您的代码并阅读一些关于的内容,我假设您正在AES/ECB/PKCS5Padding中加密,因此在java中向解密添加相同的标记就足够了:

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
您还可以查看,这将为您的问题提供一些见解

此外,您还可以查看以检查如何改进代码。看起来您只需要:

public static String decryption(String value, String key) throws Exception {
    try {
        SecretKeySpec secu = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),"AES");
        byte[] raw = value.trim().getBytes(StandardCharsets.UTF_8);
        System.out.println(raw);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secu);
        System.out.println("}}}}}}}}}}}}}}");
        byte[] fina = cipher.doFinal(raw);
        return new String(fina,StandardCharsets.UTF_8);
    }
    catch(Exception e) {
        System.out.println("dead");
    }
    return "";
}

并删除
StringToBytes(String value1)

查看您的代码并阅读一些关于的信息,我假设您正在使用
AES/ECB/PKCS5Padding进行加密
,因此在java解密中添加相同的标记就足够了:

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
您还可以查看,这将为您的问题提供一些见解

此外,您还可以查看以检查如何改进代码。看起来您只需要:

public static String decryption(String value, String key) throws Exception {
    try {
        SecretKeySpec secu = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8),"AES");
        byte[] raw = value.trim().getBytes(StandardCharsets.UTF_8);
        System.out.println(raw);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secu);
        System.out.println("}}}}}}}}}}}}}}");
        byte[] fina = cipher.doFinal(raw);
        return new String(fina,StandardCharsets.UTF_8);
    }
    catch(Exception e) {
        System.out.println("dead");
    }
    return "";
}

并删除
StringToBytes(字符串值1)

如果您能提供加密结果enc4,则此世界将非常有用。ToString什么是
算法的值
?如果您能提供加密结果enc4,则此世界将非常有用。ToString什么是
算法的值