以下代碼適用於我使用BlowFish加密對字符串進行加密。在Java中使用BlowFish加密
// create a key generator based upon the Blowfish cipher
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
// create a key
SecretKey secretkey = keygenerator.generateKey();
// create a cipher based upon Blowfish
Cipher cipher = Cipher.getInstance("Blowfish");
// initialise cipher to with secret key
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
// get the text to encrypt
String inputText = "MyTextToEncrypt";
// encrypt message
byte[] encrypted = cipher.doFinal(inputText.getBytes());
如果我想定義我自己的密鑰,我該怎麼做?
非常感謝了迅速的回答! – StefanE 2011-03-09 11:42:49
@Erik也看看我的問題http://stackoverflow.com/questions/38007478/encrypt-and-decrypt-string-using-chacha20 – Nepster 2016-06-24 10:19:30
該解決方案是不安全的,因爲它直接使用字符串作爲加密密鑰。 – Robert 2016-07-21 07:15:42