【问题标题】:Encryption with PHP, decryption with Java : IOException: data not block size aligned使用 PHP 加密,使用 Java 解密:IOException:数据未对齐块大小
【发布时间】:2023-04-06 08:54:01
【问题描述】:

我正在使用 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)

我在网上看到问题可能是在加密和解密代码时使用 NoPadding。这是 Java 代码和 PHP 代码:

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);
    }
    }

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;
?>

所以我的问题是如何在 php 和 java 代码中使用填充来修复这些东西,这样我就可以在我的应用程序中加载我的图像。非常感谢您的帮助!

【问题讨论】:

标签: php android image encryption


【解决方案1】:

您需要在加密端指定填充,在解密端指定相同的填充,以便可以删除填充。您正在使用块密码 AES,因此所有消息都需要填充到块大小的多个值。因此,您的错误消息“数据未对齐块大小”。

检查两端都有哪些填充可用,然后选择它们都支持的一个。 PKCS#5 是一种常见的填充约定,可能在两个系统上都可用。我不知道 PHP,但在 Java 中你需要:

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

【讨论】:

  • 事实上我知道如何在 Java 中做到这一点,但 PHP 是我的问题。
  • 奇怪的是,当我解密png文件时,没有异常,但是当我尝试解密jpg文件时,却抛出异常。
  • @psychenaut:对不起,我对 PHP 无能为力。至于 .png 文件,可能是该文件恰好是确切数量的块。您是否尝试过不同大小的 .png 文件?
  • 是的,我尝试了 5 种不同的图像,没有例外,只有 jpegs
猜你喜欢
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多