0
votes

enter image description hereI want to encrypted JPG file in Java with AES, but I don't know how to decrypt the JPG file in javascript. Anyone has better idea? this is my java code。

   `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

  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. All I have is a passphrase, just like what this site do!

1
When I decrypt this picture, I find that I can only decrypt the successful part. This picture shows only one part of the display - sunday

1 Answers

0
votes

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. The IV does not need to be secret.

When using "AES/CBC/NoPadding" the input must be an exact multiple of the block size (16-bytes for AES). Generally PKCS#7 (PKCS#5) padding is specified so there are no limits on the data length to be encrypted.