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;
}
}

