zoukankan      html  css  js  c++  java
  • java,php,js;AES 互通加解密

    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>  



    三个js文件下载地址: http://download.csdn.net/detail/wd4871/9526147

  • 相关阅读:
    CodeSmith的OracleProviders
    使用 Web Services Enhancements 2.0 的基于角色的安全性
    用WSE在Web服务中验证用户身份(3)
    使用 Web Services Enhancements 2.0 进行编程
    使用 Web Services Enhancements 发送带有附件的 SOAP 消息
    用WSE在Web服务中验证用户身份(1)
    Web Services Enhancements 管道技术内幕
    确保应用程序服务器的安全
    基于图象光照问题提出(看文章的记录一)
    用WSE在Web服务中验证用户身份(2)
  • 原文地址:https://www.cnblogs.com/grimm/p/7233158.html
Copyright © 2011-2022 走看看