资源简介
端对端加密,实现前端Js加密传输后台java解密;后台java加密后返回前端再js解密。
代码片段和文件信息
package com.dhcc.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.base64Decoder;
import sun.misc.base64Encoder;
/**
* des加密解密
*
* @author
*
*/
public class DesEncrypt {
Key key;
public DesEncrypt(String str) {
setKey(str);// 生成密匙
}
public DesEncrypt() {
setKey(“abc123.*abc123.*abc123.*abc123.*“);
}
/**
* 根据参数生成KEY
*/
public void setKey(String strKey) {
try {
//对比DES
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(“DESede“);
this.key = keyFactory.generateSecret(new DESedeKeySpec(strKey.getBytes(“UTF8“)));
} catch (Exception e) {
throw new RuntimeException(
“Error initializing SqlMap class. Cause: “ + e);
}
}
/**
* 加密String明文输入String密文输出
*/
public String encrypt(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = ““;
base64Encoder base64en = new base64Encoder();
try {
byteMing = strMing.getBytes(“UTF8“);
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
throw new RuntimeException(
“Error initializing SqlMap class. Cause: “ + e);
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}
/**
* 解密 以String密文输入String明文输出
*
* @param strMi
* @return
*/
public String decrypt(String strMi) {
base64Decoder base64De = new base64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = ““;
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing “UTF8“);
} catch (Exception e) {
throw new RuntimeException(
“Error initializing SqlMap class. Cause: “ + e);
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}
/**
* 加密以byte[]明文输入byte[]密文输出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {//对比DES
cipher = Cipher.getInstance(“DESede“);
cipher.init(Cipher.ENCRYPT_MODE keySecureRandom.getInstance(“SHA1PRNG“));
byt
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-15 09:28 端对端加密(Js+Java)\
文件 4577 2017-12-07 16:56 端对端加密(Js+Java)\DesEncrypt.java
文件 2746 2017-12-15 09:45 端对端加密(Js+Java)\index.html
文件 92556 2017-08-25 17:08 端对端加密(Js+Java)\jquery-1.8.0.min.js
文件 392 2017-12-14 21:41 端对端加密(Js+Java)\mode-ecb-min.js
文件 21399 2017-12-14 21:41 端对端加密(Js+Java)\tripledes.js
- 上一篇:commons-lang3-3.9.jar
- 下一篇:JAVA宿舍管理系统
评论
共有 条评论