Search in sources :

Example 66 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project c-geo by just-radovan.

the class cgBase method hashHmac.

public static byte[] hashHmac(String text, String salt) {
    byte[] macBytes = {};
    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKeySpec);
        macBytes = mac.doFinal(text.getBytes());
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgBase.hashHmac: " + e.toString());
    }
    return macBytes;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Example 67 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project hbase by apache.

the class TestHBaseFsckEncryption method setUp.

@Before
public void setUp() throws Exception {
    conf = TEST_UTIL.getConfiguration();
    conf.setInt("hfile.format.version", 3);
    conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
    conf.set(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, "hbase");
    // Create the test encryption key
    SecureRandom rng = new SecureRandom();
    byte[] keyBytes = new byte[AES.KEY_LENGTH];
    rng.nextBytes(keyBytes);
    String algorithm = conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
    cfKey = new SecretKeySpec(keyBytes, algorithm);
    // Start the minicluster
    TEST_UTIL.startMiniCluster(3);
    // Create the table
    htd = new HTableDescriptor(TableName.valueOf("default", "TestHBaseFsckEncryption"));
    HColumnDescriptor hcd = new HColumnDescriptor("cf");
    hcd.setEncryptionType(algorithm);
    hcd.setEncryptionKey(EncryptionUtil.wrapKey(conf, conf.get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY, User.getCurrent().getShortName()), cfKey));
    htd.addFamily(hcd);
    TEST_UTIL.getAdmin().createTable(htd);
    TEST_UTIL.waitTableAvailable(htd.getName(), 5000);
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyProviderForTesting(org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Before(org.junit.Before)

Example 68 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project openhab1-addons by openhab.

the class KM200Comm method encodeMessage.

/**
     * This function does the encoding for a new message to the device
     *
     */
public byte[] encodeMessage(String data) {
    byte[] encryptedDataB64 = null;
    try {
        // --- create cipher
        byte[] bdata = data.getBytes(device.getCharSet());
        final Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(device.getCryptKeyPriv(), "AES"));
        logger.debug("Create padding..");
        int bsize = cipher.getBlockSize();
        logger.debug("Add Padding and Encrypt AES..");
        final byte[] encryptedData = cipher.doFinal(addZeroPadding(bdata, bsize, device.getCharSet()));
        logger.debug("Encrypt B64..");
        try {
            encryptedDataB64 = Base64.encodeBase64(encryptedData);
        } catch (Exception e) {
            logger.error("Base64encoding not possible: {}", e.getMessage());
        }
        return encryptedDataB64;
    } catch (UnsupportedEncodingException | GeneralSecurityException e) {
        // failure to authenticate
        logger.error("Exception on encoding: {}", e);
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Cipher(javax.crypto.Cipher) JSONException(org.json.JSONException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 69 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project OpenAM by OpenRock.

the class CreateSoapSTSDeployment method setAgentPasswordEncryptionKeyEntry.

private void setAgentPasswordEncryptionKeyEntry(KeyStore soapSTSKeystore, char[] keystorePassword, String agentPasswordEncryptionKey) throws KeyStoreException {
    //the keystore password will also protected the password encryption key
    KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keystorePassword);
    SecretKey agentPasswordEncryptionSecretKey = new SecretKeySpec(agentPasswordEncryptionKey.getBytes(StandardCharsets.US_ASCII), SECRET_KEY_ALGORITHM_TYPE);
    KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(agentPasswordEncryptionSecretKey);
    soapSTSKeystore.setEntry(SharedSTSConstants.AM_INTERNAL_PEK_ALIAS, secretKeyEntry, protParam);
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeyStore(java.security.KeyStore)

Example 70 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project XobotOS by xamarin.

the class JCEKeyGenerator method engineGenerateKey.

protected SecretKey engineGenerateKey() {
    if (uninitialised) {
        engine.init(new KeyGenerationParameters(new SecureRandom(), defaultKeySize));
        uninitialised = false;
    }
    return new SecretKeySpec(engine.generateKey(), algName);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters)

Aggregations

SecretKeySpec (javax.crypto.spec.SecretKeySpec)498 Cipher (javax.crypto.Cipher)194 SecretKey (javax.crypto.SecretKey)142 Mac (javax.crypto.Mac)110 IvParameterSpec (javax.crypto.spec.IvParameterSpec)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)94 InvalidKeyException (java.security.InvalidKeyException)67 IOException (java.io.IOException)44 Key (java.security.Key)36 SecureRandom (java.security.SecureRandom)30 Test (org.junit.Test)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)29 GeneralSecurityException (java.security.GeneralSecurityException)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)27 MessageDigest (java.security.MessageDigest)25 BadPaddingException (javax.crypto.BadPaddingException)25 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)25 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 PrivateKey (java.security.PrivateKey)18 PublicKey (java.security.PublicKey)16