I need to implement use the RSA public key cryptosystem with BouncyCastle, but not too sure on how to do it. I found the example below but not sure on how to change it from random gen keys to custom strings as private and public keys...any help?
/*Public Key cryptography using the RSA algorithm.
*/
import java.security.*;
import javax.crypto.*;
public class PublicKeyCrypto {
public static void main (String[] args) throws Exception {
//
// Check args and get plaintext
if (args.length !=1) {
System.err.println("Usage: java PublicExample text");
System.exit(1);
}
byte[] plainText = args[0].getBytes("UTF8");
//
// Generate an RSA key
System.out.println( "\nStart generating RSA key" );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
System.out.println( "Finish generating RSA key" );
//
// Creates an RSA Cipher object (specifying the algorithm, mode, and padding).
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//
// Print the provider information
System.out.println( "\n" + cipher.getProvider().getInfo() );
System.out.println( "\nStart encryption" );
//
// Initializes the Cipher object.
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
//
// Encrypt the plaintext using the public key
byte[] cipherText = cipher.doFinal(plainText);
System.out.println( "Finish encryption: " );
System.out.println( new String(cipherText, "UTF8") );
System.out.println( "\nStart decryption" );
//
// Initializes the Cipher object.
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
//
// Decrypt the ciphertext using the private key
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println( "Finish decryption: " );
System.out.println( new String(newPlainText, "UTF8") );
}
}
Message was edited by:
luqman