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!

RSA decryption urgent

843810Apr 4 2002 — edited Oct 19 2003
Hi
I need to use RSA public key to decrypt a message which is encrypted by private key of the key pair. the public key is stored in the file. now I need to use that public key to decrypt a message on a seperate program.
I tried several times , but it seems like the result of decrytion is not the same as the cleartext.
encryption decryption worked fine on one program, but it doesn't work when I send the byte[] ciphertext to the other program and decrypt it. I still think that somehow the ciphertext gets changed at the receiving program. below is the code i put into the decrypting program:
ObjectInputStream ois=new ObjectInputStream(sockets.getInputStream());
String c = ois.readLine() ;// <b>maybe this line caused problem , but how I can directly receive byte[] in the objectinputstream without changing to string first?<b>
ois.close() ;
FileInputStream in = new FileInputStream("cpublic.key");
ObjectInputStream oin = new ObjectInputStream(in);
RSAPublicKey rsapukey = (RSAPublicKey)oin.readObject() ;
oin.close();
in.close();
//decrypt
Cipher C = Cipher.getInstance("RSA");
C.init(Cipher.DECRYPT_MODE, rsapukey);

byte[] unencrypted = C.doFinal(ciphertext);
System.out.write(unencrypted) ;
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2003
Added on Apr 4 2002
21 comments
1,092 views