Search in sources :

Example 86 with SecretKey

use of javax.crypto.SecretKey in project Gradle-demo by Arisono.

the class DESUtil method encryptDES.

/**
	 * 加密
	 * @throws Exception 
	 */
public static byte[] encryptDES(byte[] data, byte[] key) throws Exception {
    //获得密钥
    SecretKey secretKey = new SecretKeySpec(key, "DES");
    //Cipher完成加密
    Cipher cipher = Cipher.getInstance("DES");
    //初始化cipher
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    //加密
    byte[] encrypt = cipher.doFinal(data);
    return encrypt;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 87 with SecretKey

use of javax.crypto.SecretKey in project Gradle-demo by Arisono.

the class DESede method initKey.

/**
	 * 生成密钥
	 * @throws Exception 
	 */
public static byte[] initKey() throws Exception {
    //密钥生成器
    KeyGenerator keyGen = KeyGenerator.getInstance("DESede");
    //初始化密钥生成器
    //可指定密钥长度为112或168,默认168
    keyGen.init(168);
    //生成密钥
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 88 with SecretKey

use of javax.crypto.SecretKey in project Gradle-demo by Arisono.

the class DESede method decrypt3DES.

/**
	 * 解密
	 */
public static byte[] decrypt3DES(byte[] data, byte[] key) throws Exception {
    //恢复密钥
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    //Cipher完成解密
    Cipher cipher = Cipher.getInstance("DESede");
    //初始化cipher
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plain = cipher.doFinal(data);
    return plain;
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 89 with SecretKey

use of javax.crypto.SecretKey in project graylog2-server by Graylog2.

the class KeyUtil method createKeySpec.

private static PKCS8EncodedKeySpec createKeySpec(byte[] keyBytes, String password) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (Strings.isNullOrEmpty(password)) {
        return new PKCS8EncodedKeySpec(keyBytes);
    }
    final EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyBytes);
    final SecretKeyFactory kf = SecretKeyFactory.getInstance(pkInfo.getAlgName());
    final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
    final SecretKey secretKey = kf.generateSecret(keySpec);
    @SuppressWarnings("InsecureCryptoUsage") final Cipher cipher = Cipher.getInstance(pkInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, secretKey, pkInfo.getAlgParameters());
    return pkInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 90 with SecretKey

use of javax.crypto.SecretKey in project AndroidChromium by JackyAndroid.

the class WebappAuthenticator method getMac.

/**
     * @return A Mac, or null if it is not possible to instantiate one.
     */
private static Mac getMac(Context context) {
    try {
        SecretKey key = getKey(context);
        if (key == null) {
            // random number generator, and we must not claim that authentication can work.
            return null;
        }
        Mac mac = Mac.getInstance(MAC_ALGORITHM_NAME);
        mac.init(key);
        return mac;
    } catch (GeneralSecurityException e) {
        Log.w(TAG, "Error in creating MAC instance", e);
        return null;
    }
}
Also used : SecretKey(javax.crypto.SecretKey) GeneralSecurityException(java.security.GeneralSecurityException) Mac(javax.crypto.Mac)

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