java,php,js;AES 互通加解密
1,Java端(依赖 common-codec jar)
- package com.jiaMi;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import org.apache.commons.codec.binary.Base64;
- public class AESUtils {
- private final static String KEY="1234123412341324";
- 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="php和java互通!";
- String enStr=AESUtils.encryptData(data);
- System.out.println("加密:"+enStr);
- String deStr=AESUtils.decryptData(enStr);
- System.out.println("解密:"+deStr);
- }
- }
2,php 端
- <?php
- $privateKey = "1234123412341324";
- $iv = "1234123412341324";
- $data = "测试用的数据";
- //加密
- $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $privateKey, $data, MCRYPT_MODE_CBC, $iv);
- echo(base64_encode($encrypted));
- echo '<br/>';
- //解密
- $encryptedData = base64_decode(base64_encode($encrypted));
- $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $iv);
- echo($decrypted);
- ?>
3,js端
- <script src="./crypto-js.js"></script>
- <script src="./aes.js"></script>
- <script src="./pad-zeropadding.js"></script>
- <script>
- var data = "测试用的数据";
- var key = CryptoJS.enc.Latin1.parse('1234123412341324');
- var iv = CryptoJS.enc.Latin1.parse('1234123412341324');
- //加密
- var encrypted = CryptoJS.AES.encrypt(data,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding});
- //alert(encrypted);
- console.log(encrypted.toString());
- //解密
- var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,padding:CryptoJS.pad.ZeroPadding});
- console.log(decrypted.toString(CryptoJS.enc.Utf8));
- //alert(decrypted.toString(CryptoJS.enc.Utf8));
- lt;/script>
三个js文件下载地址: http://download.csdn.net/detail/wd4871/9526147
学习时的痛苦是暂时的 未学到的痛苦是终生的