gpt4 book ai didi

java - 使用 Java 和 Bouncy CaSTLe 进行 Rijndael 256 加密

转载 作者:行者123 更新时间:2023-11-30 05:28:58 28 4
gpt4 key购买 nike

我正在开发一个用纯 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:解密中最后一个 block 不完整

我已经为这个解密工作了两周了,我真的很绝望:(

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

最佳答案

解密方法中,必须首先对密文进行 Base64 解码 (1)。此外,解密文本的长度未正确确定(2a),并且相应字节数组的长度未相应调整(2b)。最后,从字节数组中确定 UTF8 字符串存在问题 (3)。修改decrypt方法的主体如下:

//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.Base64org.bouncycaSTLe.util.Arrays

尽管这可能是遗留代码,但有两个关于安全性的注意事项:密码通常不应加密,但hashed 。另外,ECB是没有安全感的。

关于java - 使用 Java 和 Bouncy CaSTLe 进行 Rijndael 256 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57965316/

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