Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Decrypting RSA Ciphertext saved to file as C `unsigned char`

843811Aug 23 2006 — edited Aug 24 2006
Hi all,

I have a RSA ciphertext generated by OpenSSL (on a GNU/Linux i386) that is saved to a file.

I then opened the file in Java, read the bytes and attempt to decrypt it. Initially, I get errors like padding and stuff, so I've set the padding on both ends to be the same.

Now, I think my problem is this: the ciphertext bytes are written to the file as C's `unsigned char`. However, Java's `byte` representation is not the same as C's `unsigned char`.

The way I am reading the file is this:

File -> FileInputStream -> DataInputStream

I have used DataInputStream.read() and DataInputStream.readUnsignedByte().

Now both of these upcasts the `unsigned char` of the file to a Java's `int`. The problem is the javax.crypto.Crypto related classes operate on `byte`. And the ciphertext needs to be parsed byte by byte.

So what I am asking is, how do I correctly read and decrypt RSA ciphertext that is generated by a C program, using OpenSSL and saved as `unsigned char`?

I've seen many examples of this C's `unsigned char` to Java's `byte` problem, but they mainly related to Strings encoding and not actual bytes of ciphertext.

Thanks.

Hon Hwang.

BTW, I've tried using the Integer class.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2006
Added on Aug 23 2006
2 comments
159 views