使用 RSA (Java) 加密长字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9655920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:43:03  来源:igfitidea点击:

Encrypt long String with RSA (Java)

javaencryptionrsa

提问by Shuli

I'm having problems with an RSA application I have to do in Java.

我在使用 Java 的 RSA 应用程序时遇到了问题。

I have to read a string from a file, encrypt it, and then save the encrypted string in a new file.

我必须从文件中读取一个字符串,对其进行加密,然后将加密后的字符串保存在一个新文件中。

My RSA key is 1024 bits long.

我的 RSA 密钥是 1024 位长。

The code's part where the problem is, is the following:

问题所在的代码部分如下:

            readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);

            while(readBytes != -1){
                encodedContent = ciph.update(bytesToBeEncoded, 0, readBytes);
                out.write(encodedContent);
                bytesToBeEncoded= new byte[(KEY_SIZE/8)-11];
                readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);                  
            }

            encodedContent = ciph.doFinal();
            out.write(encodedContent);

Where the variables are defined like this:

变量定义如下:

        byte[] bytesToBeEncoded = new byte[(KEY_SIZE/8)-11]; 

        FileInputStream in = new FileInputStream(new File(file1));
        FileOutputStream out = new FileOutputStream(new File(file2));
        int readBytes;

The point is that when I encrypt a less-than-117 bytes string, it works perfectly (encrypt and then decrypt well), but when the size is larger, the application throws this exception:

重点是,当我加密一个小于 117 字节的字符串时,它工作得很好(加密然后解密很好),但是当大小较大时,应用程序会抛出此异常:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes

Thrown by:

抛出:

encodedContent = ciph.doFinal();

I don't know where the problem is and what I have to do.

我不知道问题出在哪里,我必须做什么。

Could anyone help me? Thank you.

有人可以帮助我吗?谢谢你。

Sorry about my english.

对不起我的英语。

采纳答案by Steven Mastandrea

The problem is with how you are initializing the byte array that you are reading your input. You are setting the size based on the size of the RSA key instead of by the expected size of your input stream.

问题在于您如何初始化正在读取输入的字节数组。您正在根据 RSA 密钥的大小而不是输入流的预期大小设置大小。

byte[] bytesToBeEncoded = new byte[(KEY_SIZE/8)-11];

The key size is 1024 bits, so 1024/8 -11 = 128 - 11 = 117 bytes. This is the amount of data that you can read from the stream, and should not be related to the size of your RSA key.

密钥大小为 1024 位,因此 1024/8 -11 = 128 - 11 = 117 字节。这是您可以从流中读取的数据量,不应与 RSA 密钥的大小相关。

You should initialize the byte array to be the maximum size of data that you'll need to read in, which will avoid the Exception. For example:

您应该将字节数组初始化为您需要读入的最大数据大小,这将避免异常。例如:

byte[] bytesToBeEncoded = 1024;

would allow a maximum input string of 1024 characters.

将允许最大输入字符串为 1024 个字符。