- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个用纯 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.Base64
和org.bouncycaSTLe.util.Arrays
。
关于java - 使用 Java 和 Bouncy CaSTLe 进行 Rijndael 256 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57965316/
我使用函数 EncryptStringToBytes 将纯文本加密为字节数组,并且最后将字节数组转换为字符串并返回。 我使用另一个函数将加密文本解密为纯文本。 我尝试用 RC2 对文本进行打乱,但它引
我正在考虑切换到将 session 数据存储在加密的 cookie 中,而不是在我的服务器上的某个地方。虽然这将导致每个请求使用更多带宽 - 它将节省额外的数据库服务器负载和存储空间。 无论如何,我计
我在使用 BouncyCaSTLe API for Java 实现 Rijndael 加密时遇到问题。 当我执行cipher.doFinal(inputTextBytes, intOutOff);时,
我正在使用发现的 DCPcrypt 库 here . 这是一个加密字符串的小代码 InitializationVector: AnsiString; const Key: Ansistring = '
我正在尝试模仿 http://www.hanewin.net/encrypt/aes/aes-test.htm 的 Rijndael (AES) 加密在Python3. 具体来说,当我使用以下输入时:
我正在一个类中组织一些非常基本的对称加密/解密代码以供将来使用。我能够成功加密和解密...只是有一个小问题。 这是我从一个流中读入并加密/解密到另一个流的代码: public void Encrypt
我正在尝试使用 Rijndael 加密来加密图像文件,我一定是做错了什么,因为解密的文件在文件的前面带有额外的数据,最后可能还有一点额外的数据。我对这种加密算法还很陌生,所以我很确定我只是遗漏了一些简
我正在研究 AES-256-CBC 的实现(如下所示),这将用于客户端到数据库的设置。 key 存储在本地,此方案的目的是在数据库内容泄漏时使某些值不可读。 由于IV是固定长度的,所以我打算将密文设为
我正在尝试使用 Rijndael 或 Aes 以及下面的代码来加密/解密字符串。 public class Crypto { private const string defaultVecto
我想使用 C# 研究 Rijndael 算法。有人可以帮我解决这个问题吗? 最佳答案 我假设您指的是 Rijndael 加密算法 - 在这种情况下 RinjdaelManaged会有用的。 MSDN
您好,我正在尝试通过 Rijaendal 加密/解密字符串。我根本无法弄清楚为什么解密会失败。我总是以不正确的填充错误结束。让我失望的一件事是我作为 HEX 数组返回的加密结果。它的长度为 14 个字
我有一个进行 Rijndael 加密的 PHP 代码的引用。我想将它转换为java代码,我尝试了几个例子,但没有一个对我有用。这是 php 代码: $initialisationVector = ha
我多次使用以下 Rijndael 代码进行加密。但是为什么它不能加密一个 4.2 GB 的 ISO 文件呢?事实上,我的电脑有 16GB 内存,应该不是内存问题。我使用 Windows 7 旗舰版。代
只有部分字符串被解密,我认为这与我的编码有关。 这是发生了什么: string s = "The brown fox jumped over the green frog";
我尝试了来自SO和整个网络的许多答案,但仍然没有成功。 我使用以下tool进行加密。 text to encrypt: tom key: exo123exo1exo123 input (textfie
我有一个编码和解码字符串的安全类,但是当我尝试解码时 - 出现了问题。 这是我的安全类: class Security { public static String encrypt(Strin
我写的一些软件使用了.Net4.0的Rijndael算法来加密和解密许可证信息。 问题如下:该程序生成一个字符串,该字符串被转换为 byte[]。这个 byte[] 然后用 Rijndael 加密成另
我在一个网站上找到了这段代码 private void EncryptFile(string inputFile) { string password
当我选择密码加密时,我发现Rijndael算法是最好的加密之一,无法通过暴力破解。 所以我选择了Rijndael算法对用户密码进行加密。 现在我已经确定,哈希(不可逆)比加密(可逆)更安全[如果我错了
我有一些代码可以用 Rijndael 解密密码 public static string DecryptPassword(string encrypted) { using (MemorySt
我是一名优秀的程序员,十分优秀!