亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號