如何解密javascript中的文件,该文件由JAVA用AES加密 [英] How to decrypt a file in javascript which is encrypted by JAVA with AES

查看:105
本文介绍了如何解密javascript中的文件,该文件由JAVA用AES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用AES256加密了Java中的JPG文件,但是不知道用javascript解密JPG文件。谁有更好的主意?我正在苦苦挣扎4天。

I have encrypted JPG file in Java with AES256, but have no idea to decrypt the JPG file in javascript. Anyone has better idea? I'm struggling with it for 4days.

 byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 String key = "1234567890123456789012345678901d";

 AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
 SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
 cipher.init(mode, newKey, ivSpec);

 InputStream input = null;
 OutputStream output = null;

 try {
     input = new BufferedInputStream(new FileInputStream(new File("/home/java/test/aaa.JPG")));
     output = new BufferedOutputStream(new FileOutputStream(new File("/home/java/test/bbb.JPG")));
     byte[] buffer = new byte[1024];
     int read = -1;

     while((read = input.read(buffer)) != -1){
         output.write(cipher.update(buffer, 0, read));
     }

      output.write(cipher.doFinal());
 }
 finally {
     if(output != null){
         try {
             output.close();
         } catch(IOException ie){
             logger.info(ie.getMessage());
         }
     }
     if(input != null){
         try {
             input.close();
         } catch(IOException ie){
             logger.info(ie.getMessage());
         }
     }
 }

这是我的代码到目前为止尝试过我使用过CryptoJS并且Decrypt没有返回任何内容。

Here is the code I have tried so far. I have used CryptoJS and Decrypt does not return anything.

<!DOCTYPE html>
    <html>
    <head>

    <script type="text/javascript" src="./CryptoJS v3.1.2/rollups/aes.js"></script>
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
    <meta charset=utf-8 />
    <title>JS Bin</title>

    <style>
      article, aside, figure, footer, header, hgroup, 
      menu, nav, section { display: block; }
    </style>
    </head>
    <body>
      <input type='file' onchange="readURL(this);" />
        <img id="blah" src="#" alt="your image" />
        <a class="download" href="">Download</a>

        <script>
        var a = $('.download');
        var key =  CryptoJS.enc.Hex.parse("1234567890123456789012345678901d");
        var iv =  CryptoJS.enc.Hex.parse("00000000000000000000000000000000");

        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {


                    /////////
                    var decrypted = CryptoJS.AES.decrypt(e.target.result, key,
                    {
                            iv: iv,
                            mode: CryptoJS.mode.CBC,
                            padding: CryptoJS.pad.Pkcs7
                        }
                        ).toString(CryptoJS.enc.Latin1);


                    if(!/^data:/.test(decrypted)){
                        alert("Invalid pass phrase or file! Please try again.");
                        return false;
                    }

                    a.attr('href', decrypted);
                    a.attr('download', input.files[0].name.replace('.enc',''));             


            };

                //reader.readAsDataURL(input.files[0]);
                reader.readAsText(input.files[0]);
            }
        }


        </script>
    </body>
    </html>


推荐答案

你的密钥错了,Java(错误地)使用了密钥的ASCII表示:

Your key is wrong, Java (incorrectly) uses the ASCII representation of the key:

String key = "1234567890123456789012345678901d";
...
SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

导致AES-256的32字节密钥。但是你的JavaScript使用密钥的十六进制解码:

which results in a 32 byte key for AES-256. But your JavaScript uses the Hex decoding of the key:

var key =  CryptoJS.enc.Hex.parse("1234567890123456789012345678901d");

导致AES-128的16字节密钥。

which results in a 16 byte key for AES-128.

如果键错了,你显然无法得到正确的结果。

With wrong keys you will obviously not get the right results.

所以你要么必须对你的密钥进行编码,因为你在Java中使用了IV或使用十六进制解码器(默认情况下不存在于Java中)或者您应该修复您的JavaScript以使用与Java相同的方式并使用密钥字符串的ASCII编码。

So you'd either have to encode your key as you did your IV in Java or use a hex decoder (not present in Java by default) or you should "fix" your JavaScript to do the same as in Java and use the ASCII encoding of the key string.

一般来说,密钥不应该是字符串。

Keys, in general, should not be strings.

这篇关于如何解密javascript中的文件,该文件由JAVA用AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆