java - 使用 Java 和 Bouncy Castle 进行 Rijndael 256 加密
问题描述
我正在开发一个用纯 php 构建的项目,我正在对登录进行返工,但是数据库中的用户是 Rijndael-256 中的密码,我尝试了很多东西,但似乎没有任何效果,并且我觉得我很接近这段代码,但它不起作用,我真的迷路了
private final String key = "...";
public String decrypt(String password, String cypherKey) {
try {
password = password.substring(0, password.lenght() - 1); // 1
byte[] passwordBytes = password.getBytes("UTF-8");
byte[] key = cypherKey.getBytes("UTF-8");
RijndaelEngine rijndaelEngine = new RijndaelEngine(256);
KeyParameter keyParam = new KeyParameter(key);
rijndaelEngine.init(false, keyParam); // 2
PaddedBufferedBlockCipher bufferedBlock = new PaddedBufferedBlockCipher(rijndaelEngine, new ZeroBytePadding());
byte[] decryptedBytes = new byte[bufferedBlock.getOutputSize(passwordBytes.length)];
int processed = bufferedBlock.processBytes(passwordBytes, 0, passwordBytes.length, decryptedBytes, 0);
return String.valueOf(bufferedBlock.doFinal(decryptedBytes, processed));
} catch (Exeption e) {
e.printStackTrace();
}
return ""; // I know this is awful but i was trying something and left this like that
}
*1) 我不知道这是否正确,但所有加密密码都以等号结尾,我用加密工具测试过,我认为不需要
2) False为解密模式
堆栈跟踪:org.bouncycastle.crypto.DataLengthException:最后一个块在解密中不完整
我正在为这个解密工作两个星期,我真的很绝望:(
PHP代码:
function fnEncrypt($sValue)
{
include("constants.php");
return trim(
base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$SecretKey, $sValue,
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND)
)
)
);
}
function fnDecrypt($sValue)
{
include("constants.php");
return trim(
mcrypt_decrypt(
MCRYPT_RIJNDAEL_256,
$sSecretKey,
base64_decode($sValue),
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND
)
)
);
}
解决方案
在decrypt
- 方法中,密文必须首先经过 Base64 解码 (1)。此外,解密文本的长度没有正确确定(2a),相应的字节数组的长度也没有相应调整(2b)。最后,从字节数组中确定 UTF8 字符串存在问题 (3)。修改decrypt
-method的主体如下:
//password = password.substring(0, password.lenght() - 1); // 1 // Remove
//byte[] passwordBytes = password.getBytes("UTF-8"); // Remove
byte[] passwordBytes = Base64.getDecoder().decode(password); // Base64-decode the ciphertext (1)
byte[] key = cypherKey.getBytes("UTF-8");
RijndaelEngine rijndaelEngine = new RijndaelEngine(256);
KeyParameter keyParam = new KeyParameter(key);
rijndaelEngine.init(false, keyParam); // 2
PaddedBufferedBlockCipher bufferedBlock = new PaddedBufferedBlockCipher(rijndaelEngine, new ZeroBytePadding());
byte[] decryptedBytes = new byte[bufferedBlock.getOutputSize(passwordBytes.length)];
int processed = bufferedBlock.processBytes(passwordBytes, 0, passwordBytes.length, decryptedBytes, 0);
processed += bufferedBlock.doFinal(decryptedBytes, processed); // Refresh the parameter containing the length of the decrypted data (2a)
decryptedBytes = Arrays.copyOfRange(decryptedBytes, 0, processed); // Reduce the byte-array accordingly (2b)
//return String.valueOf(bufferedBlock.doFinal(decryptedBytes, processed)); // Remove
return new String(decryptedBytes, "UTF-8"); // Create a UTF-8 string from the byte-array (3)
与进口java.util.Base64
和org.bouncycastle.util.Arrays
。
推荐阅读
- python - 当 dtype 是(显然)无论如何都会选择的内容时,为什么设置 Series dtype 会引发 TypeError?
- angular - NG6:图书馆提供的服务未定义
- macos - XML 和 INI 文件无法在 Mac 上直接打开
- javascript - 在比较期间通过通配符或正则表达式省略多个键
- sql - 在 web 应用程序中保存图像文件,java
- bash - makefile 中的 Bash 通配符不起作用
- javascript - 反应 index.html 呈现但反应组件没有
- linux - 如何在 CLion 中链接库
- facebook - Facebook like_count of my own page
- php - PHP cURL 突然无法在 Windows 10 Pro 上加载