Java-通过BlowFish密码进行2字节字符加密

Java-通过BlowFish密码进行2字节字符加密,java,encryption,character,blowfish,Java,Encryption,Character,Blowfish,我正在尝试开发对称加密软件,它提供2字节字符编码 但是,下面的代码无法加密2字节字符,我不知道为什么 (该代码可正确使用英语) package com.messagesecretpro; 进口gnu.crypto.cipher.Blowfish; 导入gnu.crypto.util.Base64; 导入java.io.*; 导入java.lang.reflect.Array; 公共类密码{ 公共静态字符串加密(字符串值、字符串密钥){ 字节[]明文; 字节[]加密文本; 河豚河豚=新河豚();

我正在尝试开发对称加密软件,它提供2字节字符编码

但是,下面的代码无法加密2字节字符,我不知道为什么

(该代码可正确使用英语)


package com.messagesecretpro;
进口gnu.crypto.cipher.Blowfish;
导入gnu.crypto.util.Base64;
导入java.io.*;
导入java.lang.reflect.Array;
公共类密码{
公共静态字符串加密(字符串值、字符串密钥){
字节[]明文;
字节[]加密文本;
河豚河豚=新河豚();
//创建密钥
//String key=“zzforexamplekeytext”;
byte[]keyBytes=key.getBytes();
objectkeyObject=blowfish.makeKey(keyBytes,8);
//使文本长度为块大小的倍数
如果((cookieValue.length()%8)!=0){
而((cookieValue.length()%8)!=0){
cookieValue+=“”;
}
}
//初始化纯文本/加密文本的字节数组
明文=cookieValue.getBytes();
encryptedText=新字节[cookieValue.length()];
//以8字节块加密文本
for(int i=0;i
您应该阅读文档;Blowfish类实现了一个分组密码,而不是一个或多个。因此,您只能对数据块(8字节)使用给定的加密方法

您正在尝试迭代(使用for next循环)加密每个字节(或当前重叠的块)。但是,如果
encrypt
方法的输入不包含要加密的完整块(从给定偏移量开始),则会出现索引越界异常

因此:

  • 使用分组密码,使用良好的操作模式,并在操作模式需要时使用填充
  • 不要使用早已废弃的加密库或算法

  • 该算法的作者布鲁斯·施奈尔(Bruce Schneier)早就建议不要使用这种密码。虽然它仍然被认为是安全的,但8字节的块大小使它在大多数操作模式下相对不安全


    您最好在经过身份验证的操作模式(如GCM)中使用AES,使用唯一的nonce/密钥组合。通常,最好使用更高级别的协议/容器格式,这样您就不必(过度)依赖自己来确保密文的安全。

    您真的要发布没有缩进的代码吗?你想蒙蔽我们吗?谢谢你的修改GregS@GregS也就是说,导入源代码也是如此。如果没有导入代码的好方法,它就可以存在,这有点令人震惊。
    package com.messagesecretpro;
    import gnu.crypto.cipher.Blowfish;
    import gnu.crypto.util.Base64;
    import java.io.*;
    import java.lang.reflect.Array;
    
    public class BlowfishCipher {
        public static String encrypt(String cookieValue, String key) {
            byte[] plainText;
            byte[] encryptedText;
    
            Blowfish blowfish = new Blowfish();
            // create a key
            // String key = "zzforexamplekeytext";
            byte[] keyBytes = key.getBytes();
            Object keyObject = blowfish.makeKey(keyBytes, 8);
    
            // make the length of the text a multiple of the block size
            if ((cookieValue.length() % 8) != 0) {
                while ((cookieValue.length() % 8) != 0) {
                    cookieValue += " ";
                }
            }
    
            // initialize byte arrays for plain/encrypted text
            plainText = cookieValue.getBytes();
            encryptedText = new byte[cookieValue.length()];
    
            // encrypt text in 8-byte chunks
            for (int i = 0; i < Array.getLength(plainText); i += 8) {
                blowfish.encrypt(plainText, i, encryptedText, i, keyObject, 8);
            }
            String encryptedString = Base64.encode(encryptedText);
            return encryptedString;
        }
    
        public static String decrypt(String cookieValue, String key)
                throws UnsupportedEncodingException {
    
            byte[] encryptedText;
            byte[] decryptedText;
            Blowfish blowfish = new Blowfish();
    
            // create the key
            // String key = "zzforexamplekeytext";
            byte[] keyBytes = key.getBytes();
            Object keyObject = blowfish.makeKey(keyBytes, 8);
    
            // make the length of the string a multiple of
            // the block size
            if ((cookieValue.length() % 8) != 0) {
                while ((cookieValue.length() % 8) != 0) {
                    cookieValue += " ";
                }
            }
    
            // initialize byte arrays that will hold encrypted/decrypted
    
            encryptedText = Base64.decode(cookieValue);
            decryptedText = new byte[cookieValue.length()];
    
            // Iterate over the byte arrays by 8-byte blocks and decrypt.
            for (int i = 0; i < Array.getLength(encryptedText); i += 8) {
                blowfish.decrypt(encryptedText, i, decryptedText, i, keyObject, 8);
            }
    
            String decryptedString = new String(decryptedText);
            return decryptedString;
        }
    }