Esempio n. 1
0
  public static void main(String[] args) {
    try {

      AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance("DSA");
      paramGen.init(1024);

      AlgorithmParameters params = paramGen.generateParameters();

      DSAParameterSpec dsaParameterSpec = params.getParameterSpec(DSAParameterSpec.class);

      KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
      keyPairGenerator.initialize(dsaParameterSpec);

      KeyPair keyPair = keyPairGenerator.generateKeyPair();

      PublicKey publicKey = keyPair.getPublic();
      PrivateKey privateKey = keyPair.getPrivate();

      saveKey("BpubKey", publicKey);
      saveKey("BprivKey", privateKey);

    } catch (NoSuchAlgorithmException
        | InvalidParameterSpecException
        | InvalidAlgorithmParameterException e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
 /** Parse the key. Called by X509Key. */
 protected void parseKeyBits() throws InvalidKeyException {
   try {
     AlgorithmParameters algParams = this.algid.getParameters();
     params = algParams.getParameterSpec(ECParameterSpec.class);
     w = ECParameters.decodePoint(key, params.getCurve());
   } catch (IOException e) {
     throw new InvalidKeyException("Invalid EC key", e);
   } catch (InvalidParameterSpecException e) {
     throw new InvalidKeyException("Invalid EC key", e);
   }
 }
Esempio n. 3
0
  /** Can be used to generate new parameters */
  public static void main(String[] args) throws Exception {
    AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance(ALGORITHM);
    paramGen.init(1024);

    AlgorithmParameters params = paramGen.generateParameters();

    DHParameterSpec dhSpec = params.getParameterSpec(DHParameterSpec.class);
    System.out.println("l=" + dhSpec.getL());
    System.out.println("g=" + dhSpec.getG());
    System.out.println("p=" + dhSpec.getP());
  }
Esempio n. 4
0
 void implInit(
     int opmode, Key key, AlgorithmParameters params, SecureRandom random, CipherSpi cipherImpl)
     throws InvalidKeyException, InvalidAlgorithmParameterException {
   AlgorithmParameterSpec paramSpec = null;
   if (params != null) {
     try {
       paramSpec = params.getParameterSpec(PBEParameterSpec.class);
     } catch (InvalidParameterSpecException ipse) {
       throw new InvalidAlgorithmParameterException("requires PBE parameters");
     }
   }
   implInit(opmode, key, paramSpec, random, cipherImpl);
 }
Esempio n. 5
0
 protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
     throws InvalidKeyException, InvalidAlgorithmParameterException {
   if (params != null && params.getAlgorithm().equals("RC2")) {
     try {
       RC2ParameterSpec rc2Params = params.getParameterSpec(RC2ParameterSpec.class);
       engineInit(opmode, key, rc2Params, random);
     } catch (InvalidParameterSpecException ipse) {
       throw new InvalidAlgorithmParameterException("Wrong parameter type: RC2 expected");
     }
   } else {
     embeddedCipher.initEffectiveKeyBits(0);
     core.init(opmode, key, params, random);
   }
 }
Esempio n. 6
0
 // see JCE spec
 protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random)
     throws InvalidKeyException, InvalidAlgorithmParameterException {
   byte[] ivValue;
   if (params != null) {
     try {
       IvParameterSpec ivSpec = (IvParameterSpec) params.getParameterSpec(IvParameterSpec.class);
       ivValue = ivSpec.getIV();
     } catch (InvalidParameterSpecException e) {
       throw new InvalidAlgorithmParameterException("Could not decode IV", e);
     }
   } else {
     ivValue = null;
   }
   implInit(opmode, key, ivValue, random);
 }
Esempio n. 7
0
 /** Parse the key. Called by PKCS8Key. */
 protected void parseKeyBits() throws InvalidKeyException {
   try {
     DerInputStream in = new DerInputStream(key);
     DerValue derValue = in.getDerValue();
     if (derValue.tag != DerValue.tag_Sequence) {
       throw new IOException("Not a SEQUENCE");
     }
     DerInputStream data = derValue.data;
     int version = data.getInteger();
     if (version != 1) {
       throw new IOException("Version must be 1");
     }
     byte[] privData = data.getOctetString();
     s = new BigInteger(1, privData);
     while (data.available() != 0) {
       DerValue value = data.getDerValue();
       if (value.isContextSpecific((byte) 0)) {
         // ignore for now
       } else if (value.isContextSpecific((byte) 1)) {
         // ignore for now
       } else {
         throw new InvalidKeyException("Unexpected value: " + value);
       }
     }
     AlgorithmParameters algParams = this.algid.getParameters();
     if (algParams == null) {
       throw new InvalidKeyException(
           "EC domain parameters must be " + "encoded in the algorithm identifier");
     }
     params = algParams.getParameterSpec(ECParameterSpec.class);
   } catch (IOException e) {
     throw new InvalidKeyException("Invalid EC private key", e);
   } catch (InvalidParameterSpecException e) {
     throw new InvalidKeyException("Invalid EC private key", e);
   }
 }