Java by API/java.security/KeyPairGenerator

Материал из Java эксперт
Версия от 14:47, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

KeyPairGenerator: generateKeyPair()

  

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
public class Main {
  public static void main(String[] args) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
    kpg.initialize(1024, new SecureRandom());
    KeyPair dsaKeyPair = kpg.generateKeyPair();
  }
}





KeyPairGenerator: genKeyPair()

  
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main {
  public static void main(String[] args) {
    if (args.length != 2) {
      String err = "Usage: KeyGeneratorApp algorithmName keySize";
      System.out.println(err);
      System.exit(0);
    }
    int keySize = (new Integer(args[1])).intValue();
    SecretKey skey = null;
    KeyPair keys = null;
    String algorithm = args[0];
    try {
      KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
      kpg.initialize(keySize);
      keys = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException ex1) {
      try {
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(keySize);
        skey = kg.generateKey();
      } catch (NoSuchAlgorithmException ex2) {
        System.out.println("Algorithm not supported: " + algorithm);
        System.exit(0);
      }
    }
  }
}





KeyPairGenerator: getInstance(String algorithm) throws NoSuchAlgorithmException

  
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main {
  public static void main(String[] args) {
    if (args.length != 2) {
      String err = "Usage: KeyGeneratorApp algorithmName keySize";
      System.out.println(err);
      System.exit(0);
    }
    int keySize = (new Integer(args[1])).intValue();
    SecretKey skey = null;
    KeyPair keys = null;
    String algorithm = args[0];
    try {
      KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
      kpg.initialize(keySize);
      keys = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException ex1) {
      try {
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(keySize);
        skey = kg.generateKey();
      } catch (NoSuchAlgorithmException ex2) {
        System.out.println("Algorithm not supported: " + algorithm);
        System.exit(0);
      }
    }
  }
}





KeyPairGenerator: initialize(int keysize)

  
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main {
  public static void main(String[] args) {
    if (args.length != 2) {
      String err = "Usage: KeyGeneratorApp algorithmName keySize";
      System.out.println(err);
      System.exit(0);
    }
    int keySize = (new Integer(args[1])).intValue();
    SecretKey skey = null;
    KeyPair keys = null;
    String algorithm = args[0];
    try {
      KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
      kpg.initialize(keySize);
      keys = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException ex1) {
      try {
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(keySize);
        skey = kg.generateKey();
      } catch (NoSuchAlgorithmException ex2) {
        System.out.println("Algorithm not supported: " + algorithm);
        System.exit(0);
      }
    }
  }
}