Results 1 to 3 of 3
- 04-26-2010, 07:39 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 19
- Rep Power
- 0
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...
Java 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()); } }
- 04-26-2010, 08:25 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 11
your code looks good... just in case, take a look at these tutorials: RSA algorithm*«*Security*«*Java Tutorial
- 04-26-2010, 08:53 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 19
- Rep Power
- 0
thank you for that, it is probably the only good tutorial I have found on this.
I am actually trying to make a text encoder using a Swing GUI, so I need to fit this all in one method statement.:eek:
tyty, I will mark as solved if I solve it. Otherwise, if anyone knows why this isnt working, please advise.
Similar Threads
-
I tried debug failing: trying to include xml file contents to db table...
By lse123 in forum XMLReplies: 0Last Post: 02-12-2010, 04:33 PM -
EJB invokation failing in jdk1.5.0_23 but works in jdk1.6.0
By randle169 in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 01-28-2010, 10:09 AM -
Issues while failing to register ttf fonts.
By pvn_myne in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 08-19-2009, 09:36 AM -
Eclipse failing to start on Vista 64bit Home Prem.. while i got no prob runnin JGrasp
By kendy in forum EclipseReplies: 0Last Post: 02-01-2009, 09:43 AM -
[SOLVED] ActionEvent.setSource() is failing in Swing while working in AWT
By playwin2 in forum AWT / SwingReplies: 8Last Post: 08-28-2008, 12:48 AM
Bookmarks