嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元微信扫码支付:1 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
/**
* 加密
*
* @param datasource
* String 这里的参数是要解密string 的 hexstring,因为有些string是乱码这样子比较直观
* @param password
* String 这里的参数是是密钥string 的 hexstring
* @return String cipher.doFinal返回的是byte【】,转成hexstring 直观一点
* @throws Exception
*/
public static String encrypt(String datasource, String password)
{
String srcString = datasource;
// 位数不足,补充够正确的长度,补充字符自定义的
while ((srcString.length() % 16) != 0)
{
srcString = srcString "FF";
}
try
{
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(hexStringToByte(password));
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 现在,获取数据并加密
// 正式执行加密操作
return bytesToHexString(cipher.doFinal(hexStringToByte(srcString)));
} catch (Throwable e)
{
return e.getMessage();
}
}
/**
* 解密
*
* @param src
* String 这里的参数是要解密string 的 hexstring,因为有些string是乱码这样子比较直观
* @param password
* String 这里的参数是是密钥string 的 hexstring
* @return String cipher.doFinal返回的是byte【】,转成hexstring 直观一点
* @throws Exception
*/
public static String decrypt(String src, String password) throws Exception
{
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec(hexStringToByte(password));
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return bytesToHexString(cipher.doFinal(hexStringToByte(src)));
}