• 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自定义加密值。如果更改要两端同步改

    完了。谢谢!

  • 相关阅读:
    AUTOSAR规范与ECU软件开发(实践篇)9.7 AUTOSAR安全机制的E2E保护
    AIGC(生成式AI)试用 6 -- 从简单到复杂
    esxi网卡直通后虚拟机无网
    面试官:JVM调优,主要针对是哪一个区域?JVM内存结构是怎样的?
    C语言结合字符串和整形
    【数据结构】链表中二级指针的应用
    国内adc芯片公司
    Ubuntu安装Samba(阿里云服务器、samba端口映射)
    【Unity】找到目标的三种方式(范围内检测敌人)
    linux查找目录
  • 原文地址:https://blog.csdn.net/weixin_38351566/article/details/132686527