没有最大尺寸,但我也想建议不同的方法
加密
和
解密
public static String encrypt(String strClearText,String strKey) throws Exception{
String strData="";
try {
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
String isoText = new String(strClearText.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); // there are shorthand ways of doing this, but this is for your explaination
byte[] encrypted=cipher.doFinal(isoText.getBytes(StandardCharsets.ISO_8859_1));
strData=Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
return strData;
}
解密
public static String decrypt(String strEncrypted,String strKey) throws Exception{
String strData="";
try {
SecretKeySpec skeyspec=new SecretKeySpec(strKey.getBytes(),"Blowfish");
Cipher cipher=Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, skeyspec);
byte[] decrypted=cipher.doFinal(Base64.getDecoder().decode(strEncrypted));
isoStrData=new String(decrypted, StandardCharsets.ISO_8859_1);
strData=new String(isoStrData.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
}
return strData;
}
在程序中,键始终可以是常量,但不建议这样做。