rsa encoding problem

Hello.

I've written a short program that is just supposed to generate an RSA keyset, encrypt with a plain text message with the public key, then decrypt that same message with the private key. Seems simple enough, but I've got some problems and can't figure them out.

I'm including the code below. It works: my providers are SunJCE for the encryption engine and SunRSASign for the key genreator.

Usage: java com.blackdog.testing.encryption.AsymmetricKeyMakersomeMessageToEncode

After I execute, the cipherText, as printed to the standard output, seems to be adequately encrypted ( but how would I know? ). When I decrypt it and print it out, its something else, but NOT the original message.

Other things I have noticed: 1) the cipherText comes out the same no matter what input message I use?!!?

I suppose I'm doing something wrong, but what? I couldn't find documentation on the providers being used by my code. Perhaps the encryption is working but i'm running into character encoding problems?

BTW, if you know of documentation that explains something that I'm obviously not understanding, please point me to it. I've read through most of the stuff I could find online, but I can't find too much really.

package com.blackdog.testing.encryption;

import java.io.IOException;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.KeyPair;

import java.security.PrivateKey;

import java.security.PublicKey;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

publicclass AsymmetricKeyMaker{

publicstaticvoid main(String[] args){

String algorithm ="rsa";

String plainText = args[0];

try{

/* make keys */

KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithm);

System.out.println("Got key generator from provider = " + generator.getProvider());

KeyPair keyPair = generator.generateKeyPair();

PublicKey publicKey = keyPair.getPublic();

PrivateKey privateKey = keyPair.getPrivate();

//System.out.println(keyPair.getPublic());

//System.out.println(keyPair.getPrivate());

/* create the string to encrypt */

byte[] plainTextBytes = plainText.getBytes();

System.out.println("PlainText = " + plainText);

/*ENCRYPT */

System.out.println("Encrypting . . . ");

byte[] cypherTextBytes = encrypt ( plainTextBytes, publicKey );

String cypherText = cypherTextBytes.toString();

System.out.println("CypherText = " + cypherText);

/*DECRYPT */

System.out.println("Decrypting . . . ");

byte[] recoveredPlainTextBytes = decrypt ( cypherTextBytes, privateKey );

System.out.println("RecoveredPlainText = " + recoveredPlainTextBytes.toString());

}catch (NoSuchAlgorithmException e){

System.err.println(

"usage: java AsymmetricKeyMaker <RSA | DSA>");

}catch (Exception e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

publicstaticbyte[] encrypt(byte[] text, PublicKey key)throws Exception

{

byte[] cipherText =null;

// get an RSA cipher object and print the provider

Cipher cipher = Cipher.getInstance("RSA");

System.out.println("nProvider is:" + cipher.getProvider().getInfo());

// encrypt the plaintext using the public key

cipher.init(Cipher.ENCRYPT_MODE, key);

cipherText = cipher.doFinal(text);

return cipherText;

}

publicstaticbyte[] decrypt(byte[] text, PrivateKey key)throws Exception

{

byte[] dectyptedText =null;

// decrypt the text using the private key

Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.DECRYPT_MODE, key);

dectyptedText = cipher.doFinal(text);

return dectyptedText;

}

}

[6161 byte] By [chadmichaela] at [2007-11-26 15:50:42]
# 1
The line String cypherText = cypherTextBytes.toString();does not create a String representation of the content of the bytes. If you must have a String representation then use Base64 or Hex encoding.
sabre150a at 2007-7-8 22:10:37 > top of Java-index,Security,Cryptography...
# 2

Thanks for your feedback.

I realize it doesn't make "sense" to display the cipher text as a String. I don't know why I was printing that to the output, just for output I guess, to confirm that its no longer the same bytes that I sent into the encryption.

That's not really the issue though. Of more concern is why the plain text that I regain after decryption isn't displaying as the STring I would expect, i.e. my original plain text message?

chadmichaela at 2007-7-8 22:10:37 > top of Java-index,Security,Cryptography...
# 3
You obviously did not read my previous post.System.out.println("RecoveredPlainText = " + new String(recoveredPlainTextBytes));
sabre150a at 2007-7-8 22:10:37 > top of Java-index,Security,Cryptography...
# 4

Believe me, I'm not trying to be obtuse, but I'm not even sure what you mean by "previous post". Do you mean the previous post in this thread? Or do you mean that you have posted on this topic previously on another thread.

Looking at your last post, I just realized myself what the problem was. I've been programming higher level api's ( web application business logic, etc. ) too long I guess. I was calling toString() on a byte[] and in my head I was thinking that I was calling toString() on a StringBuffer. But I honestly don't see that your previous post was "obviously" pointing this out. Which might be an indication of my clueslessness, but, in all honesty, I don't think its entirely that.

I do appreciate the posts though. Sorry about the communication breakdowns.

chadmichaela at 2007-7-8 22:10:37 > top of Java-index,Security,Cryptography...
# 5

> Believe me, I'm not trying to be obtuse, but I'm not

> even sure what you mean by "previous post". Do you

> mean the previous post in this thread? Or do you

> mean that you have posted on this topic previously on

> another thread.

OK! Reply #1 of this thread.

>

> Looking at your last post, I just realized myself

> what the problem was. I've been programming higher

> level api's ( web application business logic, etc. )

> too long I guess. I was calling toString() on a

> byte[] and in my head I was thinking that I was

> calling toString() on a StringBuffer. But I honestly

> don't see that your previous post was "obviously"

> pointing this out.

I pointed out that the toString() method of a byte array does not produce a representation of the content of the byte array. I assumed that you would take this information and go through your code to see if it was likely to be a problem elsewhere. I misjudged you.

> Which might be an indication of

> my clueslessness, but, in all honesty, I don't think

> its entirely that.

So it is my fault. Next time I will go through every line of your code and spell out all the problem lines.

>

> I do appreciate the posts though. Sorry about the

> communication breakdowns.

sabre150a at 2007-7-8 22:10:37 > top of Java-index,Security,Cryptography...