# Re: HELP : JCE : How to use KeyPairGenerator with Cipher

Narendra Patil (naren@corp.auspex.com)
Tue, 09 Jun 1998 12:41:53 -0700

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: Jan Luehe: "Re: HELP : JCE : How to use KeyPairGenerator with Cipher"
- Previous message: Jan Luehe: "Re: HELP : JCE : How to use KeyPairGenerator with Cipher"

    Message-Id: <3.0.1.32.19980609124153.00a81150@mailhost.auspex.com>

    Date: Tue, 09 Jun 1998 12:41:53 -0700

    To: Jan Luehe <Jan.Luehe@Eng>, java-security@web3.javasoft.com,

    From: Narendra Patil <naren@corp.auspex.com>

    Subject: Re: HELP : JCE : How to use KeyPairGenerator with Cipher

    In-Reply-To: <libSDtMail.9806091212.23984.luehe@shorter>

Hello,

Enclosed below is a simple test that is trying to use the public and
private keys for encryption and decryption using a Cipher object.

When I run this program, I get following output :
%java test3
Caught exception java.security.NoSuchAlgorithmException: algorithm RSA not
available.

Please NOTE that my classpath is properly set which also includes
jce12-ea2-dom.jar in it.

I would appreciate any help in solving this.

Thanks,
Narendra

import java.io.*;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import com.sun.crypto.provider.*;

class test3 {

public static void main(String[] args) {

try {

// add JCE to the list of providers
SunJCE jce = new SunJCE();
Security.addProvider (jce);

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024, new SecureRandom());
KeyPair pair = keyGen.generateKeyPair();

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

// create the Cipher object
Cipher desCipher = Cipher.getInstance ("RSA");

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

// our clear Text
byte[] clearText = "TEST".getBytes();

// encrypt the clear text
byte[] cipherText = desCipher.doFinal (clearText);

// initilize the Cipher in the decryption mode
desCipher.init (Cipher.DECRYPT_MODE, priv);

String cipherStr = new String (cipherText);

// decrypt the text
byte[] clearText2 = desCipher.doFinal (cipherText);
String clearStr = new String (clearText2);

// print the decrypted string
System.out.println ("\n"+"DECRYPTED TEXT IS : "+clearStr);
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}

}

}

- Next message: Jan Luehe: "Re: HELP : JCE : How to use KeyPairGenerator with Cipher"
- Previous message: Jan Luehe: "Re: HELP : JCE : How to use KeyPairGenerator with Cipher"