为了账号安全,请及时绑定邮箱和手机立即绑定

OutputLengthException in Rijndael impletation

OutputLengthException in Rijndael impletation

达令说 2022-09-07 21:49:33
我在使用BouncyCastle API for Java的Rijndael加密实现时遇到问题。当我执行时,我得到了:OutputLengthExceptioncipher.doFinal(inputTextBytes, intOutOff);org.bouncycastle.crypto.OutputLengthException: output buffer too short我不完全了解如何生成该整数来执行该方法。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;    }}你能帮助我了解我做错了什么吗?
查看完整描述

1 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

调用 应将输出数组作为第一个参数 - 而不是您正在处理的输入: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

}

(我还没有验证实现的其余部分!


查看完整回答
反对 回复 2022-09-07
  • 1 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号