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 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程