Javascript的加解密有開源的庫,http://www.oschina.net/p/crypto-js/javascript
以下是具體的使用例子html
<!DOCTYPE html>前端
<html lang="zh-cn">java
<head>git
<meta charset="utf-8"/>github
<meta name="viewport" content="width=device-width, initial-scale=1" />typescript
<script src="aes_1.js"></script> //引入的js文件在該連接中:https://github.com/hellobajie/AES-of-JavaScriptapache
</head>後端
<body>加密
<script type="text/javascript" >
//16位
var pwd="abcd2016_-=00_X_";
function Decrypt(word){
var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
console.log(srcs);
var decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});
var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
function Encrypt(word){
var key = CryptoJS.enc.Utf8.parse(pwd);
var iv = CryptoJS.enc.Utf8.parse(pwd);
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7});
srcs = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
return srcs ;
}
var mm = Encrypt('DEMO');
console.log(mm)
var jm = Decrypt(mm);
console.log(jm)
</script>
若是使用typescript能夠在這找到其和ts集成的定義文件
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.util.Arrays;
String pwd = "abcd2016_-=00_X_";
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = pwd.getBytes("utf-8");
SecretKey secretKey = new SecretKeySpec(raw, "AES");
System.out.println("密鑰的長度爲:" + secretKey.getEncoded().length);
Base64 b64 = new Base64();
encrypt = b64.decode(b64.encodeToString("DEMO"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(raw));//使用解密模式初始化 密鑰
byte[] decrypt = cipher.doFinal(encrypt);
System.out.println("解密後:" + new String(decrypt));
若是,能夠在前端和後端進行各類加密的處理,或把加密的信息在先後端之間交換.