List of usage examples for org.bouncycastle.crypto.generators PKCS12ParametersGenerator PKCS12ParametersGenerator
public PKCS12ParametersGenerator(Digest digest)
From source file:GenTestDKs.java
License:Open Source License
public static void main(String[] args) { PKCS12ParametersGenerator pgen = new PKCS12ParametersGenerator(new SHA1Digest()); // SB.4: key lengths for defined OIDs // (168 for triple DES will first exercise chaining.) final int[] keyLens = { 40, 128, 168, 368 }; // SB.4 iteration count is recommended to be 1024 or more final int[] iterCounts = { 1, 2, 4, 8, 128, 1024, 1536, 2048 }; // SB.4 salt should be same length as hash function output // (=160 bits for SHA1.) byte[][] salts = new byte[3][]; salts[0] = new byte[] { 'S', 'A', 'L', 'T' }; System.out.println("4 byte salt"); printByteArray(salts[0]);//from w w w. j a v a 2 s . c o m // calls to nextBytes() are only executed once /* SecureRandom sr; try { sr = SecureRandom.getInstance("SHA1PRNG", "SUN"); } catch (Exception e) { System.err.println("UNABLE TO GET RANDOM SOURCE"); return; } */ // salts[1] = new byte[160 / 8]; // sr.nextBytes(salts[1]); salts[1] = new byte[] { (byte) 0x1d, (byte) 0x56, (byte) 0x50, (byte) 0x78, (byte) 0xc3, (byte) 0x50, (byte) 0x6f, (byte) 0x89, (byte) 0xbd, (byte) 0xa7, (byte) 0x3b, (byte) 0xb6, (byte) 0xe3, (byte) 0xe5, (byte) 0xb8, (byte) 0xa3, (byte) 0x68, (byte) 0x3d, (byte) 0xd3, (byte) 0x62 }; System.out.println("20 byte salt (same size as SHA1 output)"); printByteArray(salts[1]); // salts[2] = new byte[200 / 8]; // sr.nextBytes(salts[2]); salts[2] = new byte[] { (byte) 0xe2, (byte) 0x2c, (byte) 0x7b, (byte) 0x03, (byte) 0x16, (byte) 0x3a, (byte) 0xe5, (byte) 0x47, (byte) 0xf8, (byte) 0x23, (byte) 0x9d, (byte) 0xa4, (byte) 0x0d, (byte) 0x6f, (byte) 0x46, (byte) 0xd7, (byte) 0x9e, (byte) 0xa3, (byte) 0xc6, (byte) 0xff, (byte) 0xb3, (byte) 0xf0, (byte) 0x4e, (byte) 0xbe, (byte) 0x61 }; System.out.println("25 byte salt"); printByteArray(salts[2]); final String passwds[] = { "0000", "0001", "PSWD", "password", "abcdefghijklmnopqrstuvwxyz" }; for (int keyLenIdx = 0; keyLenIdx < keyLens.length; ++keyLenIdx) { for (int iterIdx = 0; iterIdx < iterCounts.length; ++iterIdx) { for (int saltIdx = 0; saltIdx < salts.length; ++saltIdx) { for (int pwdIdx = 0; pwdIdx < passwds.length; ++pwdIdx) { testKey(pgen, keyLens[keyLenIdx], iterCounts[iterIdx], passwds[pwdIdx], salts[saltIdx]); } // for (int pwdIdx = 0; pwdIdx < passwds.length; ++pwdIdx) } // for (int saltIdx = 0; saltIdx < salts.length; ++saltIdx) } // for (int iterIdx = 0; iterIdx < iterCounts.length; ++iterIdx) } // for (int keyLenIdx = 0; keyLenIdx < keyLens.length; ++keyLenIdx) }
From source file:com.password.locker.crypto.SecureCryptoImpl.java
License:Open Source License
/** * SecureCrypto Constructor./*from ww w. j a va 2 s . c o m*/ * * @param password * password for the crypto keyspec. * * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException */ public SecureCryptoImpl(final char[] password) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { SHA256Digest digest = new SHA256Digest(); String s = Constants.PROPERTIES.getStringProperty(Constants.SALT_KEY, PasswordUtils.getSalt(digest)); salt = Hex.decode(s); if (salt.length != digest.getDigestSize()) { LOGGER.warn("Warning salt size is not the size of the Digest."); } //--------------------------------------------------- // Setup encryption. //--------------------------------------------------- PBEParametersGenerator pGen = new PKCS12ParametersGenerator(digest); pGen.init(PBEParametersGenerator.PKCS12PasswordToBytes(password), salt, ITERATIONS); ParametersWithIV params = (ParametersWithIV) pGen.generateDerivedParameters(KEY_LEN, IV_LEN); SecretKeySpec encKey = new SecretKeySpec(((KeyParameter) params.getParameters()).getKey(), "AES"); encryption = Cipher.getInstance(Constants.CRYPTO_ALGORITHM, new BouncyCastleProvider()); encryption.init(Cipher.ENCRYPT_MODE, encKey, new IvParameterSpec(params.getIV())); //--------------------------------------------------- // Setup decryption. //--------------------------------------------------- decryption = Cipher.getInstance(Constants.CRYPTO_SEC_KEY_SPEC, new BouncyCastleProvider()); PBEKeySpec keySpec = new PBEKeySpec(password, salt, ITERATIONS); SecretKeyFactory fact = SecretKeyFactory.getInstance(Constants.CRYPTO_SEC_KEY_SPEC, new BouncyCastleProvider()); try { decryption.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec)); } catch (InvalidKeySpecException e) { ExceptionUtils.fatalError(SecureCryptoImpl.class, e); } Constants.PROPERTIES.addProperty(Constants.SALT_KEY, s); }
From source file:com.symbian.security.Pkcs12Pbe.java
License:Open Source License
public Pkcs12Pbe() { pgen = new PKCS12ParametersGenerator(new SHA1Digest()); }
From source file:de.jpm.model.EncryptionService.java
License:Open Source License
/** * * @param password/* www . ja v a 2s . c o m*/ */ public void initCipher(char[] password) { PBEParametersGenerator keyGenerator = new PKCS12ParametersGenerator(new SHA256Digest()); keyGenerator.init(PKCS12ParametersGenerator.PKCS12PasswordToBytes(password), salt, 20); CipherParameters keyParams = keyGenerator.generateDerivedParameters(256, 128); encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); encryptCipher.init(true, keyParams); decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); decryptCipher.init(false, keyParams); }
From source file:edu.vt.middleware.crypt.pbe.PKCS12KeyGenerator.java
License:Open Source License
/** {@inheritDoc} */ protected PBEParametersGenerator newParamGenerator() { return new PKCS12ParametersGenerator(digest.getDigest()); }
From source file:edu.vt.middleware.crypt.PbeKeyGenerator.java
License:Open Source License
/** * Generate a key from a text password using the PKCS#12 method described at * http://www.rsa.com/rsalabs/node.asp?id=2138. * * @param password Raw material used for key generation. * @param keyBitLength Size of generated key in bits. * @param digest Digest algorithm to use during key generation. * @param salt Key initialization data. * * @return Secret key based on password. *//*from w ww . j a v a 2 s . c o m*/ public SecretKey generatePkcs12(final char[] password, final int keyBitLength, final DigestAlgorithm digest, final byte[] salt) { return generate(new PKCS12ParametersGenerator(digest.getDigest()), PBEParametersGenerator.PKCS12PasswordToBytes(password), keyBitLength, salt); }
From source file:edu.vt.middleware.crypt.PbeKeyGenerator.java
License:Open Source License
/** * Generate a key/IV pair from a text password using the PKCS#12 method * described at http://www.rsa.com/rsalabs/node.asp?id=2138. * * @param password Raw material used for key generation. * @param keyBitLength Size of generated key in bits. * @param ivBitLength Size of generated IV in bits. * @param digest Digest algorithm to use during key generation. * @param salt Key initialization data. * * @return Secret key based on password. *///from w w w. ja va 2 s . c o m public KeyWithIV generatePkcs12(final char[] password, final int keyBitLength, final int ivBitLength, final DigestAlgorithm digest, final byte[] salt) { return generate(new PKCS12ParametersGenerator(digest.getDigest()), PBEParametersGenerator.PKCS12PasswordToBytes(password), keyBitLength, ivBitLength, salt); }
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 ww w.j av 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.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. jav a2s . co 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 av a 2s .c o m // 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; }