加完後 能夠到 http://tool.chacuo.net/cryptdes 在線DES加密 解密 進行測試javascript
若是 出現 異常 javax.crypto.IllegalBlockSizeException html
那就看我博客園的 另一篇 解決這個異常java
幫客戶整了1天 試了N個JS 的方法 終於找我弄出來了!jquery
java部分:算法
package com.util;api
import java.security.Key;app
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;測試
// 算法名稱/加密模式/填充方式
public static final String CIPHER_ALGORITHM_ECB = "DES/ECB/ZeroBytePadding"; ui
//主要看填充方式 PKCS5Padding PKCS7Padding 還有其餘的填充方式沒用過加密
// public static final String CIPHER_ALGORITHM_CBC = "DES/CBC/ZerosPadding";
public static void main(String[] args) throws Exception {
/*
* 使用 ECB mode
* 密鑰生成器 生成密鑰
* ECB mode cannot use IV
*/
byte[] key = "DAF3100DCD39CAEB5DD35E9651712A86".getBytes();
byte[] encrypt = encrypt("1212121211221",key);
String encodeBase64String = Base64.encodeBase64String(encrypt);
System.out.println(encodeBase64String);
System.out.println(new String(decrypt(encrypt, key)));
}
static byte[] getIV() {
String iv = "asdfivh7"; //IV length: must be 8 bytes long
return iv.getBytes();
}
/**
* 生成密鑰
*
* @return
* @throws Exception
*/
private static byte[] generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(56); //des 必須是56, 此初始方法沒必要須調用
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
/**
* 還原密鑰
*
* @param key
* @return
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
DESKeySpec des = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(des);
return secretKey;
}
/**
* 加密
* @param data 原文
* @param key
* @return 密文
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
cipher.init(Cipher.ENCRYPT_MODE, k);
byte[] doFinal = cipher.doFinal(data);
return cipher.doFinal(data);
}
/**
* 解密
* @param data 密文
* @param key
* @return 明文、原文
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
}
Html部分
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>JS設置DES加密處理</title> <script type="text/javascript" src="C:\Users\Administrator\Desktop\js\jquery.min.js"></script> <script src="C:\Users\Administrator\Desktop\js\rollups\tripledes.js"></script> <script src="C:\Users\Administrator\Desktop\js\components\mode-ecb.js"></script> <script> //DES 解密 加密 function encryptByDES(message, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); var encrypted = CryptoJS.DES.encrypt(message, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Zero }); return encrypted.toString(); } //DES 解密 function decryptByDES(ciphertext, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); // direct decrypt ciphertext var decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(ciphertext) }, keyHex, { mode: CryptoJS.mode.ECB, //這一步 是來填寫 加密時候填充方式 padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); } </script> <script> //加密 function encryptStr() { var strKey = $.trim($('#key').val()); var strMsg = $.trim($('#text1').val()); $('#text2').val(encryptByDES(strMsg, strKey)); } //解密 function decryptStr() { var strKey = $.trim($('#key').val()); var ciphertext = $.trim($('#text2').val()); $('#text3').val(decryptByDES(ciphertext, strKey)); } </script> </head> <body> <h1>JS設置DES加密處理</h1> <label>key</label> <input type="text" value='12345678' id="key" /> <div> <textarea id="text1" placeholder="請輸入須要加密的字符">abcde12345這個中文!@#!@$#%$#^%(":''")[]=_-</textarea> <input type="button" value="加密" onclick="encryptStr();" /> <textarea id="text2"></textarea> <input type="button" value="解密" onclick="decryptStr();" /> <textarea id="text3"></textarea> </div> </body> </html>
http://files.cnblogs.com/files/liuJava/DESjs%E5%8A%A0%E5%AF%86%E5%92%8CJava%E4%BA%92%E9%80%9A.zip 全部的包 和 html例子