首页 > 代码库 > java,php,js;AES 互通加解密
java,php,js;AES 互通加解密
1,Java端(依赖 common-codec jar)
[java] view plain copy
- 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=http://www.mamicode.com/"php和java互通!";
- String enStr=AESUtils.encryptData(data);
- System.out.println("加密:"+enStr);
- String deStr=AESUtils.decryptData(enStr);
- System.out.println("解密:"+deStr);
- }
- }
2,php 端
[php] view plain copy
- <?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端
[javascript] view plain copy
- <script src=http://www.mamicode.com/"./crypto-js.js"></script>
- <script src=http://www.mamicode.com/"./aes.js"></script>
- <script src=http://www.mamicode.com/"./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
java,php,js;AES 互通加解密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。