在Java中使用BlowFish进行加密

前端之家收集整理的这篇文章主要介绍了在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);
原文链接:https://www.f2er.com/java/124679.html

猜你在找的Java相关文章