• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Base64 decoding not returning correct output  RSS feed

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing issues when I send a RC4 encrypted data encoded with Base64 over a Socket.

The decoded base64 tend to not match the source encoded Base64. I have extracted the key issues I face into a small program below.

Any help is appreciated



Sample output:
UTF-8
Plaintext: Hello
Encrypted plaintext in RC4: ���
Encoded plaintext in Base64: F9X6mgs=
Decoded from Base64: ���
Decrypted: H_)K��pd+�

 
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

WeiJie Lim wrote:The decoded base64 tend to not match the source encoded Base64.


The Base64 code seems to be working - the problem must be with the RC4 bits.
 
Ron McLeod
Saloon Keeper
Posts: 2504
321
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is related to the repeated transforming between byte[] and String representations.

I made a change to your code to have the decodeBase64 method return the generated byte[] rather than transforming it to a String (lines 23 and 24), and also made a corresponding change to where the method is called (lines 72 and 73).  After that change it does seem to work, but I think you need to refactor your code and remove most of where you convert between bytes and Strings.

 
WeiJie Lim
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Ron ! That was the key to solving the issue.

On a sidenote, may I know why does transforming from byte[] and String affect the bytes ?
 
Saloon Keeper
Posts: 5412
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my experience, both "new String(byte[])" and "String.getBytes()" are the source of many bugs. Whenever you're thinking about using them, consider if there are alternatives. Character data (which is what strings are) and binary data (like byte arrays) simply aren't the same thing, and converting between them can cause numerous issues.
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

WeiJie Lim wrote:On a sidenote, may I know why does transforming from byte[] and String affect the bytes ?



It's because a String isn't an array of bytes. It's an array of characters, instead. And a "character" is (approximately) a character of Unicode.

If you read that Wikipedia article a bit you'll soon notice that 256 different bytes aren't anywhere near enough to cover all of the Unicode characters. It so happens that the first 128 characters of Unicode are the ASCII character set, so if all your data fall into that category then you won't run into trouble. But once you get beyond that then you're going to have to decide how to represent the non-ASCII characters as bytes. You've already encountered UTF-8, which is one common way to do that.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!