• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need Help For AES to work with UDP

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need some help to make AES work with UDP. I keep getting this error.

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length
must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at AES.decrypt(AES.java:31)
at EchoServer.main(EchoServer.java:26)


It seems that upon receiving the UDP AES encrypted message from client, it has problem decrypting due to the byte[] which is being received from UDP transmission. I have also no idea whether am I doing it the right way.
Can Somebody help and recommend a good way to solve this problem?

This is my main driver file (UDP Server):


This is my AES Driver.


All help is very much appreciated. Thanks in advance.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Wong wrote:I need some help to make AES work with UDP. I keep getting this error.
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length
must be multiple of 16 when decrypting with padded cipher


Well, the error message seems pretty clear to me. Is your input length a multiple of 16?

This is my main driver file (UDP Server):...


Please DontWriteLongLines.
I'd break yours up myself, but you have LOTS of them, and it makes your thread very hard to read.
You can use the 'Edit' button to edit your post, and the 'Preview' button to view it before you save,
but please follow the guidelines in the link.

Thanks.

Winston
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that. First time posting here.

What I mean is that, is there any way which I can do to ensure that the decryption input byte is always in multiples of 16? as the input size we receive from the UDP socket is always unpredictable?

 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The two lines
together cause your problem. You should modify your decrypt method so that it takes as arguments the byte array and the number of bytes to decrypt. You can then use these directly in the cipher.doFinal() method (check the Javadoc for Cipher). You can then pass packet.getData() and packet.getLength() as the arguments.

The cipher text is binary data. It is very common for people to use String objects as containers for binary data but not all byte and byte sequences represent valid characters so as you are finding the process is not always reversible.

There is another point to consider. UDP packets can be fragment by the system and your code does not handle the fragmentation!
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Your solution solved the problem.
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to know that but out of interest how are you handling the UDP package fragmentation issue?
 
Sebastian Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code that I show is my template code generated from my code generator for secure network protocols for my degree project. My main priority is my code generator and so I did not touch on that part. Sorry about that and my capability in coding is also limited.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote: You should modify your decrypt method so that it takes as arguments the byte array and the number of bytes to decrypt. You can then use these directly in the cipher.doFinal() method (check the Javadoc for Cipher). You can then pass packet.getData() and packet.getLength() as the arguments.!



Thank you so much Mr Tookey.. I've been looking for that answer for two hours. I checked every single link related to UDP, AES, Symmetric encryption. EVERYTHING.
Every single thread , but everytime the same stupid stuff : some random guy posting some non-working code, no answers ...
And then I read this simple post, reminding me to read the doc ; I feel a bit mad I didn't check it earlier. I should have known. At least I can say i've learned a lot, reading all these threads. Haha.
Anyway, you'll probably never see this message. I just had to write it. Thanks a lot again.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic