在Java中使用BlowFish进行加密

以下代码对我来说可以用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());

如果我想定义自己的密钥,该怎么做?

回答:

String Key = "Something";

byte[] KeyData = Key.getBytes();

SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");

Cipher cipher = Cipher.getInstance("Blowfish");

cipher.init(Cipher.ENCRYPT_MODE, KS);

以上是 在Java中使用BlowFish进行加密 的全部内容, 来源链接: utcz.com/qa/420954.html

回到顶部