简体   繁体   English

加密大字节数组

[英]Encrypting large byte array

I am using a Cipher for encryption/decryption.我正在使用Cipher进行加密/解密。 It has always been fine, but now I am having problem when dealing with large array bytes.它一直很好,但现在我在处理大数组字节时遇到了问题。

This is my implementation:这是我的实现:

val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey())
val encryptedData = cipher.doFinal(textToEncrypt.toByteArray(StandardCharsets.UTF_8))

As said the problem happens only if the input text is too big.如上所述,只有当输入文本太大时才会出现问题。 For example, if I pass as input of the doFinal a byte array of length 168036, it will give me back an array of byte, of length 65652.例如,如果我将长度为 168036 的字节数组作为doFinal的输入传递,它将返回一个长度为 65652 的字节数组。

Is this related with a limitation of the Cipher?这与密码的限制有关吗? If so, is there a way for increasing the input buffer?如果是这样,有没有办法增加输入缓冲区?

在此处输入图片说明

GVillani82 shows he's in posession of some serious Google-Fu; GVillani82 显示他拥有一些严肃的 Google-Fu; from the comments:来自评论:

"Maybe related with this https://issuetracker.google.com/issues/123307358 ?" “也许与这个https://issuetracker.google.com/issues/123307358 有关?”

Shows that this is likely a bug in runtime 27 and before.表明这可能是运行时 27 及之前版本中的错误。 Presumably it is fixed in later versions, although the bug report doesn't say which version.据推测,它在以后的版本中得到了修复,尽管错误报告没有说明是哪个版本。


However, maybe it is better to fix the issue using incremental updates.但是,也许最好使用增量更新来解决问题。 In that case make sure that your buffer is strictly below 64KiB, say 32KiB.在这种情况下,请确保您的缓冲区严格低于 64KiB,比如 32KiB。

Basically, in Java, there are two ways how you can perform incremental encryption .基本上,在 Java 中,有两种方法可以执行增量加密 One is to simply use one of the many Cipher.update methods, and supply it text from a buffer (and note that Cipher.update returns part of the ciphertext, when it becomes available).一种是简单地使用许多Cipher.update方法之一,并从缓冲区提供文本(并注意Cipher.update返回部分密文,当它变得可用时)。 Then you need to finalize the encryption using a doFinal() method.然后您需要使用doFinal()方法完成加密。

The other is to use streaming.另一种是使用流媒体。 For encryption you'd generally use a CipherOutputStream and then copy the plaintext into it, either manually (again using a buffer as you are doing now) or by simply performing the InputStream.transferTo method if you want to write an entire stream.对于加密,您通常会使用CipherOutputStream然后将明文复制到其中,手动(再次使用缓冲区,就像您现在所做的那样)或者如果您想写入整个流,只需执行InputStream.transferTo方法。 In that case, you might want to directly read bytes from the resource (keeping the original encoding).在这种情况下,您可能希望直接从资源中读取字节(保持原始编码)。 However, in this case you don't know the what buffer size it will use.但是,在这种情况下,您不知道它将使用什么缓冲区大小。 If you need streaming then it is relatively easy to create your own streaming classes using update / doFinal calls.如果您需要流式传输,那么使用update / doFinal调用创建自己的流式传输类相对容易。

Using doFinal using an in and output buffer ( ByteBuffer ) seems to still trigger the error, so that won't work.使用输入和输出缓冲区 ( ByteBuffer ) 使用doFinal似乎仍会触发错误,因此不起作用。

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

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