• java和js实现AES对称加密


    java

    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.util.Base64;
    
    public class AESUtil {
    
        public static void main(String[] args) throws Exception {
            String plainText = "admin123";
    
            System.out.println(AESEncrypt(plainText));
            System.out.println(AESDecrypt(AESEncrypt(plainText)));
        }
    
    
        /**
         * 获取AES编码规则
         * 定义密钥和向量
         *
         * @return
         */
        public static byte[] genEncodeRule() throws Exception {
            // 定义密钥和向量
            return "jsi43idkw6mqk12i".getBytes("utf-8");
        }
    
        /**
         * AES加密
         *
         * @param content 参数
         * @return
         */
        public static String AESEncrypt(String content) throws Exception {
            // 加密
            SecretKeySpec keySpec = new SecretKeySpec(genEncodeRule(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(genEncodeRule());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
            byte[] encryptedBytes = cipher.doFinal(content.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    
        /**
         * AES解密
         *
         * @param content 加密后的值
         * @return
         */
        public static String AESDecrypt(String content) throws Exception {
            // 解密
            SecretKeySpec keySpec = new SecretKeySpec(genEncodeRule(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(genEncodeRule());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            byte[] decode = Base64.getDecoder().decode(content);
            byte[] decryptedBytes = cipher.doFinal(decode);
            return new String(decryptedBytes, "UTF-8");
        }
    
    
    }
    
    • 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

    js

    
    
    • 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

    在这里插入图片描述
    js导入后直接调用 encryptData(‘admin’) 加密方法,decryptData(‘ZE4bck/oYCTmVKwBizgXyw==’)解密方法

    其中 : jsi43idkw6mqk12i自定义加密值。如果更改要两端同步改

    完了。谢谢!

  • 相关阅读:
    三、初识FreeRTOS之FreeRTOS基础知识
    React中实现大模型的打字机效果
    kubernetes—数据存储
    Java程序员:三个月刷完1000道面试真题,没想到老板直接给我升职了
    R可视化:生存分析森林图
    非对称加密之RSA是怎么加密的?
    [PAT练级笔记] 23 Basic Level 1023 组个最小数
    “单一职责模式”
    如果Controller里有私有的方法,能成功访问吗?
    德克萨斯大学奥斯汀分校自然语言处理硕士课程汉化版(第二周) - 多类别分类和神经网络
  • 原文地址:https://blog.csdn.net/weixin_38351566/article/details/132686527