Trying to use RSA encryption, failing...
I am trying to use RSA encryption and I don't know what I'm doing wrong.
Am I supposed to come up with byte values for each character of the message and put them in a byte[]?:confused:
Here is the code I'm trying to use (It's triggered by a button in a GUI)
hopefully there are enough annotations...
Code:
private void encodeButtonActionPerformed(java.awt.event.ActionEvent evt) {
try{
//making keypairgenerator
RSAKeyPairGenerator keygen=new RSAKeyPairGenerator();
//making a secure random to initialize key generator
SecureRandom rand1=new SecureRandom();
//initializing keygen
keygen.initialize(2048,rand1);
//naming key pair
KeyPair keyPair=keygen.generateKeyPair();
//defining public and private keys, setting text output to show keys
PublicKey publicKey=keyPair.getPublic();
encodePubKey.setText(publicKey.toString());
PrivateKey privateKey=keyPair.getPrivate();
encodePrivKey.setText(privateKey.toString());
//Making cipher object to encode string
Cipher encipher=Cipher.getInstance("RSA");
//initializing cipher object
encipher.init(Cipher.ENCRYPT_MODE,publicKey);
//defining string to be encoded
String encode=encodeText.getText();
//encrypting
byte[] encoded=Cipher.getInstance(encode).doFinal();
//output
encodeOutput.setText(encoded.toString());
//catching exceptions
}catch (NoSuchAlgorithmException a) {
System.err.println("Caught NoSuchAlgorithmException"+a.getMessage());
}catch (NoSuchPaddingException p) {
System.err.println("Caught NoSuchPaddingException" + p.getMessage());
}catch (InvalidKeyException k) {
System.err.println("Caught InvalidKeyException" + k.getMessage());
}catch (IllegalBlockSizeException b) {
System.err.println("Caught IllegalBlockSizeException"+b.getMessage());
}catch (BadPaddingException bp) {
System.err.println("Caught BadPaddingException" + bp.getMessage());
}
}
as you can see, there are a LOT OF EXCEPTIONS!!:eek: