zoukankan      html  css  js  c++  java
  • AES前后加密算法代码

     首先下载 aes.js加密工具类:

     本文采用的是 AES/ECB/PKCS5Padding的加密方式进行加密的:

     js加密写法如下: 

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
    </head>
    
    <script src="aes.js"></script>
    <body>
    
    
    <script>
    
       
    
        var key = CryptoJS.enc.Utf8.parse("8NONwyJtHesysWpM");
    
        var plaintText = 'ABCDEFGH'; // 明文
    
        var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
    
        console.log("加密前:"+plaintText);
        console.log("加密后:"+encryptedData);
    
        encryptedData = encryptedData.ciphertext.toString();
    
        var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
        var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    
        var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
    
        var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    
        console.log("解密后:"+decryptedStr);
    
        
         var pwd = "PCsUFtgog9/qpqmqXsuCRQ==";
        //加密服务端返回的数据
        var decryptedData = CryptoJS.AES.decrypt(pwd, key, {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
        
        console.log("解密服务端返回的数据:"+decryptedStr);
    
    </script>
    </body>
    </html>

     Java加解密类: 

    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.net.util.Base64;  
    
    public class AESUtil {
        public static String encrypt(String input, String key){
          byte[] crypted = null;
          try{
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
              Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
              cipher.init(Cipher.ENCRYPT_MODE, skey);
              crypted = cipher.doFinal(input.getBytes());
            }catch(Exception e){
                System.out.println(e.toString());
            }
            return new String(Base64.encodeBase64(crypted));
        }
    
        public static String decrypt(String input, String key){
            byte[] output = null;
            try{
              SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
              Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
              cipher.init(Cipher.DECRYPT_MODE, skey);
              output = cipher.doFinal(Base64.decodeBase64(input));
            }catch(Exception e){
              System.out.println(e.toString());
            }
            return new String(output);
        }
        
        public static void main(String[] args) {
          String key = "8NONwyJtHesysWpM";
          String data = "ABCDEFGH";
          System.out.println(AESUtil.decrypt(AESUtil.encrypt(data, key), key));
          System.out.println(AESUtil.encrypt(data, key));        
        }    
    }
  • 相关阅读:
    Android 自定义android控件EditText边框背景
    Android安全问题 静音拍照与被拍
    Android 自绘TextView解决提前换行问题,支持图文混排
    Android EditText属性
    Android invalidate()自动清屏,屏幕刷新
    Cocos2d-x 3.0final 终结者系列教程12-Vector&amp;map&amp;value
    思维方式--SMART原则
    从.net复制源代码中国农历阵列,必要做日历
    POJ 3071-Football(可能性dp)
    mongodb group包(最具体的、最受欢迎、最容易理解的解释)
  • 原文地址:https://www.cnblogs.com/shawWey/p/8425894.html
Copyright © 2011-2022 走看看