• 用java语言写一个AES算法,使用AES(CBC模式)对数据进行加密或解密。加解密用到的密钥(Key)和密钥偏移量(IV),代码实例类编写。


    以下是一个使用Java编写的AES算法实例,使用AES(CBC模式)对数据进行加密和解密。代码中包括了生成随机密钥和密钥偏移量的方法。

    java
    Copy code
    import javax.crypto.*;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.Base64;

    public class AESExample {

        private static final String AES_ALGORITHM = "AES";
        private static final String AES_TRANSFORMATION = "AES/CBC/PKCS5Padding";
        private static final int AES_KEY_SIZE = 256;

        public static void main(String[] args) {
            try {
                // 生成随机密钥
                SecretKey secretKey = generateRandomKey();

                // 生成随机密钥偏移量
                byte[] iv = generateRandomIV();

                String plaintext = "Hello, World!";

                // 加密
                String ciphertext = encrypt(plaintext, secretKey, iv);
                System.out.println("加密后的文本:" + ciphertext);

                // 解密
                String decryptedText = decrypt(ciphertext, secretKey, iv);
                System.out.println("解密后的文本:" + decryptedText);
            } catch (NoSuchAlgorithmException | NoSuchPaddingException
                    | InvalidKeyException | InvalidAlgorithmParameterException
                    | IllegalBlockSizeException | BadPaddingException e) {
                e.printStackTrace();
            }
        }

        // 生成随机密钥
        public static SecretKey generateRandomKey() throws NoSuchAlgorithmException {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM);
            keyGenerator.init(AES_KEY_SIZE);
            return keyGenerator.generateKey();
        }

        // 生成随机密钥偏移量
        public static byte[] generateRandomIV() {
            SecureRandom secureRandom = new SecureRandom();
            byte[] iv = new byte[16];
            secureRandom.nextBytes(iv);
            return iv;
        }

        // AES加密
        public static String encrypt(String plaintext, SecretKey secretKey, byte[] iv)
                throws NoSuchPaddingException, NoSuchAlgorithmException,
                InvalidAlgorithmParameterException, InvalidKeyException,
                BadPaddingException, IllegalBlockSizeException {
            Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }

        // AES解密
        public static String decrypt(String ciphertext, SecretKey secretKey, byte[] iv)
                throws NoSuchPaddingException, NoSuchAlgorithmException,
                InvalidAlgorithmParameterException, InvalidKeyException,
                BadPaddingException, IllegalBlockSizeException {
            Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
            byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes);
        }
    }
    上述代码中,generateRandomKey方法用于生成随机密钥,generateRandomIV方法用于生成随机密钥偏移量。encrypt方法使用提供的密钥和密钥偏移量对给定的明文进行加密,返回Base64编码后的密文。decrypt方法使用相同的密钥和密钥偏移量对密文进行解密,并返回解密后的明文。

    请注意,上述代码仅供示例目的,实际使用时需要妥善管理密钥和密钥偏移量以确保安全性。

  • 相关阅读:
    基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀
    leetcode:2032. 至少在两个数组中出现的值
    #! /usr/bin/env node 命令与 npm link 建立项目间软连接(一)
    NewStarCTF 2023 公开赛道 WEEK2|Crypto
    Python&Web服务器(HTTP协议)
    2023 “华为杯” 中国研究生数学建模竞赛(E题)深度剖析|数学建模完整代码+建模过程全解全析
    想考【软考高级】,但不具备计算机基础?“系规”适合你
    uni-app:实现等待加载功能
    FW-1答案
    普鸥知识产权|西班牙商标注册流程你知道吗?
  • 原文地址:https://blog.csdn.net/gb4215287/article/details/133915825