Java Code Examples for javax.crypto.KeyGenerator#getProvider()

The following examples show how to use javax.crypto.KeyGenerator#getProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: COSCryptoModuleBase.java    From markdown-image-kit with MIT License 5 votes vote down vote up
/**
 * @param kekMaterials non-null encryption materials
 */
protected final SecretKey generateCEK(final EncryptionMaterials kekMaterials,
        final Provider providerIn) {
    final String keygenAlgo = contentCryptoScheme.getKeyGeneratorAlgorithm();
    KeyGenerator generator;
    try {
        generator = providerIn == null ? KeyGenerator.getInstance(keygenAlgo)
                : KeyGenerator.getInstance(keygenAlgo, providerIn);
        generator.init(contentCryptoScheme.getKeyLengthInBits(),
                cryptoScheme.getSecureRandom());
        // Set to true if the key encryption involves the use of BC's public key
        boolean involvesBCPublicKey = false;
        KeyPair keypair = kekMaterials.getKeyPair();
        if (keypair != null) {
            String keyWrapAlgo =
                    cryptoScheme.getKeyWrapScheme().getKeyWrapAlgorithm(keypair.getPublic());
            if (keyWrapAlgo == null) {
                Provider provider = generator.getProvider();
                String providerName = provider == null ? null : provider.getName();
                involvesBCPublicKey = CryptoRuntime.BOUNCY_CASTLE_PROVIDER.equals(providerName);
            }
        }
        SecretKey secretKey = generator.generateKey();
        if (!involvesBCPublicKey || secretKey.getEncoded()[0] != 0)
            return secretKey;
        for (int retry = 0; retry < 10; retry++) {
            secretKey = generator.generateKey();
            if (secretKey.getEncoded()[0] != 0)
                return secretKey;
        }
        // The probability of getting here is 2^80, which is impossible in practice.
        throw new CosClientException("Failed to generate secret key");
    } catch (NoSuchAlgorithmException e) {
        throw new CosClientException(
                "Unable to generate envelope symmetric key:" + e.getMessage(), e);
    }
}
 
Example 2
Source File: COSCryptoModuleBase.java    From cos-java-sdk-v5 with MIT License 5 votes vote down vote up
/**
 * @param kekMaterials non-null encryption materials
 */
protected final SecretKey generateCEK(final EncryptionMaterials kekMaterials,
        final Provider providerIn) {
    final String keygenAlgo = contentCryptoScheme.getKeyGeneratorAlgorithm();
    KeyGenerator generator;
    try {
        generator = providerIn == null ? KeyGenerator.getInstance(keygenAlgo)
                : KeyGenerator.getInstance(keygenAlgo, providerIn);
        generator.init(contentCryptoScheme.getKeyLengthInBits(),
                cryptoScheme.getSecureRandom());
        // Set to true if the key encryption involves the use of BC's public key
        boolean involvesBCPublicKey = false;
        KeyPair keypair = kekMaterials.getKeyPair();
        if (keypair != null) {
            String keyWrapAlgo =
                    cryptoScheme.getKeyWrapScheme().getKeyWrapAlgorithm(keypair.getPublic());
            if (keyWrapAlgo == null) {
                Provider provider = generator.getProvider();
                String providerName = provider == null ? null : provider.getName();
                involvesBCPublicKey = CryptoRuntime.BOUNCY_CASTLE_PROVIDER.equals(providerName);
            }
        }
        SecretKey secretKey = generator.generateKey();
        if (!involvesBCPublicKey || secretKey.getEncoded()[0] != 0)
            return secretKey;
        for (int retry = 0; retry < 10; retry++) {
            secretKey = generator.generateKey();
            if (secretKey.getEncoded()[0] != 0)
                return secretKey;
        }
        // The probability of getting here is 2^80, which is impossible in practice.
        throw new CosClientException("Failed to generate secret key");
    } catch (NoSuchAlgorithmException e) {
        throw new CosClientException(
                "Unable to generate envelope symmetric key:" + e.getMessage(), e);
    }
}
 
Example 3
Source File: AESTest.java    From java_security with MIT License 5 votes vote down vote up
public static void bcAES()
{
	try 
	{
		Security.addProvider(new BouncyCastleProvider());
		
		// 生成KEY
		KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");	
		keyGenerator.getProvider();
		keyGenerator.init(128);
		// 产生密钥
		SecretKey secretKey = keyGenerator.generateKey();
		// 获取密钥
		byte[] keyBytes = secretKey.getEncoded();
		
		
		// KEY转换
		Key key = new SecretKeySpec(keyBytes, "AES");
		
		
		// 加密
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);
		byte[] result = cipher.doFinal(src.getBytes());
		System.out.println("bc aes encrypt:" + Hex.encodeHexString(result));
		
		// 解密
		cipher.init(Cipher.DECRYPT_MODE, key);
		result = cipher.doFinal(result);
		System.out.println("bc aes decrypt:" + new String(result));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: DESTest.java    From java_security with MIT License 5 votes vote down vote up
public static void bcDES()
{
	try 
	{
		Security.addProvider(new BouncyCastleProvider());
		
		// 生成KEY
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DES", "BC");
		keyGenerator.getProvider();
		keyGenerator.init(56);
		// 产生密钥
		SecretKey secretKey = keyGenerator.generateKey();
		// 获取密钥
		byte[] bytesKey = secretKey.getEncoded();
		
		
		// KEY转换
		DESKeySpec desKeySpec = new DESKeySpec(bytesKey);
		SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
		Key convertSecretKey = factory.generateSecret(desKeySpec);
		
		
		// 加密
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
		byte[] result = cipher.doFinal(src.getBytes());
		System.out.println("bc des encrypt:" + Hex.encodeHexString(result));
		
		// 解密
		cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
		result = cipher.doFinal(result);
		System.out.println("bc des decrypt:" + new String(result));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: DES3Test.java    From java_security with MIT License 5 votes vote down vote up
public static void bc3DES()
{
	try 
	{
		Security.addProvider(new BouncyCastleProvider());
		
		// 生成KEY
		KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede", "BC");
		keyGenerator.getProvider();
		keyGenerator.init(168);
		// 产生密钥
		SecretKey secretKey = keyGenerator.generateKey();
		// 获取密钥
		byte[] bytesKey = secretKey.getEncoded();
		
		
		// KEY转换
		DESedeKeySpec desKeySpec = new DESedeKeySpec(bytesKey);
		SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
		Key convertSecretKey = factory.generateSecret(desKeySpec);
		
		
		// 加密
		Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
		byte[] result = cipher.doFinal(src.getBytes());
		System.out.println("bc 3des encrypt:" + Hex.encodeHexString(result));
		
		// 解密
		cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
		result = cipher.doFinal(result);
		System.out.println("bc 3des decrypt:" + new String(result));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 6
Source File: S3CryptoModuleBase.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
/**
 * @param kekMaterials non-null encryption materials
 */
protected final SecretKey generateCEK(
        final EncryptionMaterials kekMaterials,
        final Provider providerIn) {
    final String keygenAlgo = contentCryptoScheme.getKeyGeneratorAlgorithm();
    KeyGenerator generator;
    try {
        generator = providerIn == null
            ? KeyGenerator.getInstance(keygenAlgo)
            : KeyGenerator.getInstance(keygenAlgo, providerIn);
        generator.init(contentCryptoScheme.getKeyLengthInBits(),
                cryptoScheme.getSecureRandom());
        // Set to true iff the key encryption involves the use of BC's public key
        boolean involvesBCPublicKey = false;
        KeyPair keypair = kekMaterials.getKeyPair();
        if (keypair != null) {
            String keyWrapAlgo = cryptoScheme.getKeyWrapScheme().getKeyWrapAlgorithm(keypair.getPublic());
            if (keyWrapAlgo == null) {
                Provider provider = generator.getProvider();
                String providerName = provider == null ? null : provider.getName();
                involvesBCPublicKey = CryptoRuntime.BOUNCY_CASTLE_PROVIDER.equals(providerName);
            }
        }
        SecretKey secretKey = generator.generateKey();
        if (!involvesBCPublicKey || secretKey.getEncoded()[0] != 0)
            return secretKey;
        for (int retry = 0; retry < 9; retry++) {
            // Regenerate the random key due to a bug/feature in BC:
            // https://github.com/aws/aws-sdk-android/issues/15
            secretKey = generator.generateKey();
            if (secretKey.getEncoded()[0] != 0)
                return secretKey;
        }
        // The probability of getting here is 2^80, which is impossible in practice.
        throw new SdkClientException("Failed to generate secret key");
    } catch (NoSuchAlgorithmException e) {
        throw new SdkClientException(
                "Unable to generate envelope symmetric key:"
                        + e.getMessage(), e);
    }
}