bouncycastle java 中 AES/CBC/ISO10126Padding 模式的问题

发布于 2024-11-16 15:14:22 字数 2694 浏览 1 评论 0原文

当我运行此代码时,每次运行时,控制台上打印的“加密字符串:”都是不同的。即加密和解密模式为 AES/CBC/ISO10126Padding 但加密的字符串确实解密成功。 当我在“AES”模式下运行相同的代码时,控制台上打印的“加密字符串:”每次都是相同的。请您告诉我为什么在 AES/CBC/ISO10126Padding 的情况下,每次运行时输出都是不同的。

import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;



//import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;


public class AES_Encrypt_Decrypt_Bouncy_Castle {

    public static SecretKeySpec getKey() {
        try {
            String keyFromfile = "ZoyNMZzsOYh7BxwcgJseBji95hmVTlgKe/9KFY44Jzg=";

            byte[] keyBytes = Base64.decode(keyFromfile.getBytes());
            byte[] finalKeyBytes = new byte[32];

            for (int i = 0; i < 32; i++) {
                finalKeyBytes[i] = keyBytes[i];
            }
            System.out.println("keyBytes="+keyBytes.length);
            System.out.println("Key is "+ keyBytes);
            for(int j=32;j<keyBytes.length;j++){
                System.out.println("keyBytes="+keyBytes[j]);
            }           
            SecretKeySpec skeySpec = new SecretKeySpec(finalKeyBytes, "AES");
            return skeySpec;
        } catch (Exception e) {
            return null;
        }

    }


    public static void main(String[] args) throws Exception {

        SecretKeySpec skeySpec = getKey();
        byte [] iv = Base64.decode("AWNCK2F/9llI0Rs+dQB36Q==");
        IvParameterSpec initializationVector = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());  
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initializationVector);
        String message = "Brian12345678";
        byte[] encrypted = cipher.doFinal(message.getBytes());
        System.out.println("Decoded plain text = "+new String (encrypted));
        byte[] encodedEncryptedText = Base64.encode(encrypted);
        System.out.println("encrypted string: " + new String (encodedEncryptedText));
        Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
        cipher2.init(Cipher.DECRYPT_MODE, skeySpec,initializationVector);
        byte[] original = cipher2.doFinal(Base64.decode(encodedEncryptedText));
        String originalString = new String(original);
        System.out.println("Original string: " + originalString);

    }

}

When I run this code the "encrypted string:" printed on the console is different each time I run. i.e. the encryption and decryption mode is AES/CBC/ISO10126Padding However the encrypted string does decrypt successfully.
When I run the same code in 'AES' mode the "encrypted string:" printed on the console is the same each time. Please can you tell me why in the case of AES/CBC/ISO10126Padding the output is diff each time i run it.

import java.io.FileOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;



//import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;


public class AES_Encrypt_Decrypt_Bouncy_Castle {

    public static SecretKeySpec getKey() {
        try {
            String keyFromfile = "ZoyNMZzsOYh7BxwcgJseBji95hmVTlgKe/9KFY44Jzg=";

            byte[] keyBytes = Base64.decode(keyFromfile.getBytes());
            byte[] finalKeyBytes = new byte[32];

            for (int i = 0; i < 32; i++) {
                finalKeyBytes[i] = keyBytes[i];
            }
            System.out.println("keyBytes="+keyBytes.length);
            System.out.println("Key is "+ keyBytes);
            for(int j=32;j<keyBytes.length;j++){
                System.out.println("keyBytes="+keyBytes[j]);
            }           
            SecretKeySpec skeySpec = new SecretKeySpec(finalKeyBytes, "AES");
            return skeySpec;
        } catch (Exception e) {
            return null;
        }

    }


    public static void main(String[] args) throws Exception {

        SecretKeySpec skeySpec = getKey();
        byte [] iv = Base64.decode("AWNCK2F/9llI0Rs+dQB36Q==");
        IvParameterSpec initializationVector = new IvParameterSpec(iv);
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());  
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, initializationVector);
        String message = "Brian12345678";
        byte[] encrypted = cipher.doFinal(message.getBytes());
        System.out.println("Decoded plain text = "+new String (encrypted));
        byte[] encodedEncryptedText = Base64.encode(encrypted);
        System.out.println("encrypted string: " + new String (encodedEncryptedText));
        Cipher cipher2 = Cipher.getInstance("AES/CBC/ISO10126Padding", new BouncyCastleProvider());
        cipher2.init(Cipher.DECRYPT_MODE, skeySpec,initializationVector);
        byte[] original = cipher2.doFinal(Base64.decode(encodedEncryptedText));
        String originalString = new String(original);
        System.out.println("Original string: " + originalString);

    }

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-11-23 15:14:22

来自维基百科填充页面

ISO 10126(已撤回,2007 [2] [3])指定应在最后一个块的末尾使用随机字节进行填充,并且填充边界应由最后一个字节指定。

这可以解释为什么加密的数据不同,但可以成功解密。

From the wikipedia padding page:

ISO 10126 (withdrawn, 2007 [2] [3] ) specifies that the padding should be done at the end of that last block with random bytes, and the padding boundary should be specified by the last byte.

That could explain why the encrypted data is different, but can decrypt successfully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文