Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / April 2004

Tip: Looking for answers? Try searching our database.

Base Encoding and decoding Java

Thread view: 
Ramy076 - 01 Apr 2004 12:49 GMT
I have a C App which uses openssl for Encoding and decoding which
needs to be ported to java.

The decoded text does not match between the C App and the java App.
The size of the output itself is wrong. I think i am doing something
basically wrong. pls help.

Output is not the same as seen here because of special characters
Output from C: is only 12 bytes long

ýmýýýýýý.ýýý~ý.ý.ýýýýuýýýý_ý.ýý7ýuý...`.K.|dR..ýH?ý.ý.}ý..oý

Output from Java: 128 bytes including a new line char.

+mýtýný+ýýOý~+?ý
fýý8uýý-ý_ý?f-7ýuý???`?K?|dR?ýýH?+?v?}d?ýo-ýQýxDSG-?'ýýuý?H1Fzý8|ýý}1ý~-ýHw-fý?}ýý~-_ýý_i
+HAGaBý?ý_~ý?uv?ýý?|D

Following is the code in both C and Java
C Code:

char* my = new char(172);
char  base64[78];
int                x;
int                keyLength;
EVP_ENCODE_CTX    ctx;

getbaseData()
{
    memset(my,0,172);
    strcpy(my,"1234567899adserffsgbhgrthfbsdferwsdfghjtyjf+dfgDGRdgHEsdfGRSGH+ASD/FG/sdfesfgW/SFVGHeERT4sEYJ5+sdfERSDFGesfsfJTdfTGJfsSfSHfEZpsdfa36fsJf3sdfaSDFSEFHYUKMF5dfftsadfsd+asffES=");
    EVP_DecodeInit(&ctx);
    EVP_DecodeUpdate(&ctx, (unsigned char *)base64, &x, (unsigned char *)
my, 172);
    keyLength = x;    //getting 48
    EVP_DecodeFinal(&ctx, (unsigned char *)&base64[x], &x);
    keyLength += x;    //getting 12
}

Java Code used the Base64 code from
http://ostermiller.org/utils/Base64.html i also tried the B64 class
from http://sourceforge.net/snippet/detail.php?type=snippet&id=100549
- both the results are the same.

java com.Ostermiller.util.Base64 -vd
1234567899adserffsgbhgrthfbsdferwsdfghjtyjf+dfgDGRdgHEsdfGRSGH+ASD/FG/sdfesfgW/SFVGHeERT4sEYJ5+sdfERSDFGesfsfJTdfTGJfsSfSHfEZpsdfa36fsJf3sdfaSDFSEFHYUKMF5dfftsadfsd+asffES=
Roedy Green - 02 Apr 2004 01:15 GMT
>The decoded text does not match between the C App and the java App.
>The size of the output itself is wrong. I think i am doing something
>basically wrong. pls help.

see http://mindprod.com/jgloss/base64.html

Base64 output is might be chars or raw bytes, using a limited number
of the ASCII characters.  What do showed there does not look like
Base64 output.  

I suspect your problem lies confusing bytes with 16-bit characters
somewhere along the line.

you should see something like this:

    F5s/R/DrkZeKiA==

The other possibility is that your C program is using something other
than Base64, perhaps a home-brew encoding.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Stephen Ostermiller - 04 Apr 2004 15:23 GMT
Your input is 172 characters, so your ouput should be 128 bytes.
(Base64 is 3 bytes to 4 characters, so 128 = 3/4*172 - padding)

The 12 bytes that you are getting from your C implementation is
certainly not correct.

Stephen
Michel Gallant - 04 Apr 2004 16:43 GMT
Good description here:
  http://www.fourmilab.ch/webtools/base64/rfc1341.html
also, some sample encode/decode here:
 "Core Java" Vol II, Horstmann, Cornell, 3rd. Edn. 1998  p. 330.

- Mitch Gallant
  www.jensign.com

> Your input is 172 characters, so your ouput should be 128 bytes.
> (Base64 is 3 bytes to 4 characters, so 128 = 3/4*172 - padding)
[quoted text clipped - 3 lines]
>
> Stephen


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.