Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

use RSA in java

843810May 10 2004 — edited Feb 25 2008
Hello, everyone,
I am a little bit confused about the JCE: Does the JCE include the RSA algorithm?
I think JCE should have include the RSA algorithm because it is mentioned in the "Java Cryptography Extension (JCE) Reference Guide". But after I wrote a simple code just try to test the RSA algorithm, and after execution, it says:

Exception in thread "main" java.security.NoSuchAlgorithmException: Cannot find a
ny provider supporting RSA
at javax.crypto.Cipher.getInstance(DashoA6275)
at enc.main(enc.java:22)

The code is as following:
import java.io.*;
import java.security.*;
import javax.crypto.*;

class enc {

    public static void main (String[] args) throws NoSuchAlgorithmException, 

InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException, 

BadPaddingException, NoSuchPaddingException {

/* Generate a RSA key pair */

        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

        keyGen.initialize(1024, random);

        KeyPair pair = keyGen.generateKeyPair();
        PrivateKey priv = pair.getPrivate();
        PublicKey pub = pair.getPublic();

/* Create the cipher */      
        Cipher rsaCipher = Cipher.getInstance("RSA");

// Initialize the cipher for encryption
        rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

// Cleartext
        byte[] cleartext = "This is just an example".getBytes();
        System.out.println("the original cleartext is: " + cleartext.toString());

// Encrypt the cleartext
        byte[] ciphertext = rsaCipher.doFinal(cleartext);

// Initialize the same cipher for decryption
        rsaCipher.init(Cipher.DECRYPT_MODE, priv);

// Decrypt the ciphertext
        byte[] cleartext1 = rsaCipher.doFinal(ciphertext);
        System.out.println("the final cleartext is: " + cleartext1.toString());
    }
}
Does anyone know what's wrong with my code, or what should I do?
I appreciate any help, thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2008
Added on May 10 2004
17 comments
438 views