Search in sources :

Example 31 with Key

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

the class CipherInputStream1Test method test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100]);
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherInputStream cis = new CipherInputStream(bais, c);
    assertNotNull(cis);
}
Also used : CipherInputStream(javax.crypto.CipherInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) NullCipher(javax.crypto.NullCipher) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 32 with Key

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

the class EncryptedPrivateKeyInfoTest method testGetKeySpecKeyProvider01.

public final void testGetKeySpecKeyProvider01() throws Exception {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            try {
                // check that method under test throws NPE
                epki.getKeySpec((Key) null, (Provider) null);
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            }
            try {
                // check that method under test throws NPE
                epki.getKeySpec(new Key() {

                    public String getAlgorithm() {
                        return "alg";
                    }

                    public String getFormat() {
                        return "fmt";
                    }

                    public byte[] getEncoded() {
                        return new byte[] {};
                    }
                }, (Provider) null);
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            }
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Key(java.security.Key)

Example 33 with Key

use of java.security.Key in project buck by facebook.

the class ApkBuilderStep method createKeystoreProperties.

private PrivateKeyAndCertificate createKeystoreProperties() throws IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
    KeyStore keystore = KeyStore.getInstance(JARSIGNER_KEY_STORE_TYPE);
    KeystoreProperties keystoreProperties = keystorePropertiesSupplier.get();
    InputStream inputStream = filesystem.getInputStreamForRelativePath(pathToKeystore);
    char[] keystorePassword = keystoreProperties.getStorepass().toCharArray();
    try {
        keystore.load(inputStream, keystorePassword);
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        throw new HumanReadableException(e, "%s is an invalid keystore.", pathToKeystore);
    }
    String alias = keystoreProperties.getAlias();
    char[] keyPassword = keystoreProperties.getKeypass().toCharArray();
    Key key = keystore.getKey(alias, keyPassword);
    // key can be null if alias/password is incorrect.
    if (key == null) {
        throw new HumanReadableException("The keystore [%s] key.alias [%s] does not exist or does not identify a key-related " + "entry", pathToKeystore, alias);
    }
    Certificate certificate = keystore.getCertificate(alias);
    return new PrivateKeyAndCertificate((PrivateKey) key, (X509Certificate) certificate);
}
Also used : InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) HumanReadableException(com.facebook.buck.util.HumanReadableException) Key(java.security.Key) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 34 with Key

use of java.security.Key in project hbase by apache.

the class TestEncryptionUtil method testKeyWrapping.

// There does not seem to be a ready way to test either getKeyFromBytesOrMasterKey
// or createEncryptionContext, and the existing code under MobUtils appeared to be
// untested.  Not ideal!
@Test
public void testKeyWrapping() throws Exception {
    // set up the key provider for testing to resolve a key for our test subject
    // we don't need HBaseConfiguration for this
    Configuration conf = new Configuration();
    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
    // generate a test key
    byte[] keyBytes = new byte[AES.KEY_LENGTH];
    new SecureRandom().nextBytes(keyBytes);
    String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
    Key key = new SecretKeySpec(keyBytes, algorithm);
    // wrap the test key
    byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
    assertNotNull(wrappedKeyBytes);
    // unwrap
    Key unwrappedKey = EncryptionUtil.unwrapKey(conf, "hbase", wrappedKeyBytes);
    assertNotNull(unwrappedKey);
    // only secretkeyspec supported for now
    assertTrue(unwrappedKey instanceof SecretKeySpec);
    // did we get back what we wrapped?
    assertTrue("Unwrapped key bytes do not match original", Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
    // unwrap with an incorrect key
    try {
        EncryptionUtil.unwrapKey(conf, "other", wrappedKeyBytes);
        fail("Unwrap with incorrect key did not throw KeyException");
    } catch (KeyException e) {
    // expected
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyProviderForTesting(org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting) Key(java.security.Key) KeyException(java.security.KeyException) Test(org.junit.Test)

Example 35 with Key

use of java.security.Key in project hbase by apache.

the class TestEncryptionUtil method testWALKeyWrappingWithIncorrectKey.

@Test(expected = KeyException.class)
public void testWALKeyWrappingWithIncorrectKey() throws Exception {
    // set up the key provider for testing to resolve a key for our test subject
    // we don't need HBaseConfiguration for this
    Configuration conf = new Configuration();
    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
    // generate a test key
    byte[] keyBytes = new byte[AES.KEY_LENGTH];
    new SecureRandom().nextBytes(keyBytes);
    String algorithm = conf.get(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
    Key key = new SecretKeySpec(keyBytes, algorithm);
    // wrap the test key
    byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
    assertNotNull(wrappedKeyBytes);
    // unwrap with an incorrect key
    EncryptionUtil.unwrapWALKey(conf, "other", wrappedKeyBytes);
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyProviderForTesting(org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting) Key(java.security.Key) Test(org.junit.Test)

Aggregations

Key (java.security.Key)302 PrivateKey (java.security.PrivateKey)112 SecretKey (javax.crypto.SecretKey)83 KeyStore (java.security.KeyStore)64 PublicKey (java.security.PublicKey)62 Cipher (javax.crypto.Cipher)60 X509Certificate (java.security.cert.X509Certificate)57 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)50 Test (org.junit.Test)44 IOException (java.io.IOException)42 ByteArrayInputStream (java.io.ByteArrayInputStream)38 Certificate (java.security.cert.Certificate)36 SecretKeySpec (javax.crypto.spec.SecretKeySpec)36 KeyFactory (java.security.KeyFactory)35 InvalidKeyException (java.security.InvalidKeyException)32 KeyGenerator (javax.crypto.KeyGenerator)32 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)26 KeyStoreException (java.security.KeyStoreException)22 SecureRandom (java.security.SecureRandom)21 IvParameterSpec (javax.crypto.spec.IvParameterSpec)21