Creates a 1024 bit RSA key pair and stores it to the filesystem as two files : RSA algorithm « Security « Java Tutorial






import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class MainClass {
  public static void main(String[] args) throws Exception {
    String password = "password";

    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(1024);
    KeyPair keyPair = keyPairGenerator.genKeyPair();
    String publicKeyFilename = "public";

    byte[] publicKeyBytes = keyPair.getPublic().getEncoded();

    FileOutputStream fos = new FileOutputStream(publicKeyFilename);
    fos.write(publicKeyBytes);
    fos.close();

    String privateKeyFilename = "privateKeyFilename";

    byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();

    byte[] encryptedPrivateKeyBytes = passwordEncrypt(password.toCharArray(), privateKeyBytes);

    fos = new FileOutputStream(privateKeyFilename);
    fos.write(encryptedPrivateKeyBytes);
    fos.close();
  }

  private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);

    PBEKeySpec keySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
    SecretKey key = keyFactory.generateSecret(keySpec);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
  }
}








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