如何在Java中解码文本密码Blowfish CFB?

如何在Java中解码文本密码Blowfish CFB?,java,decode,blowfish,Java,Decode,Blowfish,我无法正确地解码文本。代码如下: public void decode() { byte[] KeyData = {0x2f, 0x18, (byte) 0xf5, (byte) 0x96, (byte) 0xa2, 0x17, 0x18, 0x29}; byte[] IV = {0x01, 0x3d, (byte) 0xcf, (byte) 0xe2, (byte) 0xe3, (byte) 0xf9, (byte) 0xdd, (byte) 0x81}; Secret

我无法正确地解码文本。代码如下:

public void decode() {
    byte[] KeyData = {0x2f, 0x18, (byte) 0xf5, (byte) 0x96, (byte) 0xa2, 0x17, 0x18, 0x29};
    byte[] IV = {0x01, 0x3d, (byte) 0xcf, (byte) 0xe2, (byte) 0xe3, (byte) 0xf9, (byte) 0xdd, (byte) 0x81};
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("Blowfish/CFB/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }

    byte[] ciphertext = hexToBytes("424749c1d3b497");

    // Decrypt
    try {
        cipher.init(Cipher.DECRYPT_MODE, KS, new javax.crypto.spec.IvParameterSpec(IV));
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    byte[] message = new byte[0];
    try {
        message = cipher.doFinal(ciphertext);
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    System.out.println("-- Decrypted -----------");
    System.out.println("HEX:\t " + bytesToHex(message));
    System.out.println("PLAIN:\t " + new String(message));

}

public byte[] hexToBytes(String str) {
    if (str == null) {
        return null;
    } else if (str.length() < 2) {
        return null;
    } else {
        int len = str.length() / 2;
        byte[] buffer = new byte[len];
        for (int i = 0; i < len; i++) {
            buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
        }
        return buffer;
    }

}

public  String bytesToHex(byte[] bytes) {

    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}
公共无效解码(){
byte[]KeyData={0x2f,0x18,(byte)0xf5,(byte)0x96,(byte)0xa2,0x17,0x18,0x29};
字节[]IV={0x01,0x3d,(字节)0xcf,(字节)0xe2,(字节)0xe3,(字节)0xf9,(字节)0xdd,(字节)0x81};
SecretKeySpec KS=新SecretKeySpec(KeyData,“河豚”);
密码=空;
试一试{
cipher=cipher.getInstance(“Blowfish/CFB/NoPadding”);
}捕获(无算法异常){
e、 printStackTrace();
}捕获(无此填充例外){
e、 printStackTrace();
}
字节[]密文=十六字节(“424749c1d3b497”);
//解密
试一试{
init(cipher.DECRYPT_模式,KS,新javax.crypto.spec.IvParameterSpec(IV));
}捕获(InvalidKeyException e){
e、 printStackTrace();
}捕获(无效算法参数异常e){
e、 printStackTrace();
}
字节[]消息=新字节[0];
试一试{
message=cipher.doFinal(密文);
}捕获(非法块大小异常e){
e、 printStackTrace();
}捕获(BadPaddingException e){
e、 printStackTrace();
}
System.out.println(“--Decrypted------”;
System.out.println(“十六进制:\t”+bytesToHex(消息));
System.out.println(“普通:\t”+新字符串(消息));
}
公共字节[]十六字节(字符串str){
如果(str==null){
返回null;
}else if(str.length()<2){
返回null;
}否则{
int len=str.length()/2;
字节[]缓冲区=新字节[len];
对于(int i=0;i>>4];
hexChars[j*2+1]=hexArray[v&0x0F];
}
返回新字符串(hexChars);
}
我的结果是:
HEX:37F8D5CEE036CD
PLAIN:7����6.�

正确结果:
HEX:373630353633
PLAIN:7605563

在此处检查正确的结果:


我更改了算法名称和模式

cipher = Cipher.getInstance("Blowfish/CFB8/NOPADDING");
它帮助了我