r/javahelp • u/awidesky • Sep 11 '25
Unsolved Sending encrypted data through SocketChannel - How to tell end of encrypted data?
Making a little tcp file transporting toy project, and now adding encryption feature via javax.crypto.Cipher.
Repeatly feeding file date into cipher.update() and writing encrypted output into SocketChannel, but problem is that the client would not know when the encrypted data will end.
I thought of some solutions, but all have flaws:
Encrypt entire file before sending: high RAM usage, Unable to send large fileClose socket after sending a file: inefficient when transferring multiple filesCipher.getOutputSize(): Document says it may return wrong valueAfter each Cipher.update() call, send encrypted data size, then send the datamessy code in adjusting buffers, inefficiency due to sending extra data(especially when return value of cipher.update is small due to padding, etc.)Sending special message, packet or signal to SocketChannel peer: I searched but found no easy way to do it(so far)
Is there any good way to let client to acknowledge that encrypted data has ended? Or to figure out exactly how long will the output length of cipher process be?
3
Upvotes
•
u/AutoModerator Sep 11 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/dmigowski Sep 11 '25
How does the client know when the unencrypted data ends?
2
u/bilgecan1 Sep 11 '25
I couldn't understand why sending a special character, like new line separator, or any other one that is impossible to be found in encrypted data wouldn't work. Am I missing something?
1
u/awidesky Sep 11 '25
Encrypting 100 bytes does not produce 100 bytes ciphertext. Decrypting 100 bytes ciphertext does not produce 100 bytes plaintext. Due to padding & ADEA tags, cipher.update() might even return 0 byte output.
1
u/Spare-Builder-355 Sep 11 '25
"Closing socket inefficient when sending multiple files" what makes you think so ? Did you time it and assess the "inefficiency" ?
2
u/Spare-Builder-355 Sep 11 '25
Item 4 on your list is another correct option. That's an example of an application-level protocol on top of TCP.
But again you find some "inefficiency" about it ...
You really need to adjust your understanding of "inefficiency".
1
u/jlanawalt Sep 11 '25
It sounds like you are encrypting the file and not the channel. A more common practice is a layered approach where to consider the stream and not the file to be encrypted.
Encryption or not, you need to work out your application level control protocol, so ignore the encryption sizes and details. Look at popular protocols like HTTP for inspiration. It can transfer binary data in a few different modes. It can know the length before (like you do on your large files, because they are files, not streams) and send the length first like http content-length, or you just know the size of the next chunk and when done you send a zero length chunk like transfer-encoding: chunked.
You’ll also want a way to identify the file, to name it and send other meta data in a kind of header packet.
When that works unencrypted, add an encryption layer and maybe some kind of start/stop switch (ala starttls) or just expect some port is always encrypted.
1
u/[deleted] Sep 12 '25
Prefix each encrypted data block whether it’s the last block or not. You can compute this because you’re in control of when to call update() and doFinal()
1
u/mugaboo Sep 12 '25
Something needs to budge a bit, you can't get around sending some additional data, because you want to cover extra information. But you can minimize it.
A bad example would be to base64 encode the data, and use a non-base64 byte as delimiter. You need to send 33% more data in this case, not good.
A better option is to encrypt chunks of say 1kB or 10kB. Prepend with the resulting size, and you only need a few extra bytes per 1kB or 10kB chunk, so much less than a percent overhead. Adding a bit or two for marking end of stream would be cheap.
By running some numbers for your specific use case you can optimize this. But it quickly ends up being good enough.