开发者

Encryption with PHP, decryption with Java : IOException: data not block size aligned

I'm using PHP encryption of data and than I'm decrypting the data (images) in my Android application,but sometimes it's throwin me an IOException and I'm not really sure how to fix that.When I'm encrypting an PNG image file,it's ok,there is no exception thrown and my app can load the image in list view.But when I'm encrypting a jpg file it's throwing me this exception :

08-04 06:36:54.734: WARN/System.err(254): java.io.IOException: data not block size aligned
08-04 06:36:54.734: WARN/System.err(254):     at javax.crypto.CipherInputStream.read(CipherInputStream.java:97)
08-04 06:36:54.734: WARN/System.err(254):     at javax.crypto.CipherInputStream.read(CipherInputStream.java:152)
08-04 06:36:54.734: WARN/System.err(254):     at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:183)
08-04 06:36:54.734: WARN/System.err(254):     at java.io.BufferedInputStream.read(BufferedInputStream.java:346)
08-04 06:36:54.734: WARN/System.err(254):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-04 06:36:54.734: WARN/System.err(254):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
08-04 06:36:54.734: WARN/System.err(254):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:515)
08-04 06:36:54.734: WARN/System.err(254):     at com.custom.lazylist.ImageLoader.getBitmap(ImageLoader.java:105)
08-04 06:36:54.734: WARN/System.err(254):     at com.custom.lazylist.ImageLoader.access$0(ImageLoader.java:75)
08-04 06:36:54.734: WARN/System.err(254):     at com.custom.lazylist.ImageLoader$PhotosLoader.run(ImageLoader.java:228)

I read over the internet that it's the problem is probably using NoPadding at encrypting and decrypting code.Here is the Java code and PHP code :

JAVA

    try {

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

    AssetManager is = this.getAssets();
    InputStream fis = is.open("card2_encrypted.jpg");

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    FileOutputStream fos  = new FileOutputStream(
               new File(Environment.getExternalStorageDirectory(), "card2_decrypted.jpg"));




    byte[] b = new byte[8];
    int i;

    while ((i = cis.read(b)) != -1) {
      fos.write(b, 0, i);
    }
    fos.flush(); fos.close();
    cis.close(); fis.close();   

    }
    catch(Exception e){
        e.fillInStackTrace();
        Log.v("Error","Error "+e);
    }
    }

and the PHP code :

<?php
    $secret_key   = "01234567890abcde";
    $iv           = "fedcba9876543210";
    $infile       = "ss1.jpg";
    $outfile      = "ss1_encrypted.jpg";

    $crypttext = file_get_contents($infile);
    $plaintext = mcrypt_encrypt(MCRYPT_RIJNDA开发者_JAVA技巧EL_128,
    $secret_key, $crypttext, MCRYPT_MODE_CBC, $iv);

    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . strlen($plaintext));
    header('Content-Disposition: attachment; filename=' . ($outfile));
    echo $plaintext;
?>

So my quesion is how can I fix that stuff with padding in php and java code,so I can load my images in my app.Thanks a lot for the help!


You need to specify padding at the encryption end, and the same padding at the decryption end so the padding can be removed. You are using a block cypher, AES, so all messages need to be padded to a mutliple of the block size. Hence your error message "data not block size aligned".

Check what paddings are available at both ends and pick one that they both support. PKCS#5 is a common padding convention which is likely to be available on both systems. I do not know PHP, but in Java you would need:

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜