Results 1 to 3 of 3
  1. #1
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Unhappy 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());
            }
        }
    as you can see, there are a LOT OF EXCEPTIONS!!:eek:

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    your code looks good... just in case, take a look at these tutorials: RSA algorithm**Security**Java Tutorial

  3. #3
    soccermiles is offline Member
    Join Date
    Apr 2010
    Posts
    19
    Rep Power
    0

    Default

    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

  1. Replies: 0
    Last Post: 02-12-2010, 03:33 PM
  2. EJB invokation failing in jdk1.5.0_23 but works in jdk1.6.0
    By randle169 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 01-28-2010, 09:09 AM
  3. Issues while failing to register ttf fonts.
    By pvn_myne in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 08-19-2009, 08:36 AM
  4. Replies: 0
    Last Post: 02-01-2009, 08:43 AM
  5. Replies: 8
    Last Post: 08-27-2008, 11:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •