AlgorithmParameters implGetParameters() {
   AlgorithmParameters params = null;
   if (salt == null) {
     // Cipher is not initialized with parameters;
     // follow the recommendation in PKCS12 v1.0
     // section B.4 to generate salt and iCount.
     salt = new byte[DEFAULT_SALT_LENGTH];
     SunJCE.getRandom().nextBytes(salt);
     iCount = DEFAULT_COUNT;
   }
   PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iCount);
   try {
     params = AlgorithmParameters.getInstance(pbeAlgo, "SunJCE");
   } catch (GeneralSecurityException gse) {
     // should never happen
     throw new RuntimeException("SunJCE provider is not configured properly");
   }
   try {
     params.init(pbeSpec);
   } catch (InvalidParameterSpecException ipse) {
     // should never happen
     throw new RuntimeException("PBEParameterSpec not supported");
   }
   return params;
 }
Exemple #2
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();
    }
  }
Exemple #3
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);
   }
 }
  /** 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());
  }
Exemple #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);
   }
 }
Exemple #6
0
  /*
   * Generate PBE Algorithm Parameters
   */
  private AlgorithmParameters getAlgorithmParameters(String algorithm) throws IOException {
    AlgorithmParameters algParams = null;

    // create PBE parameters from salt and iteration count
    PBEParameterSpec paramSpec = new PBEParameterSpec(getSalt(), iterationCount);
    try {
      algParams = AlgorithmParameters.getInstance(algorithm);
      algParams.init(paramSpec);
    } catch (Exception e) {
      IOException ioe = new IOException("getAlgorithmParameters failed: " + e.getMessage());
      ioe.initCause(e);
      throw ioe;
    }
    return algParams;
  }
 // see JCE spec
 protected AlgorithmParameters engineGetParameters() {
   if (iv == null) {
     return null;
   }
   IvParameterSpec ivSpec = new IvParameterSpec(iv);
   try {
     AlgorithmParameters params =
         AlgorithmParameters.getInstance(keyAlgorithm, P11Util.getSunJceProvider());
     params.init(ivSpec);
     return params;
   } catch (GeneralSecurityException e) {
     // NoSuchAlgorithmException, NoSuchProviderException
     // InvalidParameterSpecException
     throw new ProviderException("Could not encode parameters", e);
   }
 }
  private AlgorithmParameters getParameters() throws NoSuchAlgorithmException {
    AlgorithmParameters ap = AlgorithmParameters.getInstance(this.getAlgName());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);

    try {
      dOut.writeObject(infoObj.getEncryptionAlgorithm().getParameters());
      dOut.close();

      ap.init(bOut.toByteArray());
    } catch (IOException e) {
      throw new NoSuchAlgorithmException("unable to parse parameters");
    }

    return ap;
  }
 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);
 }
Exemple #10
0
 /*
  * parse Algorithm Parameters
  */
 private AlgorithmParameters parseAlgParameters(DerInputStream in) throws IOException {
   AlgorithmParameters algParams = null;
   try {
     DerValue params;
     if (in.available() == 0) {
       params = null;
     } else {
       params = in.getDerValue();
       if (params.tag == DerValue.tag_Null) {
         params = null;
       }
     }
     if (params != null) {
       algParams = AlgorithmParameters.getInstance("PBE");
       algParams.init(params.toByteArray());
     }
   } catch (Exception e) {
     IOException ioe = new IOException("parseAlgParameters failed: " + e.getMessage());
     ioe.initCause(e);
     throw ioe;
   }
   return algParams;
 }
  /**
   * Returns a table listing the input parameters of this algorithm (which should match up with the
   * scripting parameters used in {@link #setGUIFromParams()}).
   *
   * @return A parameter table listing the inputs of this algorithm.
   */
  public ParameterTable createInputParameters() {
    final ParameterTable table = new ParameterTable();

    try {

      table.put(new ParameterExternalImage(AlgorithmParameters.getInputImageLabel(1)));
      table.put(new ParameterBoolean(AlgorithmParameters.DO_OUTPUT_NEW_IMAGE, true));
    } catch (final ParserException e) {
      // this shouldn't really happen since there isn't any real parsing going on...
      e.printStackTrace();
    }

    return table;
  }
  /**
   * Constructs an <code>EncryptedPrivateKeyInfo</code> from the encryption algorithm parameters and
   * the encrypted data.
   *
   * <p>Note: the <code>encrypedData</code> is cloned when constructing this object.
   *
   * @param algParams the algorithm parameters for the encryption algorithm. <code>
   *     algParams.getEncoded()</code> should return the ASN.1 encoded bytes of the <code>parameters
   *     </code> field of the <code>AlgorithmIdentifer</code> component of the <code>
   *     EncryptedPrivateKeyInfo</code> type.
   * @param encryptedData encrypted data.
   * @exception NullPointerException if <code>algParams</code> or <code>encryptedData</code> is
   *     null.
   * @exception IllegalArgumentException if <code>encryptedData</code> is empty, i.e. 0-length.
   * @exception NoSuchAlgorithmException if the specified algName of the specified <code>algParams
   *     </code> parameter is not supported.
   */
  public EncryptedPrivateKeyInfo(AlgorithmParameters algParams, byte[] encryptedData)
      throws NullPointerException, IllegalArgumentException, NoSuchAlgorithmException {
    if (algParams == null || encryptedData == null) {
      throw new NullPointerException("parameters null");
    }

    org.bouncycastle.asn1.x509.AlgorithmIdentifier kAlgId = null;

    try {
      ByteArrayInputStream bIn = new ByteArrayInputStream(algParams.getEncoded());
      ASN1InputStream dIn = new ASN1InputStream(bIn);

      kAlgId =
          new AlgorithmIdentifier(
              new DERObjectIdentifier(algParams.getAlgorithm()), dIn.readObject());
    } catch (IOException e) {
      throw new IllegalArgumentException("error in encoding: " + e.toString());
    }

    infoObj =
        new org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo(
            kAlgId, (byte[]) encryptedData.clone());
    algP = this.getParameters();
  }
Exemple #13
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);
   }
 }
 // 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);
 }
  /**
   * Returns a table listing the input parameters of this algorithm (which should match up with the
   * scripting parameters used in {@link #setGUIFromParams()}).
   *
   * @return A parameter table listing the inputs of this algorithm.
   */
  public ParameterTable createInputParameters() {
    final ParameterTable table = new ParameterTable();

    try {
      table.put(new ParameterExternalImage(AlgorithmParameters.getInputImageLabel(1)));
      table.put(new ParameterBoolean(AlgorithmParameters.DO_OUTPUT_NEW_IMAGE, true));
      table.put(new ParameterBoolean(AlgorithmParameters.DO_PROCESS_WHOLE_IMAGE, true));
      table.put(new ParameterBoolean(AlgorithmParameters.DO_PROCESS_3D_AS_25D, false));
      table.put(new ParameterInt("search_window_side", 15));
      table.put(new ParameterInt("similarity_window_side", 7));
      table.put(new ParameterFloat("noise_standard_deviation", 10f));
      table.put(new ParameterFloat("degree_of_filtering", 1.414f));
      table.put(new ParameterBoolean("do_rician", false));
    } catch (final ParserException e) {
      // this shouldn't really happen since there isn't any real parsing going on...
      e.printStackTrace();
    }

    return table;
  }
  /** {@inheritDoc} */
  protected void setGUIFromParams() {
    image = scriptParameters.retrieveInputImage();
    parentFrame = image.getParentFrame();

    if (!image.isColorImage()) {
      throw new ParameterException(
          AlgorithmParameters.getInputImageLabel(1), "Source Image must be Color");
    }

    setBlueMin(scriptParameters.getParams().getInt("blue_min"));
    setRedMin(scriptParameters.getParams().getInt("red_min"));
    setRedFraction(scriptParameters.getParams().getFloat("red_fraction"));
    setMergingDistance(scriptParameters.getParams().getFloat("merging_distance"));
    setGreenMin(scriptParameters.getParams().getInt("green_min"));
    setGreenFraction(scriptParameters.getParams().getFloat("green_fraction"));
    setGreenRegionNumber(scriptParameters.getParams().getInt("green_region_number"));
    setTwoGreenLevels(scriptParameters.getParams().getBoolean("two_green_levels"));
    setBlueBoundaryFraction(scriptParameters.getParams().getFloat("blue_boundary_fraction"));
    setBlueSmooth(scriptParameters.getParams().getBoolean("blue_smooth"));
    setInterpolationDivisor(scriptParameters.getParams().getFloat("interpolation_divisor"));
  }
  /**
   * Store the result image in the script runner's image table now that the action execution is
   * finished.
   */
  protected void doPostAlgorithmActions() {

    if (displayLoc == NEW) {
      AlgorithmParameters.storeImageInRunner(getResultImage());
    }
  }