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

标签 java encryption rijndael

我正在开发一个用纯 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/

相关文章:

c# - 当出现 "Padding is invalid and cannot be removed"错误时,如何将 C++ Rijndael 密码学转换为 C#?

c# - 部分读取最终 block 后的填充错误

ruby - Perl & Ruby 交换 AES 加密信息

java - Struts 2 验证不适用于动态表单字段

java - LDAP。没有身份验证的Spring应用程序

php - 存储加密 key 的安全方法 - PHP

mysql - 什么类型的密码哈希?可能的mysql哈希值?

python - 在 mysql 中生成加密密码,以填充 django 数据库

java - 如何从 Java 代码获取 Ganglia 指标

Java GUI JPanel 不工作