简体   繁体   English

如何解密CryptoJS中的文件,JAVA使用AES对其加密

[英]How to decrypt a file in CryptoJS , encrypted by JAVA with AES

enter image description here I want to encrypted JPG file in Java with AES, but I don't know how to decrypt the JPG file in javascript. 在这里输入图像描述,我想使用AES在Java中加密JPG文件,但是我不知道如何在JavaScript中解密JPG文件。 Anyone has better idea? 有人有更好的主意吗? this is my java code。 这是我的Java代码。

   `private static void EncFile(File srcFile, File encFile) throws Exception 
      {
           if(!srcFile.exists()){
            System.out.println("source file not exixt");
            return;
        }//
         if(!encFile.exists()){
           System.out.println("encrypt file created");
           encFile.createNewFile();
        }

    byte[] bytes = new byte[1024*8];   
    String key = "1234567812345678";
    String iv = "1234567812345678";
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    int blockSize = cipher.getBlockSize();
    SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);


    InputStream fis  = new FileInputStream(srcFile);
   // CipherInputStream cin = new CipherInputStream(fis, cipher);  
    OutputStream fos = new FileOutputStream(encFile);

   while ((dataOfFile = fis.read(bytes)) >0) {


       byte[] encrypted = cipher.doFinal(bytes);
        fos.write(encrypted,0,dataOfFile);
    }

    fis.close();
    fos.flush();
    fos.close();
}`

this my javascipt code, I have used CryptoJS and Decrypt does 这是我的javascipt代码,我用过CryptoJS和Decrypt

  var key  = CryptoJS.enc.Latin1.parse('1234567812345678');
  var iv   = CryptoJS.enc.Latin1.parse('1234567812345678'); 

var url = "http://192.168.0.103/show3d_test/test/CR4/E1234.png";  

var xhr = new XMLHttpRequest();  
xhr.open("GET",url,true);   
xhr.responseType = "arraybuffer";   
xhr.onload = function() {  
  if(xhr.readyState ==4){  
    if (xhr.status == 200){  
        process(xhr.response,key);   
    }
  }  
}  
xhr.send();  


function process(buffer,key) {  
 var view = new Uint8Array(buffer);  
 var contentWA = CryptoJS.enc.u8array.parse(view);   
 var dcBase64String = contentWA.toString(CryptoJS.enc.Base64);  
 var decrypted = CryptoJS.AES.decrypt(dcBase64String,key,
{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.NoPadding});

var d64 = decrypted.toString(CryptoJS.enc.Base64);  


var img = new Image;  
img.src = "data:image/png;base64,"+d64;  
document.body.append(img);  
}  `

Anyone know how to do that? 有人知道该怎么做吗? I've seen so much example about CryptoJS - Java encrypt/decrypt but most of them use hardcoded IV/key, or just send IV/key from cryptoJS side to Java side. 我已经看到了很多有关CryptoJS的示例-Java加密/解密,但大多数都使用硬编码的IV /密钥,或者只是将IV /密钥从cryptoJS端发送到Java端。 All I have is a passphrase, just like what this site do! 我所拥有的只是一个密码短语,就像该网站的内容一样!

Not sure what the exact question is but this should help. 不知道确切的问题是什么,但这应该有所帮助。

A general and secure method for an IV is to create one with a CSPRNG (random bytes) and prefix the encrypted data with the IV so it will be available for decryption. IV的一种通用且安全的方法是使用CSPRNG(随机字节)创建一个,并在IV之前为加密数据添加前缀,以便将其解密。 The IV does not need to be secret. IV不需要保密。

When using "AES/CBC/NoPadding" the input must be an exact multiple of the block size (16-bytes for AES). 使用“ AES / CBC / NoPadding”时,输入必须是块大小的精确倍数(AES为16字节)。 Generally PKCS#7 (PKCS#5) padding is specified so there are no limits on the data length to be encrypted. 通常,将指定PKCS#7(PKCS#5)填充,因此对要加密的数据长度没有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM