Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输出Rijndael执行中的异常_Java_Encryption_Bouncycastle_Rijndael_Block Cipher - Fatal编程技术网

Java 输出Rijndael执行中的异常

Java 输出Rijndael执行中的异常,java,encryption,bouncycastle,rijndael,block-cipher,Java,Encryption,Bouncycastle,Rijndael,Block Cipher,我在使用BouncyCastleAPI for Java实现Rijndael加密时遇到问题 当我执行cipher.doFinal(inputTextBytes,intOutOff)时,我得到了outputLengtheexception: org.bouncycastle.crypto.outputLength异常:输出缓冲区太短 我不完全理解如何生成该整数来执行doFinal()方法 以下是我尝试过的: public class RijndaelAndRFC2899Implementation

我在使用BouncyCastleAPI for Java实现Rijndael加密时遇到问题

当我执行
cipher.doFinal(inputTextBytes,intOutOff)时,我得到了
outputLengtheexception

org.bouncycastle.crypto.outputLength异常:输出缓冲区太短

我不完全理解如何生成该整数来执行
doFinal()
方法

以下是我尝试过的:

public class RijndaelAndRFC2899Implementation {

    final static String WORD = "763059";
    final static String PASSWORD = "515t3ma5m15B4d35";
    final static byte[] SALT = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
    final static int KEY_SIZE = 256;
    final static int BLOCK_SIZE = 128;
    final static int ITERATIONS = 1000;

    public static void main(String[] args) throws Exception {
        BufferedBlockCipher cipher = getCipher(PASSWORD, true);
        byte[] inputText = WORD.getBytes("UTF-8");
        byte asd[] = new byte[cipher.getOutputSize(inputText.length)];
        int l = cipher.processBytes(inputText, 0, inputText.length, asd, 0);
        int n = cipher.doFinal(inputText, l); //<---HERE PRODUCES OutputLengthException
    }

    private static BufferedBlockCipher getCipher(String password, boolean encrypt) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(password.getBytes("UTF-8"));
        byte[] newPassword = md.digest();

        PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();
        generator.init(newPassword, SALT, ITERATIONS);

        ParametersWithIV iv = ((ParametersWithIV) generator.generateDerivedParameters(KEY_SIZE, BLOCK_SIZE));

        RijndaelEngine engine = new RijndaelEngine();
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));   
        cipher.init(encrypt, iv);

        return cipher;
    }

}
公共类RijndaelandRFC2899实现{
最终静态字符串WORD=“763059”;
最终静态字符串密码=“515t3ma5m15B4d35”;
最后一个静态字节[]SALT=新字节[]{1,2,3,4,5,6,7,8};
最终静态整数键大小=256;
最终静态整型块大小=128;
最终静态整数迭代=1000;
公共静态void main(字符串[]args)引发异常{
BufferedBlockCipher cipher=getCipher(密码,true);
字节[]inputText=WORD.getBytes(“UTF-8”);
字节asd[]=新字节[cipher.getOutputSize(inputText.length)];
int l=cipher.processBytes(inputText,0,inputText.length,asd,0);
int n=cipher.doFinal(inputText,l);//调用
doFinal()
应该将输出数组作为第一个参数-而不是您正在处理的输入:

public static void main(String[] args) throws Exception {
    BufferedBlockCipher cipher = getCipher(PASSWORD, true);
    byte[] inputText = WORD.getBytes("UTF-8");
    byte asd[] = new byte[cipher.getOutputSize(inputText.length)];
    int l = cipher.processBytes(inputText, 0, inputText.length, asd, 0);
    int n = cipher.doFinal(asd, l); // <--- Change to asd
}
publicstaticvoidmain(字符串[]args)引发异常{
BufferedBlockCipher cipher=getCipher(密码,true);
字节[]inputText=WORD.getBytes(“UTF-8”);
字节asd[]=新字节[cipher.getOutputSize(inputText.length)];
int l=cipher.processBytes(inputText,0,inputText.length,asd,0);
int n=cipher.doFinal(asd,l);//对
doFinal()
的调用应该将输出数组作为第一个参数,而不是您正在处理的输入:

public static void main(String[] args) throws Exception {
    BufferedBlockCipher cipher = getCipher(PASSWORD, true);
    byte[] inputText = WORD.getBytes("UTF-8");
    byte asd[] = new byte[cipher.getOutputSize(inputText.length)];
    int l = cipher.processBytes(inputText, 0, inputText.length, asd, 0);
    int n = cipher.doFinal(asd, l); // <--- Change to asd
}
publicstaticvoidmain(字符串[]args)引发异常{
BufferedBlockCipher cipher=getCipher(密码,true);
字节[]inputText=WORD.getBytes(“UTF-8”);
字节asd[]=新字节[cipher.getOutputSize(inputText.length)];
int l=cipher.processBytes(inputText,0,inputText.length,asd,0);

int n=cipher.doFinal(asd,l);//还要注意doFinal的返回值('n');根据密码算法,getOutputSize可能高估了实际输出大小,因此输出可能没有完全填满输出缓冲区('asd')。返回值是增量输出长度,因此您可能还需要“int n=l+cipher.doFinal(asd,l);”相反。还要注意doFinal的返回值('n');根据密码算法,getOutputSize可能高估了实际输出大小,因此输出可能没有完全填满输出缓冲区('asd'))。返回值是增量输出长度,因此您可能还希望使用“int n=l+cipher.doFinal(asd,l);”代替。