java 河豚算法调用
一种算是安全好用的加密算法,不多说直接上代码
加密:
String strKey ="hetun"
public static String encryptBlowFish(String strClearText) throws Exception{
String strData="";
try {
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
byte[] encrypted=cipher.doFinal(strClearText.getBytes());
strData = new String(Hex.encodeHex(encrypted));
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
return strData;
}
解密:
/**
* Blowfish解密
* @param strEncrypted
* @return
* @throws Exception
*/
public static String decryptBlowFish(String strEncrypted) throws Exception{
String strData="";
try {
byte[] encrypted = Hex.decodeHex(strEncrypted.toCharArray());
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeyspec);
byte[] decrypted=cipher.doFinal(encrypted);
strData=new String(decrypted);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
return strData;
}