加密解密

xiaoxiao2021-02-27  365

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); }

//

转载请注明原文地址: https://www.6miu.com/read-2913.html

最新回复(0)