62,635
社区成员




public static void main(String[] args) throws Exception {
String mykey="key1";
String text = "this is the secret msg";
SecretKeySpec key = new SecretKeySpec(mykey.getBytes("UTF-8"), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); //NoPadding");
if ( cipher == null || key == null) {
throw new Exception("Invalid key or cypher");
}
// encrypt
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
// decrypt
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainBytes = cipher.doFinal(encrypted);
String text2 = new String(plainBytes, "UTF-8");
System.out.println(text2);
}