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

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.

[1279 byte] By [honhwanga] at [2007-10-3 3:25:02]
# 1
You don't have to do anything. Just read into a byte[] array and pass that to the crypto system. It will do what it has to do.
ejpa at 2007-7-14 21:17:55 > top of Java-index,Security,Cryptography...
# 2

One does not normally have to worry about signed or unsigned bytes in encryption. The bytes are considered just as bytes and not as numbers (two's complement) so nothing changes. Based on the limited informtion you have given then I would expect

File --> FileInputStream -> CipherInputStream

to be all that is required.

sabre150a at 2007-7-14 21:17:55 > top of Java-index,Security,Cryptography...