• AES加解密


     原文:http://www.cnblogs.com/grimm/p/7233158.html

    1,Java端(依赖 common-codec jar)

    [java] view plain copy
     
    1. package com.jiaMi;  
    2.   
    3. import javax.crypto.Cipher;  
    4. import javax.crypto.spec.IvParameterSpec;  
    5. import javax.crypto.spec.SecretKeySpec;  
    6.   
    7. import org.apache.commons.codec.binary.Base64;  
    8.   
    9.   
    10. public class AESUtils {  
    11.       
    12.     private final static String KEY="1234123412341324";  
    13.     private final static String IV="1234123412341234";  
    14.       
    15.     /** 
    16.      * aes 加密 
    17.      * @param data 
    18.      * @return 
    19.      */  
    20.     public static String encryptData(String data){  
    21.         try {  
    22.             Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");  
    23.             int blockSize = cipher.getBlockSize();  
    24.             byte[] dataBytes = data.getBytes();  
    25.             int plaintextLength = dataBytes.length;  
    26.             if (plaintextLength % blockSize != 0) {  
    27.                 plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));  
    28.             }  
    29.             byte[] plaintext = new byte[plaintextLength];  
    30.             System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);  
    31.             SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), "AES");  
    32.             IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());  
    33.             cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);  
    34.             byte[] encrypted = cipher.doFinal(plaintext);  
    35.             return new String(Base64.encodeBase64(encrypted));  
    36.         } catch (Exception e) {  
    37.             e.printStackTrace();  
    38.         }  
    39.         return null;  
    40.     }  
    41.       
    42.       
    43.     /** 
    44.      * aes 解密 
    45.      * @param data 密文 
    46.      * @return 
    47.      */  
    48.     public static String decryptData(String data){  
    49.         try {  
    50.             byte[] encrypted1 =Base64.decodeBase64(data.getBytes());  
    51.             Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");  
    52.             SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), "AES");  
    53.             IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());  
    54.             cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);  
    55.             byte[] original = cipher.doFinal(encrypted1);  
    56.             String originalString = new String(original);  
    57.             return originalString;  
    58.         } catch (Exception e) {  
    59.             e.printStackTrace();  
    60.         }  
    61.         return null;  
    62.     }  
    63.       
    64.       
    65.     public static void main(String[] args) {  
    66.         String data="php和java互通!";  
    67.         String enStr=AESUtils.encryptData(data);  
    68.         System.out.println("加密:"+enStr);  
    69.         String deStr=AESUtils.decryptData(enStr);  
    70.         System.out.println("解密:"+deStr);  
    71.     }  
    72.   
    73. }  


    2,php 端

    [php] view plain copy
     
    1. <?php  
    2.  $privateKey = "1234123412341324";  
    3.  $iv    = "1234123412341324";  
    4.  $data  = "测试用的数据";  
    5.   
    6.  //加密  
    7.  $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);  
    8.  echo(base64_encode($encrypted));  
    9.  echo '<br/>';  
    10.   
    11.  //解密  
    12.  $encryptedData = base64_decode(base64_encode($encrypted));  
    13.  $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);  
    14.  echo($decrypted);  
    15. ?>  


    3,js端

    [javascript] view plain copy
     
      1.  <script src="./crypto-js.js"></script>  
      2.  <script src="./aes.js"></script>  
      3.  <script src="./pad-zeropadding.js"></script>  
      4.  <script>  
      5. var data = "测试用的数据";  
      6. var key  = CryptoJS.enc.Latin1.parse('1234123412341324');  
      7. var iv   = CryptoJS.enc.Latin1.parse('1234123412341324');  
      8.   
      9. //加密  
      10. var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});  
      11.   
      12.   
      13. //alert(encrypted);  
      14.   
      15.    console.log(encrypted.toString());  
      16.   
      17. //解密  
      18. var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});  
      19. console.log(decrypted.toString(CryptoJS.enc.Utf8));  
      20.   
      21. //alert(decrypted.toString(CryptoJS.enc.Utf8));  
      22. lt;/script>  
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;  
    
    
    public class AESUtils {  
       
      //固定16位
      private final static String KEY="abcjffg_!cdefg98";
      private final static String IV="1234123412341234";  
        
      /** 
       * aes 加密 
       * @param data 
       * @return 
       */  
      public static String encryptData(String data){  
          try {  
              Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");  
              int blockSize = cipher.getBlockSize();  
              byte[] dataBytes = data.getBytes();  
              int plaintextLength = dataBytes.length;  
              if (plaintextLength % blockSize != 0) {  
                  plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));  
              }  
              byte[] plaintext = new byte[plaintextLength];  
              System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);  
              SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), "AES");  
              IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());  
              cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);  
              byte[] encrypted = cipher.doFinal(plaintext);  
              return new String(Base64.encodeBase64(encrypted));  
          } catch (Exception e) {  
              e.printStackTrace();  
          }  
          return null;  
      }  
        
        
      /** 
       * aes 解密 
       * @param data 密文 
       * @return 
       */  
      public static String decryptData(String data){  
          try {  
              byte[] encrypted1 =Base64.decodeBase64(data.getBytes());  
              Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");  
              SecretKeySpec keyspec = new SecretKeySpec(KEY.getBytes(), "AES");  
              IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());  
              cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);  
              byte[] original = cipher.doFinal(encrypted1);  
              String originalString = new String(original);  
              return originalString;  
          } catch (Exception e) {  
              e.printStackTrace();  
          }  
          return null;  
      }  
        
        
      public static void main(String[] args) {  
          String data="172_72,72_59,272_129,168_125";  
          String enStr=AESUtils.encryptData(data);  
          System.out.println("加密:"+enStr);  
          String deStr=AESUtils.decryptData(enStr);  
          System.out.println("解密:"+deStr);  
      }  
    
    }  
  • 相关阅读:
    css 文本超过指定行数,显示省略号
    CSS 使radio和checkbox框,和文字对齐平行
    C# 使用InputStream接收 解析表单参数
    CSS 控制文本超出宽度,显示省略号
    C# 将http在线文件,保存到服务器指定位置
    C# Get请求
    C# POST请求
    js 使用XMLHttpRequest 上传文件,显示进度条
    js 验证字符长度,一个中文2个字符,英文和数字为1个字符
    iOS Carthage集成SnapKit
  • 原文地址:https://www.cnblogs.com/shihaiming/p/7683044.html
Copyright © 2020-2023  润新知