Search in sources :

Example 46 with SecretKey

use of javax.crypto.SecretKey in project Talon-for-Twitter by klinker24.

the class TwitterMultipleImageHelper method computeSignature.

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKey secretKey = null;
    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);
    byte[] text = baseString.getBytes();
    return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Example 47 with SecretKey

use of javax.crypto.SecretKey in project Android-Terminal-Emulator by jackpal.

the class ShortcutEncryption method generateKeys.

/**
     * Generates new secret keys suitable for the encryption scheme described
     * above.
     *
     * @throws GeneralSecurityException if an error occurs during key generation.
     */
public static Keys generateKeys() throws GeneralSecurityException {
    KeyGenerator gen = KeyGenerator.getInstance(ENC_ALGORITHM);
    gen.init(KEYLEN);
    SecretKey encKey = gen.generateKey();
    /* XXX: It's probably unnecessary to create a different keygen for the
         * MAC, but JCA's API design suggests we should just in case ... */
    gen = KeyGenerator.getInstance(MAC_ALGORITHM);
    gen.init(KEYLEN);
    SecretKey macKey = gen.generateKey();
    return new Keys(encKey, macKey);
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 48 with SecretKey

use of javax.crypto.SecretKey in project hadoop by apache.

the class RMStateStore method getCredentialsFromAppAttempt.

public Credentials getCredentialsFromAppAttempt(RMAppAttempt appAttempt) {
    Credentials credentials = new Credentials();
    SecretKey clientTokenMasterKey = appAttempt.getClientTokenMasterKey();
    if (clientTokenMasterKey != null) {
        credentials.addSecretKey(AM_CLIENT_TOKEN_MASTER_KEY_NAME, clientTokenMasterKey.getEncoded());
    }
    return credentials;
}
Also used : SecretKey(javax.crypto.SecretKey) Credentials(org.apache.hadoop.security.Credentials)

Example 49 with SecretKey

use of javax.crypto.SecretKey in project RxCache by VictorAlbertos.

the class BuiltInEncryptor method generateSecretKey.

private SecretKeySpec generateSecretKey(String key) throws Exception {
    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
    secureRandom.setSeed(key.getBytes("UTF-8"));
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(KEY_LENGTH, secureRandom);
    SecretKey secretKey = keyGenerator.generateKey();
    return new SecretKeySpec(secretKey.getEncoded(), "AES");
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator)

Example 50 with SecretKey

use of javax.crypto.SecretKey in project DataX by alibaba.

the class SecretUtil method encrypt3DES.

/**
     * 加密 DESede<br>
     * 用密钥加密
     *
     * @param data 裸的原始数据
     * @param key  加密的密钥
     * @return 结果也采用base64加密
     * @throws Exception
     */
public static String encrypt3DES(String data, String key) {
    try {
        // 生成密钥
        SecretKey desKey = new SecretKeySpec(build3DesKey(key), KEY_ALGORITHM_3DES);
        // 对数据加密
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_3DES);
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        return encryptBASE64(cipher.doFinal(data.getBytes(ENCODING)));
    } catch (Exception e) {
        throw DataXException.asDataXException(FrameworkErrorCode.SECRET_ERROR, "3重DES加密出错", e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DataXException(com.alibaba.datax.common.exception.DataXException)

Aggregations

SecretKey (javax.crypto.SecretKey)491 Cipher (javax.crypto.Cipher)176 SecretKeySpec (javax.crypto.spec.SecretKeySpec)141 KeyGenerator (javax.crypto.KeyGenerator)121 SecretKeyFactory (javax.crypto.SecretKeyFactory)89 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)87 SecureRandom (java.security.SecureRandom)61 InvalidKeyException (java.security.InvalidKeyException)58 PBEKeySpec (javax.crypto.spec.PBEKeySpec)58 IvParameterSpec (javax.crypto.spec.IvParameterSpec)46 IOException (java.io.IOException)44 Test (org.junit.Test)40 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)35 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)34 KeyStore (java.security.KeyStore)32 PrivateKey (java.security.PrivateKey)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 KeyStoreException (java.security.KeyStoreException)29 BadPaddingException (javax.crypto.BadPaddingException)29 Mac (javax.crypto.Mac)29