在项目中,会经常用到数据的加密处理,对敏感的数据处理。我在项目中用到了记录用户名和密码的功能。在做远程登陆时使用。
DES 加密,现在还没有使用加密机。 直接上代码了。
DES 加密接口
package com.hkrt.des;
public interface Encrypt {
/**
* DES加密
* @param key 是十六进制
* @param src ASCII值
* @return ASCII 值 会有不可见字符 通常把加密后的数据转成十六进制
* @throws Exception
*/
public byte[] DesEncryptByte2(String key, byte [] src)throws Exception;//
/**
* DES解密
* @param key 是十六进制
* @param src 是十六进制
* @return
* @throws Exception
*/
public String DesDecrypt(String key, String src)throws Exception;//
}
DES 加密接口实现
package com.hkrt.des;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Des implements Encrypt {
private static final String DES = "DES"; // 定义 加密算法
private static final String CFB = "DES/ECB/NoPadding"; //
// DES解密
public String DesDecrypt(String key, String szSrc) throws Exception {
String str = "";
if (key.length() != 16) {
throw new Exception("DES密钥长度必须为十六进制的16字节");
}
byte[] srcBytes = DESdecryptMode(hexStr2ByteArr(key), hexStr2ByteArr(szSrc));
byte[] srcBytesEnd = srcBytes;
str = new String(srcBytesEnd);
return str;
}
// DES解密方法
public static byte[] DESdecryptMode(byte[] keybyte, byte[] src) {
try {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, DES);
// 解密
Cipher c1 = Cipher.getInstance(CFB);
c1.init(Cipher.DECRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
//将十六进制字符串转换成原始字节数组
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
System.out.println("---"+strIn);
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
// System.out.println(arrOut[i/2]);
}
return arrOut;
}
// DES加密 返回byte[]
public byte[] DesEncryptByte2(String key, byte [] szSrc) throws Exception {
if(szSrc.length %8 != 0){
throw new Exception("数据体的长度必须为8的倍数");
}
if (key.length() != 16) {
throw new Exception("DES密钥长度必须为十六进制的16字节");
}
byte[] encoded = DESencryptMode(hexStr2ByteArr(key), szSrc);
return encoded;
}
// DES加密方法
public byte[] DESencryptMode(byte[] keybyte, byte[] src) {
try {
// 生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, DES);
// 加密
Cipher c1 = Cipher.getInstance(CFB);
c1.init(Cipher.ENCRYPT_MODE, deskey);
return c1.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
}
DesUtil 工具类
package com.hkrt.des;
import java.util.ArrayList;
public class DesUtil {
/** 右补null */
public static byte[] fillByte(byte[] bRecv) {
int iLen;
iLen = (bRecv.length) % 8;
byte[] bSend;
if (iLen != 0) {
iLen = 8 - iLen;
byte[] bSpace = new byte[iLen];
bSend = new byte[bRecv.length + iLen];
System.arraycopy(bRecv, 0, bSend, 0, bRecv.length);
for (int i = 0; i < iLen; i++) {
// 不足位数补空格
bSpace[i] = (byte) 0;
}
System.arraycopy(bSpace, 0, bSend, bRecv.length, bSpace.length);
} else {
bSend = new byte[bRecv.length];
System.arraycopy(bRecv, 0, bSend, 0, bRecv.length);
}
return bSend;
}
/** 把ASCII 转成十六进制 */
public static String printHexString(byte[] b) {
String result = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result = result + hex.toUpperCase();
}
return result;
}
/** 将十六进制字符串转换成原始字节数组 */
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/*** 去除不可见字符( byte 是0的不可见字符) */
public static byte[] removeNoSeeChar(byte[] srcBytes) {
ArrayList<Byte> listArr = new ArrayList<Byte>();
for (int i = 0; i < srcBytes.length; i++) {
listArr.add(srcBytes[i]);
}
lableB: for (int j = listArr.size() - 1; j >= 0; j--) {
if (listArr.get(j) == 0) {
listArr.remove(listArr.get(j));
} else {
// break;
break lableB;
}
}
byte[] bs = new byte[listArr.size()];
for (int k = 0; k < listArr.size(); k++) {
bs[k] = listArr.get(k).byteValue();
}
return bs;
}
}
工厂类:
package com.hkrt.des;
public class DesFactory {
public static Encrypt getInstance(){
return new Des();
}
}
测试结果:
package com.hkrt.des;
public class DesTest {
public static void main(String[] args) {
String srcStr="456789你";
System.out.println("加密结果"+jiaMi(srcStr));
System.out.println("解密结果"+jieMi("DD543BEBCB344F9B"));
}
/**
* @param srcStr 原始数据
* @return 十六进制数据
*/
private static String jiaMi(String srcStr){
String keyStr="123456";
String key = DesUtil.printHexString(DesUtil.fillByte(keyStr.getBytes()));//给key 补位 并转成十六进制
byte[] src = DesUtil.fillByte(srcStr.getBytes());//给src 补位
byte[] result=null;
try {
result = DesFactory.getInstance().DesEncryptByte2(key, src);
} catch (Exception e) {
e.printStackTrace();
}
return DesUtil.printHexString(result);
}
/**
* @param srcStr 十六进制
* @return 真实数据
*/
private static String jieMi(String srcStr){
String keyStr="123456";
String key = DesUtil.printHexString(DesUtil.fillByte(keyStr.getBytes()));//给key 补位 并转成十六进制
String result =null;
try {
result = DesFactory.getInstance().DesDecrypt(key, srcStr);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}