java - Bouncy CaSTLe 问题和 Triple DES

标签 java encryption bouncycastle tripledes

您好,我有一个使用 Triple DES 的客户端。我知道我知道如果 AES 我不会有问题它是一个较旧的解决方案集成。我有下面的代码,它接收一个文件并写入另一个文件。在解密方法上,我不理解文件长度的第二个参数。请帮忙。我得到的错误如下: 线程“main”中的异常 org.bouncycaSTLe.crypto.DataLengthException:解密中的最后一个 block 不完整 在 org.bouncycaSTLe.crypto.paddings.PaddedBufferedBlockCipher.doFinal(未知来源) 在 DESede_BC.decrypt(DESede_BC.java:102) 在 DESede_BC.main(DESede_BC.java:120)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;



import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class DESede_BC {

PaddedBufferedBlockCipher encryptCipher;
PaddedBufferedBlockCipher decryptCipher;

// Buffers used to transport the bytes from one stream to another
byte[] buf = new byte[8];       //input buffer - block size length
byte[] obuf = new byte[557];    //output buffer

byte[] key = null;              //the key

public DESede_BC(){
    //use a default 192 bit key
    key = "thekey".getBytes();
    InitCiphers();
}
public DESede_BC(byte[] keyBytes){
    key = new byte[keyBytes.length];
    System.arraycopy(keyBytes, 0 , key, 0, keyBytes.length);
    InitCiphers();
}

private void InitCiphers(){
    encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
    encryptCipher.init(true, new KeyParameter(key));
    decryptCipher =  new PaddedBufferedBlockCipher(new DESedeEngine());
    decryptCipher.init(false, new KeyParameter(key));
}

public void ResetCiphers() {
    if(encryptCipher!=null)
        encryptCipher.reset();
    if(decryptCipher!=null)
        decryptCipher.reset();
}

public void encrypt(InputStream in, long length, OutputStream out)
throws ShortBufferException, 
    IllegalBlockSizeException,
    BadPaddingException,
    DataLengthException,
    IllegalStateException,
    InvalidCipherTextException
{
    try {
    // Bytes written to out will be encrypted
    // Read in the cleartext bytes from in InputStream and
    //      write them encrypted to out OutputStream

    int noBytesRead = 0;        //number of bytes read from input
    int noBytesProcessed = 0;   //number of bytes processed

    while ((noBytesRead = in.read(buf)) >= 0) {
        noBytesProcessed =
                encryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
        out.write(obuf, 0, noBytesProcessed);
    }
     noBytesProcessed =
             encryptCipher.doFinal(obuf, 0);

     out.write(obuf, 0, noBytesProcessed);

    out.flush();
}
catch (java.io.IOException e) {
    System.out.println(e.getMessage());
}
}
    public void decrypt(InputStream in,long length, OutputStream out)
   throws ShortBufferException, IllegalBlockSizeException,  BadPaddingException,
        DataLengthException, IllegalStateException, InvalidCipherTextException
    {
        try {
        // Bytes read from in will be decrypted
        // Read in the decrypted bytes from in InputStream and and
        //      write them in cleartext to out OutputStream

        int noBytesRead = 0;        //number of bytes read from input
        int noBytesProcessed = 0;   //number of bytes processed

        while ((noBytesRead = in.read(buf)) >= 0) {
                noBytesProcessed = decryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
                out.write(obuf, 0, noBytesProcessed);
        }
        noBytesProcessed = decryptCipher.doFinal(obuf, 0);
        out.write(obuf, 0, noBytesProcessed);

        out.flush();
    }
    catch (java.io.IOException e) {
         System.out.println(e.getMessage());
    }
}

public static void main(String... args)
        throws Exception
      {

    DESede_BC d = new DESede_BC();
FileInputStream fis2 = new FileInputStream("c:\\2.in");
FileOutputStream fos2 = new FileOutputStream("c:\\decrypted.txt");

    d.decrypt(fis2, new Long("128"), fos2);



      }

}

最佳答案

我认为您需要在 doFinal 之前将输入解码为 Base64:

byte[] obuf = Base64.decode(obuf, Base64.NO_OPTIONS);
byte[] decValue = c.doFinal(obuf);

关于java - Bouncy CaSTLe 问题和 Triple DES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905939/

相关文章:

java - 在为 HashMap 分配值时使用(对象)

encryption - 在没有进一步信息的情况下是否可以推迟信息披露?

java - 带 Spring Boot 的充气城堡

java - 使用充气城堡使用中间证书正确创建新证书

c# - 使用 BouncyCaSTLe 的数字签名验证 - ECDSA with SHA 256, C#

java - JDK/JRE 版本是否单独确定 SSLSocket.getSupportedProtocols() 的结果?

java - java中静态变量内存什么时候释放?

java - 在使用 RMI 的分布式系统上使用 Apache Shiro 的正确方法?

java - 使用 AES 和 Cipher 将 java 代码转换为 php

c# - GDPR : Encrypted logging in C#