Search in sources :

Example 61 with AlgorithmParameters

use of java.security.AlgorithmParameters in project robovm by robovm.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    DSAParametersGenerator pGen;
    if (strength <= 1024) {
        pGen = new DSAParametersGenerator();
    } else {
        pGen = new DSAParametersGenerator(new SHA256Digest());
    }
    if (random == null) {
        random = new SecureRandom();
    }
    if (strength == 1024) {
        params = new DSAParameterGenerationParameters(1024, 160, 80, random);
        pGen.init(params);
    } else if (strength > 1024) {
        params = new DSAParameterGenerationParameters(strength, 256, 80, random);
        pGen.init(params);
    } else {
        pGen.init(strength, 20, random);
    }
    DSAParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = AlgorithmParameters.getInstance("DSA", BouncyCastleProvider.PROVIDER_NAME);
        params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : DSAParametersGenerator(org.bouncycastle.crypto.generators.DSAParametersGenerator) DSAParameterSpec(java.security.spec.DSAParameterSpec) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(org.bouncycastle.crypto.params.DSAParameterGenerationParameters) SecureRandom(java.security.SecureRandom) DSAParameters(org.bouncycastle.crypto.params.DSAParameters) InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 62 with AlgorithmParameters

use of java.security.AlgorithmParameters in project robovm by robovm.

the class SealedObject method getObject.

/**
     * Returns the wrapped object, decrypting it using the specified key.
     *
     * @param key
     *            the key to decrypt the data with.
     * @return the encapsulated object.
     * @throws IOException
     *             if deserialization fails.
     * @throws ClassNotFoundException
     *             if deserialization fails.
     * @throws NoSuchAlgorithmException
     *             if the algorithm to decrypt the data is not available.
     * @throws InvalidKeyException
     *             if the specified key cannot be used to decrypt the data.
     */
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    try {
        Cipher cipher = Cipher.getInstance(sealAlg);
        if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
            AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
            params.init(encodedParams);
            cipher.init(Cipher.DECRYPT_MODE, key, params);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key);
        }
        byte[] serialized = cipher.doFinal(encryptedContent);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
        return ois.readObject();
    } catch (NoSuchPaddingException e) {
        // with existing padding
        throw new NoSuchAlgorithmException(e.toString());
    } catch (InvalidAlgorithmParameterException e) {
        // with correct algorithm parameters
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalBlockSizeException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (BadPaddingException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalStateException e) {
        // should never be thrown because cipher is initialized
        throw new NoSuchAlgorithmException(e.toString());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameters(java.security.AlgorithmParameters) ObjectInputStream(java.io.ObjectInputStream)

Example 63 with AlgorithmParameters

use of java.security.AlgorithmParameters in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4.

/**
     * Test #4 for
     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
     * </code>
     * constructor <br>
     * Assertion: byte array is copied to prevent subsequent modification <br>
     * Test preconditions: valid array passed then modified <br>
     * Expected: getEncryptedData(), invoked after above modification, must
     * return array as it was before the modification
     *
     * @throws IOException
     */
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray4() throws Exception {
    AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
    // use pregenerated AlgorithmParameters encodings
    ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
    byte[] encryptedDataCopy = EncryptedPrivateKeyInfoData.encryptedData.clone();
    // pass valid array
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap, encryptedDataCopy);
    // modify array passed
    encryptedDataCopy[0] = (byte) 6;
    // check that internal state has not been affected
    assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData, epki.getEncryptedData()));
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) AlgorithmParameters(java.security.AlgorithmParameters)

Example 64 with AlgorithmParameters

use of java.security.AlgorithmParameters in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testGetEncoded03.

/**
     * Test #3 for <code>getEncoded()</code> method <br>
     * Assertion: returns the ASN.1 encoding of this object <br>
     * Test preconditions: test object created using ctor which takes algorithm
     * name and encrypted data as a parameters <br>
     * Expected: equivalent encoded form (without alg params) must be returned
     *
     * @throws IOException
     */
public final void testGetEncoded03() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
            // use pregenerated AlgorithmParameters encodings
            ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]));
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap, EncryptedPrivateKeyInfoData.encryptedData);
            // check that method under test returns
            // valid encoded form
            assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]), epki.getEncoded()));
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 65 with AlgorithmParameters

use of java.security.AlgorithmParameters in project robovm by robovm.

the class KeyAgreementThread method test.

@Override
public void test() throws Exception {
    AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
    apg.init(1024, new SecureRandom());
    AlgorithmParameters ap = apg.generateParameters();
    DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
    KeyAgreementGen kag1 = new KeyAgreementGen(ps);
    KeyAgreementGen kag2 = new KeyAgreementGen(ps);
    byte[] bArray1 = kag1.getPublicKeyBytes();
    byte[] bArray2 = kag2.getPublicKeyBytes();
    byte[] sk1 = kag1.getSecretKey(algName, bArray2);
    byte[] sk2 = kag2.getSecretKey(algName, bArray1);
    if (Arrays.areEqual(sk1, sk2) == false) {
        throw new Exception("Generated keys are not the same");
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5