加密解密

originalString 解密载体(解密成功originalString为解密后字符串,解密失败则为null)

可以运用时间戳在访问的时候加密  一段密文只能解析一段

public static String decryptData(Object input,String password){
String originalString = null;
try {
byte[] base64 = Base64.getDecoder().decode(input.toString().getBytes("UTF-8"));
SecretKeyFactory factory = null;
SecretKey tmp = null;
SecretKey secret = null;
factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), mSalt, ITERATIONS, KEYLEN_BITS);
tmp = factory.generateSecret(spec);
secret = new SecretKeySpec(tmp.getEncoded(), "AES");
mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
String A = "0123456789ABCDEF";
mInitVec = A.getBytes("UTF-8");
mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));
String base642 = new String(base64, Charset.forName("UTF-8"));
byte[] dbyt = mDecipher.doFinal(Base64.getDecoder().decode(base642));
originalString = new String(dbyt);
//Db("decode :" +  originalString);
} catch (Exception e) {
originalString = null;
logger.warn(e.getMessage());
}
return originalString;
}

/**
* @描述: 查询密文是否存在若不在则解密入库如果密文存在则返回null
* @author thssliuyong
* @param input解密前内容
* @param password秘钥
* @return originalString 解密后字符串
*/
public static String decrypt(Object input,String password){
String originalString = null;
long searchResult = -2;
try {
searchResult = serachBody(input.toString());
if(searchResult<0){
return originalString;
}
originalString = decryptData(input, password);
} catch (Exception e) {
originalString = null;
logger.warn(e.getMessage());
}

return originalString;
}

/**
* @描述:加密入库 
* @author thssliuyong
* @param conn
* @param body 加密后密文
* @throws SQLException 
*/
private static long insertDataBase(Connection conn,String body) throws SQLException{
Dao.Tables.xyd_log xyd_log = new Dao().new Tables().new xyd_log();
xyd_log.body.setValue(body);
xyd_log.create_time.setValue(new Date());
return xyd_log.insert(conn);
}

//


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值