An RSA sample application : RSA algorithm « Security « Java Tutorial






import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.spec.RSAKeyGenParameterSpec;
import java.security.spec.RSAPublicKeySpec;

public class MainClass {
  public static void main(String[] args) throws Exception {
    int eValue = 79;
    int bitLength = 1024; // KeySize

    BigInteger e = e = new BigInteger(Integer.toString(eValue));

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(bitLength);
    KeyPair kp = kpg.generateKeyPair();

    KeyFactory kfactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),
        RSAPublicKeySpec.class);

    kpg = KeyPairGenerator.getInstance("RSA", "SunRsaSign");

    e = new BigInteger(Integer.toString(eValue));
    System.out.println("e =" + e);
    RSAKeyGenParameterSpec param = new RSAKeyGenParameterSpec(bitLength, e);
    kpg.initialize(param);
    kp = kpg.generateKeyPair();

    kfactory = KeyFactory.getInstance("RSA", "SunRsaSign");

    kspec = (RSAPublicKeySpec) kfactory.getKeySpec(kp.getPublic(),RSAPublicKeySpec.class);
  }
}








36.38.RSA algorithm
36.38.1.Basic RSA example.
36.38.2.RSA example with random key generation.
36.38.3.RSA example with PKCS #1 Padding.
36.38.4.RSA example with OAEP Padding and random key generation.
36.38.5.An example of using RSA to encrypt a single asymmetric key.
36.38.6.Simple Digital Signature Example
36.38.7.Creates a 1024 bit RSA key pair and stores it to the filesystem as two files
36.38.8.RSA Signature Generation
36.38.9.An RSA sample application