gpt4 book ai didi

java - Android/Java AES 256 CBC 与 PHP 中的 PKCS5Padding 解密

转载 作者:行者123 更新时间:2023-11-30 11:18:33 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中加密一些数据,然后将其发送到 PHP 页面进行解密和处理。

正在使用的密码是"AES/CBC/PKCS5Padding"

现在一切正常(在大量挖掘信息之后)。但是,生成的解密数据在末尾添加了许多新行,这些行在从应用程序发送的原始数据中不存在。

我假设这是 PHP 不支持 PKCS5Padding 的副作用。假设末尾总是在字符串后附加换行符或空格,我感到很不舒服。

如果我尝试使用 mcrypt docs 中建议的代码,加密缓冲区被清空。

是否有更好的取消填充解决方法?


编辑 : 根据要求添加代码

PHP

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
if($cipher === false)
{
trigger_error("AES compatible cipher missing", E_USER_WARNING);
exit;
}
$InitResult = mcrypt_generic_init($cipher, $AesPassword, $AesIv);
if($InitResult !== 0)
{
trigger_error("AES cipher init failed", E_USER_WARNING);
exit;
}
// now do the decryption
$DataBlock = mdecrypt_generic($cipher, $EncryptedBlock);
// close down mcrypt
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);

安卓/Java:

String strEncrypted = null;
Cipher cipher = null;
IvParameterSpec ivSpec = null;
byte[] btEncrypted = null;

try
{
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
ivSpec = new IvParameterSpec(m_btIV);

cipher.init(Cipher.ENCRYPT_MODE, m_KeySpec, ivSpec);
btEncrypted = cipher.doFinal(strData.getBytes(m_strCharSet));
strEncrypted = Base64.encodeToString(btEncrypted, Base64.NO_PADDING | Base64.NO_WRAP);
}
catch(Exception e)
{
e.printStackTrace();
}

return strEncrypted;

请注意, key 和 iv 在 Android 中计算并在 POST 数据中传输到服务器。

这有帮助吗?

最佳答案

遗憾的是,Java SE 提供程序不支持 PHP 填充。 Bouncy CaSTLe 也不支持这种填充,因为 Bouncy CaSTLe 始终至少填充 1 个字节,即使是零填充也是如此。


经过大量调整,这是我能想到的最好的:

/**
* Pads data with zero valued bytes until the next block boundary is met.
* Does not pad if the number of blocks is already on a boundary. This
* method is not safe for binary data that may end with zero valued bytes as
* they may be removed by the unpadding method.
* If available, try and use PKCS#7 compatible padding instead.
*
* @param data
* the binary data to pad, never null
* @param blocksize
* the block size in bytes of the block cipher
* @return the padded binary data as a copy
* @throws NullPointerException
* if data is null
*/
public static byte[] phpPad(final byte[] data, final int blocksize) {
if (data.length == 0) {
return data;
}

final int blocks = (data.length - 1) / blocksize + 1;
return Arrays.copyOf(data, blocks * blocksize);
}

/**
* Unpads data removing zero valued bytes, removing up to blocksize - 1
* bytes of padding. The input of the unpad method should consist of n times
* the blocksize.
*
* @param data
* the binary data to unpad, never null
* @param blocksize
* the block size in bytes of the block cipher
* @return the unpadded binary data as a copy
* @throws NullPointerException
* if data is null
* @throws IllegalArgumentException
* if the data is not n times the blocksize
*/
public static byte[] phpUnpad(final byte[] data, final int blocksize) {
if (data.length % blocksize != 0) {
throw new IllegalArgumentException(
"Padded data should dividable by the block size");
}

if (data.length == 0) {
return data.clone();
}

int padBytes = 0;
for (; padBytes < blocksize; padBytes++) {
if (data[data.length - padBytes - 1] != 0x00) {
break;
}
}

return Arrays.copyOf(data, data.length - padBytes);
}

关于java - Android/Java AES 256 CBC 与 PHP 中的 PKCS5Padding 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23809324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com