密码AES/CBC/PKCS5PADDIND在申请重新引入后不能解密

发布于 2025-01-27 17:26:32 字数 2260 浏览 1 评论 0原文

我正在尝试使用密码加密和解密一些文本,并使用“ AES/CBC/PKCS5PADDING”算法进行加密,但是如果我重新启动该应用程序,则无法解密加密的文本。 我正在加密文本,将基本64文本中的加密字节转换,将其存储并在必要时检索,因此将Base64文本转换为字节中,并试图解密原始文本。

我正在使用的代码是:

private static SecretKey getKeyFromPassword(String password, String salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {

    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(UTF_8),
            65536, 256);
    SecretKey secret = new SecretKeySpec(factory.generateSecret(spec)
            .getEncoded(), "AES");
    return secret;
}


private static IvParameterSpec generateIv() {
    byte[] iv = new byte[16];
    new SecureRandom().nextBytes(iv);
    return new IvParameterSpec(iv);
}

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        // string for test
        String teste = "teste teste teste teste";

        //getting start with Cipher
        SecretKey secretKey = getKeyFromPassword("pass", "salt");
        IvParameterSpec ivParameterSpec = generateIv();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        //encrypting
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] cipherText = new byte[0];
        cipherText = cipher.doFinal(teste.getBytes(UTF_8));

        // encrypt to base64 text
        String criptado = Base64.getEncoder().encodeToString(cipherText);
        

        //decripting
        byte[] plainText = new byte[0];
        byte[] rawText = Base64.getDecoder().decode(criptado);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        plainText = cipher.doFinal(rawText);
        String decriptado = new String(plainText);

问题是位键的大小吗?像在此主题中一样: PHP中的CBC/PKCS5PADDING(AES-256-CBC)产生了不同的结果

我将本指南列入了代码:baeldung.com/java-aes-ecryption-decryption

I'm trying to encrypt and decrypt some texts using Cipher with the "AES/CBC/PKCS5Padding" algorithm but if I restart the application the text that was encrypt can't be decrypted.
I'm encrypting the text, transforming the encrypted bytes in base64 text, storing it and retrieving it when necessary, so transforming the base64 text in bytes and trying to decrypt to take the original text.

The code I'm using is:

private static SecretKey getKeyFromPassword(String password, String salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {

    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(UTF_8),
            65536, 256);
    SecretKey secret = new SecretKeySpec(factory.generateSecret(spec)
            .getEncoded(), "AES");
    return secret;
}


private static IvParameterSpec generateIv() {
    byte[] iv = new byte[16];
    new SecureRandom().nextBytes(iv);
    return new IvParameterSpec(iv);
}

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidAlgorithmParameterException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        // string for test
        String teste = "teste teste teste teste";

        //getting start with Cipher
        SecretKey secretKey = getKeyFromPassword("pass", "salt");
        IvParameterSpec ivParameterSpec = generateIv();
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        //encrypting
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] cipherText = new byte[0];
        cipherText = cipher.doFinal(teste.getBytes(UTF_8));

        // encrypt to base64 text
        String criptado = Base64.getEncoder().encodeToString(cipherText);
        

        //decripting
        byte[] plainText = new byte[0];
        byte[] rawText = Base64.getDecoder().decode(criptado);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        plainText = cipher.doFinal(rawText);
        String decriptado = new String(plainText);

The problem is something with the size of the bit key? Like in this topic: java AES/CBC/PKCS5PADDING in php (AES-256-CBC) resulting different result

I took this guide to the code: baeldung.com/java-aes-encryption-decryption

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文