简体   繁体   English

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

[英]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 : 我正在使用PHP加密数据,而不是在解密我的Android应用程序中的数据(图像),但有时它会让我产生IOException并且我不确定如何解决这个问题。当我加密PNG时图像文件,没关系,没有异常抛出,我的应用程序可以在列表视图中加载图像。但是当我加密jpg文件时,它会抛出这个异常:

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 : 我在互联网上读到,问题是可能在加密和解密代码时使用NoPadding。这里是Java代码和PHP代码:

JAVA 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代码:

<?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! 所以我的问题是如何在php和java代码中使用填充来修复那些东西,所以我可以在我的app中加载我的图像。谢谢你的帮助!

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. 您正在使用块密码AES,因此需要将所有消息填充到块大小的多个部分。 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. PKCS#5是一种常见的填充约定,可能在两个系统上都可用。 I do not know PHP, but in Java you would need: 我不懂PHP,但在Java中你需要:

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

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

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