• JAVA使用AES实现对称加密


    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.nio.charset.StandardCharsets;
    import java.util.Base64;
    import java.util.UUID;
    
    /**
     * @author chunyang.leng
     * @date 2021-11-05 11:18 上午
     */
    
    public class AesUtils {
    
        /**
         * 密钥长度必须为16位
         */
        private static final String PASSWORD = "0123456789123456";
    
        private static final String ALGORITHM = "AES";
        
        private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
        /**
         * AES加密字符串
         *
         * @param message 需要被加密的字符串
         * @return base64 密文
         */
        public static String encrypt(String message) throws Exception {
            if (message == null ){
                return null;
            }
            // AES专用密钥
            SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
            // 创建密码器
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            byte[] byteContent = message.getBytes(StandardCharsets.UTF_8);
            // 初始化为加密模式的密码器
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            // 加密后的byte数据
            byte[] bytes = cipher.doFinal(byteContent);
            // 转base64
            return Base64.getEncoder().encodeToString(bytes);
        }
    
        /**
         * 解密AES加密过的字符串
         *
         * @param encryptMessage  AES加密过过的内容
         * @return 明文
         */
        public static String decrypt(String  encryptMessage)throws Exception {
            if (encryptMessage == null){
                return null;
            }
            
            byte[] decodeArray = Base64.getDecoder().decode(encryptMessage);
            // AES专用密钥
            SecretKeySpec secretKey = new SecretKeySpec(PASSWORD.getBytes(), ALGORITHM);
            // 创建密码器
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            // 初始化为解密模式的密码器
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            // 明文数组
            byte[] bytes = cipher.doFinal(decodeArray);
    
            return new String(bytes, StandardCharsets.UTF_8);
    
        }
    
    
        public static void main(String[] args) throws Exception {
    
            boolean b = true;
            for (int i = 0; i < 50 ; i++) {
                String uuid = UUID.randomUUID().toString();
                System.out.println("uuid=====>" + uuid);
                String encrypt = encrypt(uuid);
                System.out.println("encrypt=====>" + encrypt);
                String decrypt = decrypt(encrypt);
                System.out.println("decrypt=====>" + decrypt);
                b &= decrypt.equals(uuid);
            }
            System.out.println("final =====>" + b);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    transformers简介
    Spring Controller内存马
    linux查找生产问题常用命令——参数解释
    kafka ack确认机制
    Spring容器启动流程
    leetcode1两数之和
    xorm数据库操作之Join、Union
    20.2 OpenSSL 非对称RSA加解密算法
    【Linux】结合当前软件工程技术的背景,评价当前市面上流行的操作系统的局限性
    Navicat:显示的行数与表中实际的行数不一致
  • 原文地址:https://blog.csdn.net/weixin_42321034/article/details/126243367