前端加密
前端使用开源CryptoJS (crypto.js) 为 JavaScript加密算法,Github地址(https://github.com/brix/crypto-js)。目前已支持的算法包括:
MD5、SHA-1、SHA-256、AES、Rabbit、MARC4、HMAC、HMAC-MD5、HMAC-SHA1、HMAC-SHA256、PBKDF2
下载crypto-js.js,引入到你的HTML文件中,我试验了以上算法中的四种算法,代码如下:
代码语言:javascript复制//AES-128-CBC加密模式,key需要为16位,key和iv可以一样
function encryptAES(data) {
var key = CryptoJS.enc.Utf8.parse('123456789abcdefg');
var iv = CryptoJS.enc.Utf8.parse('123456789abcdefg');
return CryptoJS.AES.encrypt(data, key, {iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}).toString();
}
//DES加密算法
function encryptByDES(message) {
var keyHex = CryptoJS.enc.Utf8.parse("bs7dff53");
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
//MD5加密
var encryPass = CryptoJS.MD5(password).toString();
//SHA1 加密
var encryPass =CryptoJS.SHA1(password).toString();
为了实现前端加密的算法和后端算法一致,我使用Java编写了后端算法。代码如下:
代码语言:javascript复制//使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同!
private static String aesKEY = "123456789abcdefg";
private static String aesIV = "123456789abcdefg";
//DES加密的key 长度为八位
private static String desKey = "bs7dff53";
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
private static String toHexString(byte[] b) {
StringBuilder sb = new StringBuilder(b.length * 2);
for (int i = 0; i < b.length; i ) {
sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
sb.append(HEX_DIGITS[b[i] & 0x0f]);
}
return sb.toString();
}
/**
* MD5加密算法
* @param SourceString
* @return
* @throws NoSuchAlgorithmException
*/
public static String Bit32(String SourceString) throws NoSuchAlgorithmException {
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(SourceString.getBytes());
byte messageDigest[] = digest.digest();
return toHexString(messageDigest);
}
public static String Bit16(String SourceString) throws NoSuchAlgorithmException {
return Bit32(SourceString).substring(8, 24);
}
/**
* SHA1算法进行加密
* @param decript
* @return
* @throws Exception
*/
public static String getSHA1(String decript)throws Exception
{
MessageDigest digest = MessageDigest
.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i )
{
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
}
/**
* 加密方法
* @param data 要加密的数据
* @param key 加密key
* @param iv 加密iv
* @return 加密的结果
* @throws Exception
*/
public static String encryptAES(String data) throws Exception {
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(aesKEY.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(aesIV.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new Base64().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 解密方法
* @param data 要解密的数据
* @param key 解密key
* @param iv 解密iv
* @return 解密的结果
* @throws Exception
*/
public static String desEncryptAES(String data) throws Exception {
try {
byte[] encrypted1 = new Base64().decode(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec(aesKEY.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(aesIV.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;
}
}
/**
* DES加密
* @param datasource
* @return
*/
public static String encryptDES(String datasource){
try{
SecureRandom random = new SecureRandom();
DESKeySpec desKeys = new DESKeySpec(desKey.getBytes("UTF-8"));
//创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKeys);
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES");
//用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
//现在,获取数据并加密
byte[] temp = Base64.encodeBase64(cipher.doFinal(datasource.getBytes()));
return IOUtils.toString(temp,"UTF-8");
}catch(Throwable e){
e.printStackTrace();
return null;
}
}
/**
* DES解密
* @return
*/
public static String decodeDES(String data) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKeys = new DESKeySpec(desKey.getBytes("UTF-8"));
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKeys);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return IOUtils.toString(cipher.doFinal(Base64.decodeBase64(data)),"UTF-8");
}