dongqiao8502 2011-08-04 06:56
浏览 62
已采纳

使用PHP加密,使用Java解密:IOException:数据不是块大小对齐

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_RIJNDAEL_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!

  • 写回答

1条回答 默认 最新

  • douxi3977 2011-08-04 09:08
    关注

    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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果