简体   繁体   English

使用RSA Bouncy Castle加密/解密无法正常工作

[英]Encrypting/decrypting with RSA Bouncy Castle not working properly

I'm trying to encryt/decrypt using RSAEngine library at bouncy castle, with a 2048 bit length key. 我正试图在充气城堡使用RSAEngine库加密/解密,密钥长度为2048位。 I'm able to create the keys, store in different files and get from the files, but when I decrypt an image it makes something that I don't know that the file decrypted is not shown correctly.Files are created correctly,and I think the problem is at processBlock method while encrypting and/or decrypting.The code is the following to encrypt: 我能够创建密钥,存储在不同的文件中并从文件中获取,但是当我解密图像时,它会产生一些我不知道解密文件没有正确显示的内容。文件创建正确,我认为问题在于加密和/或解密时的processBlock方法。加密的代码如下:

InputStream clearTextFile;
    FileOutputStream textFileProcessed=new FileOutputStream(fileName);
            //getKey is a method I implemented and works correctly
    RSAKeyParameters key=getKey(keyFileName);
    RSAEngine rsaEngine=new RSAEngine();
    rsaEngine.init(true,key);           
    clearTextFile=new FileInputStream(nameClearTextFile);
    byte[] bytesReaded;
    int nBytesReaded;
    int inputBlockSize=rsaEngine.getInputBlockSize();
    do
    {
        bytesReaded = new byte[inputBlockSize];
        nBytesReaded=clearTextFile.read(bytesReaded);
        if(nBytesReaded>-1)
        {       //This is for the last block if it's not 256 byte length
            if(nBytesReaded<inputBlockSize)
            {
                byte[] temp=new byte[nBytesReaded];
                for(int i=0;i<nBytesReaded;i++)
                {
                    temp[i]=bytesReaded[i];
                }
                byte[] encryptedText=rsaEngine.processBlock(temp,0,nBytesReaded);
                textFileProcessed.write(encryptedText);
            }
            else
            {
                byte[] encryptedText=rsaEngine.processBlock(bytesReaded,0,inputBlockSize);
                textFileProcessed.write(encryptedText); 
            }
        }
    }while(nBytesReaded>-1);
    textFileProcessed.flush();
    textFileProcessed.close();
    textFileProcessed.close();

And to decrypt: 并解密:

InputStream encryptedTextFile=new FileInputStream(nameOfFile);
    OutputStream decryptedTextFile=new FileOutputStream(nameOfFile);
    RSAKeyParameters key=getKey(nameKeyFile);
    RSAEngine rsaEngine=new RSAEngine();
    rsaEngine.init(false,key);
    byte[] bytesReaded;
    int nBytesReaded;
    int inputBlockSize=rsaEngine.getInputBlockSize();
    do
    {
        bytesLeidos = new byte[inputBlockSize];
        nBytesReaded=encryptedTextFile.read(bytesReaded);
        if(nBytesReaded>-1)
        {
                byte[] decryptedText=rsaEngine.processBlock(bytesReaded,0,inputBlockSize);          
                decryptedTextFile.write(decryptedText);                 
        }
    }while(nBytesReaded>-1);
    decryptedTextFile.flush();
    decryptedTextFile.close();
    encryptedTextFile.close();

Thanks in advance 提前致谢

RSAEngine does not add padding, you will lose any leading zeros in your data blocks as a result. RSAEngine不添加填充,因此您将丢失数据块中的任何前导零。 You need to use one of the encoding modes available as well. 您还需要使用其中一种编码模式。

I'd recommend using a symmetric key algorithm as well and just using RSA to encrypt the symmetric key. 我建议使用对称密钥算法,只使用RSA加密对称密钥。 It will be a lot faster, and depending on your data, safer as well. 它会快得多,并且取决于您的数据,也更安全。

Regards, 问候,

David 大卫

I think you need to change this line: 我想你需要改变这一行:

   if(nBytesReaded>1)

to this 对此

   if(nBytesReaded>-1)

And change this in the decypt part, maybe: 并在decypt部分改变这一点,也许:

rsaEngine.init(false,clave);

to this 对此

rsaEngine.init(false,key);

But there may be more. 但可能会有更多。 You aren't encrypting the whole input if the last block isn't full size. 如果最后一个块不是完整大小,则不加密整个输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM