在java中使用rsa加密和解密大文件 [英] encrypting and decryption large file using rsa in java

查看:534
本文介绍了在java中使用rsa加密和解密大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RSA算法来加密和解密大小超过rsa密钥大小的文件。

I am using RSA algorithm for encryption and decryption of a file with size more than rsa key size.

在下面的加密代码中,我正在逐块读取文件内容并转换为密文。块大小为32个字节。

In the code below for encryption, i am reading file content in block-wise and converting into cipher text. Block-size is 32 bytes.

FileInputStream fin1 = new FileInputStream(genfile);

FileOutputStream fout = new FileOutputStream(seedcipher);

byte[] block = new byte[32];
int i;
while ((i = fin1.read(block)) != -1)
{
    byte[] inputfile= cipher.doFinal(block);
    fout.write(inputfile);
}

fin1.close();

在解密部分,在我提到块大小的代码中完成相同的逐块解密as 128 bytes

At decryption part, same block-wise decryption is done in the code where i have mentioned the block size as 128 bytes

FileInputStream fin1 = new FileInputStream(encryptedfile);
FileOutputStream fout = new FileOutputStream(seedcipher);

DataInputStream dos =new DataInputStream(fin1);
DataOutputStream dosnew =new DataOutputStream(fout);
byte[] block = new byte[128];
int i;
while ((i = fin1.read(block)) != -1)
{
    byte[] inputfile= cipher.doFinal(block);
      fout.write(inputfile);
}

输入文件大小为81.3 kB,文件包含

Input file size is 81.3 kB and file contains


0
1
2
3
4.....29000 

解密文件后,输出包含一些不相关的额外值。为什么结果中有额外的数据?

After the file is decrypted,output contain some extra values which are not relevant. why is that extra data in the result?

推荐答案

你的IO代码逐块读取错误:

Your IO code for reading block by block is incorrect:

while ((i = fin1.read(block)) != -1) {
    byte[] inputfile= cipher.doFinal(block);
    fout.write(inputfile);
}




  1. 它假定每次你要求阅读一个块,读取整个块。情况不一定如此。只能读取几个字节。 read()方法返回实际读取的字节数(并存储在 i 中)。你不应该忽略它。

  2. 最后一个块很可能不完整,除非你的文件大小是32的倍数。所以在最后一次迭代中,你正在加密文件的最后N个剩余字节+上一次迭代中存储在字节数组中的32-N个字节。

  1. It assumes that every time you ask to read a block, a whole block is read. That is not necessarily the case. Only a few bytes might be read. The number of bytes that are actually read are returned by the read() method (and stored in i). You should not ignore it.
  2. The last block has a pretty good chance of being incomplete, unless your file size is a multiple of 32. So at the last iteration, you're encrypting the last N remaining bytes of the file + the 32 - N bytes that were stored in the byte array at the previous iteration.

使用RSA加密大文件不是一个好主意。例如,您可以生成随机AES密钥,使用RSA对其进行加密并将其存储在输出文件中,然后使用AES加密文件本身,这样可以更快,并且对大输入没有任何问题。解密将读取加密的AES密钥,对其进行解密,然后使用AES解密文件的其余部分。

Using RSA to encrypt a large file is not a good idea. You could for example generate a random AES key, encrypt it using RSA and store it in the output file, and then encrypt the file itself with AES, which is much faster and doesn't have any problem with large inputs. The decryption would read the encrypted AES key, decrypt it, and then decrypt the rest of the file with AES.

这篇关于在java中使用rsa加密和解密大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆