Search in sources :

Example 6 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project robovm by robovm.

the class DSAPublicKeyTest method test_getY.

/**
     * java.security.interfaces.DSAPublicKey
     * #getY()
     * test covers following use cases
     *   Case 1: check with predefined p, q, g, x
     *   Case 2: check with random p, q, g, x. It takes some time (up to
     *           minute)
     */
public void test_getY() throws Exception {
    KeyPairGenerator keyGen = null;
    KeyPair keys = null;
    DSAPrivateKey priv = null;
    DSAPublicKey publ = null;
    // Case 1: check with predefined p, q, g, x
    keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(new DSAParameterSpec(Util.P, Util.Q, Util.G), new SecureRandom(new MySecureRandomSpi(), null) {
    });
    keys = keyGen.generateKeyPair();
    priv = (DSAPrivateKey) keys.getPrivate();
    publ = (DSAPublicKey) keys.getPublic();
    assertNotNull("Invalid Y value", publ.getY());
    // Case 2: check with random p, q, g, x. It takes some time (up to
    // minute)
    keyGen = KeyPairGenerator.getInstance("DSA");
    keys = keyGen.generateKeyPair();
    priv = (DSAPrivateKey) keys.getPrivate();
    publ = (DSAPublicKey) keys.getPublic();
    assertNotNull("Invalid Y value", publ.getY());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPair(java.security.KeyPair) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 7 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project robovm by robovm.

the class KeyPairGenerator4Test method test_initializeLjava_security_spec_AlgorithmParameterSpec.

/**
     * java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec)
     */
public void test_initializeLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    // create DSAParams
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    keyPairGenerator.initialize(1024);
    DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair().getPublic();
    DSAParams params = key.getParams();
    KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
    keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()));
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 8 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project robovm by robovm.

the class KeyPairGenerator4Test method test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom.

/**
     * java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec,
     *        java.security.SecureRandom)
     */
public void test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() throws Exception {
    // create DSAParams
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    keyPairGenerator.initialize(1024);
    DSAPublicKey key = (DSAPublicKey) keyPairGenerator.genKeyPair().getPublic();
    DSAParams params = key.getParams();
    KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
    keyPair.initialize(new DSAParameterSpec(params.getP(), params.getQ(), params.getG()), new SecureRandom());
}
Also used : DSAParameterSpec(java.security.spec.DSAParameterSpec) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) DSAParams(java.security.interfaces.DSAParams) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 9 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project Conversations by siacs.

the class Conversation method getOtrFingerprint.

public synchronized String getOtrFingerprint() {
    if (this.otrFingerprint == null) {
        try {
            if (getOtrSession() == null || getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
                return null;
            }
            DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey();
            this.otrFingerprint = getAccount().getOtrService().getFingerprint(remotePubKey).toLowerCase(Locale.US);
        } catch (final OtrCryptoException | UnsupportedOperationException ignored) {
            return null;
        }
    }
    return this.otrFingerprint;
}
Also used : OtrCryptoException(net.java.otr4j.crypto.OtrCryptoException) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 10 with DSAPublicKey

use of java.security.interfaces.DSAPublicKey in project carat by amplab.

the class SamplingLibrary method getSignatures.

public static List<String> getSignatures(PackageInfo pak) {
    List<String> sigList = new LinkedList<String>();
    String[] pmInfos = pak.requestedPermissions;
    if (pmInfos != null) {
        byte[] bytes = getPermissionBytes(pmInfos);
        String hexB = convertToHex(bytes);
        sigList.add(hexB);
    }
    Signature[] sigs = pak.signatures;
    for (Signature s : sigs) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(s.toByteArray());
            byte[] dig = md.digest();
            // Add SHA-1
            sigList.add(convertToHex(dig));
            CertificateFactory fac = CertificateFactory.getInstance("X.509");
            if (fac == null)
                continue;
            X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
            if (cert == null)
                continue;
            PublicKey pkPublic = cert.getPublicKey();
            if (pkPublic == null)
                continue;
            String al = pkPublic.getAlgorithm();
            if (al.equals("RSA")) {
                md = MessageDigest.getInstance("SHA-256");
                RSAPublicKey rsa = (RSAPublicKey) pkPublic;
                byte[] data = rsa.getModulus().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of modulus
                sigList.add(convertToHex(dig));
            } else if (al.equals("DSA")) {
                DSAPublicKey dsa = (DSAPublicKey) pkPublic;
                md = MessageDigest.getInstance("SHA-256");
                byte[] data = dsa.getY().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of public key (DSA)
                sigList.add(convertToHex(dig));
            } else {
                Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
            }
        } catch (NoSuchAlgorithmException e) {
        // Do nothing
        } catch (CertificateException e) {
        // Do nothing
        }
    }
    return sigList;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) MessageDigest(java.security.MessageDigest)

Aggregations

DSAPublicKey (java.security.interfaces.DSAPublicKey)31 DSAParams (java.security.interfaces.DSAParams)19 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)11 PublicKey (java.security.PublicKey)10 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)10 BigInteger (java.math.BigInteger)9 RSAPublicKey (java.security.interfaces.RSAPublicKey)8 InvalidKeyException (java.security.InvalidKeyException)7 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)6 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)6 IOException (java.io.IOException)5 GeneralSecurityException (java.security.GeneralSecurityException)5 KeyPairGenerator (java.security.KeyPairGenerator)5 X509Certificate (java.security.cert.X509Certificate)5 KeyFactory (java.security.KeyFactory)4 KeyPair (java.security.KeyPair)4 CertPathValidatorException (java.security.cert.CertPathValidatorException)4 DSAParameterSpec (java.security.spec.DSAParameterSpec)4 CertificateException (java.security.cert.CertificateException)3 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)3