Encrypting large byte array

GVillani82

I am using a Cipher for encryption/decryption. 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.

Is this related with a limitation of the Cipher? If so, is there a way for increasing the input buffer?

enter image description here

Maarten Bodewes

GVillani82 shows he's in posession of some serious Google-Fu; from the comments:

"Maybe related with this https://issuetracker.google.com/issues/123307358 ?"

Shows that this is likely a bug in runtime 27 and before. 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.

Basically, in Java, there are two ways how you can perform incremental encryption. 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). Then you need to finalize the encryption using a doFinal() method.

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. 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.

Using doFinal using an in and output buffer (ByteBuffer) seems to still trigger the error, so that won't work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Encrypting large byte array

From Dev

encrypting and decrypting a byte array

From Dev

Web service returning a large byte array

From Dev

Java Heap Memory Holding Large byte Array

From Dev

ZLib decompression fails on large byte array

From Dev

Encrypting (large) files in PHP with openSSL

From Dev

Download large files using large byte array causes "Out of memory"

From Dev

WinRT RSA encrypting large data buffer

From Dev

encrypting and decryption large file using rsa in java

From Dev

Encrypting a large file with AES using JAVA

From Dev

Encrypting with large numbers, using a Caesar Cipher

From Dev

getting negative byte array length or large amount of byte array length at client side in java

From Dev

Get Byte array from very large integer represented as string

From Dev

How do I read a large input byte-wise into an array?

From Dev

IndexOf, encrypting, array help in java

From Java

Why aren't large RSA keys encrypting to a unique value?

From Dev

Encrypting a Large File of irregular size using AES algorithm

From Dev

Encrypting/Decrypting(Using AES) Large files and transferring over HTTP

From Dev

java.lang.OutOfMemoryError when encrypting/decrypting large files

From Dev

Convert BYTE* to Array<byte>^

From Dev

Best way to return large file as split zip files,Stream or Byte array WCF

From Dev

Generate a large number of the same byte

From Dev

Ironpython Array[byte] to byte string

From Dev

byte array[] to byte is it possible in java?

From Dev

Byte Array to Byte conversion error

From Dev

Ironpython Array[byte] to byte string

From Dev

Reading array byte per byte

From Dev

byte array[] to byte is it possible in java?

From Dev

Convert XDocument to byte array (and byte array to XDocument)

Related Related

  1. 1

    Encrypting large byte array

  2. 2

    encrypting and decrypting a byte array

  3. 3

    Web service returning a large byte array

  4. 4

    Java Heap Memory Holding Large byte Array

  5. 5

    ZLib decompression fails on large byte array

  6. 6

    Encrypting (large) files in PHP with openSSL

  7. 7

    Download large files using large byte array causes "Out of memory"

  8. 8

    WinRT RSA encrypting large data buffer

  9. 9

    encrypting and decryption large file using rsa in java

  10. 10

    Encrypting a large file with AES using JAVA

  11. 11

    Encrypting with large numbers, using a Caesar Cipher

  12. 12

    getting negative byte array length or large amount of byte array length at client side in java

  13. 13

    Get Byte array from very large integer represented as string

  14. 14

    How do I read a large input byte-wise into an array?

  15. 15

    IndexOf, encrypting, array help in java

  16. 16

    Why aren't large RSA keys encrypting to a unique value?

  17. 17

    Encrypting a Large File of irregular size using AES algorithm

  18. 18

    Encrypting/Decrypting(Using AES) Large files and transferring over HTTP

  19. 19

    java.lang.OutOfMemoryError when encrypting/decrypting large files

  20. 20

    Convert BYTE* to Array<byte>^

  21. 21

    Best way to return large file as split zip files,Stream or Byte array WCF

  22. 22

    Generate a large number of the same byte

  23. 23

    Ironpython Array[byte] to byte string

  24. 24

    byte array[] to byte is it possible in java?

  25. 25

    Byte Array to Byte conversion error

  26. 26

    Ironpython Array[byte] to byte string

  27. 27

    Reading array byte per byte

  28. 28

    byte array[] to byte is it possible in java?

  29. 29

    Convert XDocument to byte array (and byte array to XDocument)

HotTag

Archive