List of usage examples for org.bouncycastle.crypto.generators PKCS12ParametersGenerator init
public void init(byte[] password, byte[] salt, int iterationCount)
From source file:GenTestDKs.java
License:Open Source License
private static void testKey(PKCS12ParametersGenerator pgen, int keyLen, int iterCount, String password, byte[] salt) { System.out.println("key len = " + keyLen + ", iter count = " + iterCount + ", password = \"" + password + "\", salt len = " + salt.length); char[] pwChars = password.toCharArray(); byte[] pwBytes = PBEParametersGenerator.PKCS12PasswordToBytes(pwChars); pgen.init(pwBytes, salt, iterCount); KeyParameter kp = (KeyParameter) pgen.generateDerivedParameters(keyLen); printByteArray(kp.getKey());//from w w w. ja va 2 s .com }
From source file:org.cesecore.util.StringTools.java
License:Open Source License
public static String pbeEncryptStringWithSha256Aes192(final String in) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { CryptoProviderTools.installBCProviderIfNotAvailable(); if (CryptoProviderTools.isUsingExportableCryptography()) { log.warn("Obfuscation not possible due to weak crypto policy."); return in; }/*from w ww . j av a2 s. c o m*/ final Digest digest = new SHA256Digest(); final PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PBEParametersGenerator.PKCS12PasswordToBytes(p), getSalt(), iCount); final ParametersWithIV params = (ParametersWithIV) pGen.generateDerivedParameters(192, 128); final SecretKeySpec encKey = new SecretKeySpec(((KeyParameter) params.getParameters()).getKey(), "AES"); final Cipher c; c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV())); final byte[] enc = c.doFinal(in.getBytes("UTF-8")); final byte[] hex = Hex.encode(enc); return new String(hex); }
From source file:org.ejbca.util.StringTools.java
License:Open Source License
public static String pbeEncryptStringWithSha256Aes192(final String in) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { if (CryptoProviderTools.isUsingExportableCryptography()) { log.warn("Obfuscation not possible due to weak crypto policy."); return in; }//from w w w.j a v a 2 s. c o m final Digest digest = new SHA256Digest(); final PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PBEParametersGenerator.PKCS12PasswordToBytes(p), getSalt(), iCount); final ParametersWithIV params = (ParametersWithIV) pGen.generateDerivedParameters(192, 128); final SecretKeySpec encKey = new SecretKeySpec(((KeyParameter) params.getParameters()).getKey(), "AES"); final Cipher c; c = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); c.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV())); final byte[] enc = c.doFinal(in.getBytes("UTF-8")); final byte[] hex = Hex.encode(enc); return new String(hex); }
From source file:org.xmind.core.internal.security.BouncyCastleSecurityProvider.java
License:Open Source License
private BufferedBlockCipher createCipher(boolean encrypt, IEncryptionData encData, String password) throws CoreException { checkEncryptionData(encData);/*w ww . j a v a 2s. c om*/ // Create a parameter generator PKCS12ParametersGenerator paramGen = new PKCS12ParametersGenerator(new MD5Digest()); // Get the password bytes byte[] pwBytes = password == null ? new byte[0] : PBEParametersGenerator.PKCS12PasswordToBytes(password.toCharArray()); // Initialize the parameter generator with password bytes, // salt and iteration counts paramGen.init(pwBytes, getSalt(encData), getIterationCount(encData)); // Generate a parameter CipherParameters param = paramGen.generateDerivedParameters(128); // Create a block cipher BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); // Initialize the block cipher cipher.init(encrypt, param); return cipher; }