JAVA后台数据加密
程序员文章站
2022-03-12 22:25:36
...
给一个简单的加密类,使用方便快捷。当然加密的key非常规律,如果想改可以修改key变为复杂的key。
撸代码先:首先由一个加密类DES.class
需要导入的类如下:
import java.nio.charset.Charset;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
类及方法:
public class DES
{
//中文也是可以加密的,所以规定解密后的字符编码为UTF-8.
private static String DEFAULT_CHARSET = "UTF-8";
/**
*
* @Title: encript
* @Description: DES加密
* @param @param org
* @return byte[] 返回类型
* @throws
*/
public String encript(String org)
{
byte[] destBytes = null;
byte[] orgBytes = org.getBytes(Charset.forName(DEFAULT_CHARSET));
try
{
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(this.getKey());
SecretKeyFactory keyFactory;
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher ciper = Cipher.getInstance("DES/CBC/NOPadding");
Cipher ciper = Cipher.getInstance("DES");
// ciper.init(Cipher.ENCRYPT_MODE, securekey, new
// IvParameterSpec(this
// .getIV()));
ciper.init(Cipher.ENCRYPT_MODE, securekey, sr);
destBytes = ciper.doFinal(orgBytes);
}
catch(Exception e)
{
e.printStackTrace();
}
String dest = bytesToHexString(destBytes);
return dest;
}
/**
*
* @Title: decript
* @Description: DES解密
* @param @param org
* @return String 返回类型
* @throws
*/
public String decript(String org)
{
byte[] destBytes = null;
byte[] orgBytes = hexStringToBytes(org);
try
{
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(this.getKey());
SecretKeyFactory keyFactory;
keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher ciper = Cipher.getInstance("DES/CBC/NOPadding");
Cipher ciper = Cipher.getInstance("DES");
// ciper.init(Cipher.ENCRYPT_MODE, securekey, new
// IvParameterSpec(this
// .getIV()));
ciper.init(Cipher.DECRYPT_MODE, securekey, sr);
destBytes = ciper.doFinal(orgBytes);
}
catch(Exception e)
{
e.printStackTrace();
}
String dest = new String(destBytes, Charset.forName(DEFAULT_CHARSET));
return dest;
}
/**
*
* @Title: getKey
* @Description: 获取**(16位)
* @return byte[] 返回类型
* @throws
*/
private byte[] getKey()
{
// 固定**
String key = "12345678abcdefgh";
return key.getBytes(Charset.forName(DEFAULT_CHARSET));
}
/**
*
* @Title: getIV
* @Description: 获取初始向量(8位),取key的前8位,不足补0
* @return byte[] 返回类型
* @throws
*/
public byte[] getIV()
{
byte[] key = this.getKey();
byte[] iv = new byte[8];
System.arraycopy(key, 0, iv, 0, 8);
System.out.println("iv :" + iv);
return iv;
}
/**
*
* @Title: bytesToHexString
* @Description: byte[]转十六进制字符串
* @param @param src
* @return String 返回类型
* @throws
*/
public String bytesToHexString(byte[] src)
{
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0)
{
return null;
}
for (int i = 0; i < src.length; i++)
{
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2)
{
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
*
* @Title: hexStringToBytes
* @Description: 十六进制字符串转byte[]
* @param @param hexString
* @return byte[] 返回类型
* @throws
*/
public byte[] hexStringToBytes(String hexString)
{
if (hexString == null || hexString.equals(""))
{
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] dest = new byte[length];
for (int i = 0; i < length; i++)
{
int pos = i * 2;
dest[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return dest;
}
/**
*
* @Title: charToByte
* @Description: char转byte
* @param @param c
* @return byte 返回类型
* @throws
*/
private byte charToByte(char c)
{
int index = "0123456789ABCDEF".indexOf(c);
if (index == -1)
{
index = "0123456789abcdef".indexOf(c);
}
return (byte) index;
}
/**
*
* @Title: printHexString
* @Description: 将指定byte数组以16进制的形式打印到控制台
* @return void 返回类型
* @throws
*/
public void printHexString(byte[] bytes)
{
for (int i = 0; i < bytes.length; i++)
{
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1)
{
hex = '0' + hex;
}
System.out.print(hex.toUpperCase());
}
}
public static void main(String arg[])
{
DES des = new DES();
String testOrg = "我是中国人!123dfasdf ";
// DES 加密
String encript = des.encript(testOrg);
System.out.println("encript data: " + encript);
// DES 解密
String decript = des.decript(encript);
System.out.println("decript data: " + decript);
// Base64 加密
byte[] base64Encript = Base64.encode(testOrg.getBytes(Charset
.forName(DEFAULT_CHARSET)), Base64.DEFAULT);
System.out.println("base64 encript:"
+ new String(base64Encript, Charset.forName(DEFAULT_CHARSET)));
// Base64 解密
byte[] base64Decript = Base64.decode(base64Encript, Base64.DEFAULT);
System.out.println("base64 decript:"
+ new String(base64Decript, Charset.forName(DEFAULT_CHARSET)));
}
}