List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec
public PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength)
From source file:org.everit.password.encryptor.pbkdf2.PBKDF2PasswordEncryptorImpl.java
private String encryptSecure(final byte[] salt, final String plainPassword, final String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException { int deriverdKeyLenght = Algorithm.SUPPORTED_ALGORITHMS_AND_KEY_LENGTHS.get(algorithm); KeySpec spec = new PBEKeySpec(plainPassword.toCharArray(), salt, iterationCount, deriverdKeyLenght); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm); byte[] passwordDigest = secretKeyFactory.generateSecret(spec).getEncoded(); byte[] passwordDigestBase64 = Base64.encodeBase64(passwordDigest); String passwordDigestBase64StringUTF8 = StringUtils.newStringUtf8(passwordDigestBase64); byte[] saltBase64 = Base64.encodeBase64(salt); String saltBase64StringUTF8 = StringUtils.newStringUtf8(saltBase64); return SEPARATOR_START + algorithm + SEPARATOR_END + SEPARATOR_START + saltBase64StringUTF8 + SEPARATOR_END + passwordDigestBase64StringUTF8; }
From source file:io.stallion.utils.Encrypter.java
private static SecretKeySpec makeKeySpec(String password, String salt) { byte[] saltBytes = new byte[0]; try {/* ww w . j av a 2 s .c om*/ saltBytes = Hex.decodeHex(salt.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, ITERATIONS, KEY_LENGTH); SecretKey secretKey; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); secretKey = factory.generateSecret(keySpec); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("Not a valid encryption algorithm", e); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException("Not a valid secret key", e); } SecretKeySpec skeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); return skeySpec; }
From source file:com.doplgangr.secrecy.filesystem.encryption.AES_Crypter.java
private static int generatePBKDF2IterationCount(String passphrase, byte[] salt) { int calculatedIterations = 0; try {/*from ww w . jav a 2s . c o m*/ PBEKeySpec pbeKeySpec = new PBEKeySpec(passphrase.toCharArray(), salt, Config.PBKDF2_ITERATIONS_BENCHMARK, AES_KEY_SIZE_BIT); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SECRET_KEY_ALGORITHM); long startTime = System.currentTimeMillis(); secretKeyFactory.generateSecret(pbeKeySpec); long finishTime = System.currentTimeMillis(); calculatedIterations = (int) ((Config.PBKDF2_ITERATIONS_BENCHMARK / (double) (finishTime - startTime)) * Config.PBKDF2_CREATION_TARGET_MS); } catch (Exception e) { Util.log("Cannot benchmark PBKDF2!"); } if (calculatedIterations > Config.PBKDF2_ITERATIONS_MIN) { Util.log("Using " + calculatedIterations + " PBKDF2 iterations"); return calculatedIterations; } Util.log("Using " + Config.PBKDF2_ITERATIONS_MIN + " PBKDF2 iterations"); return Config.PBKDF2_ITERATIONS_MIN; }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * ???/*from w ww .ja v a 2 s . co m*/ * <dl> * <dt>? * <dd>PBKDF2WithHmacSHA1?????????65536?128?????? * </dl> * @param password * @param salt * @return ? */ public static SecretKey createSecretKey(final char[] password, final byte[] salt) { try { final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); final KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); return factory.generateSecret(spec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new StandardRuntimeException(e); } }
From source file:com.sangupta.passcode.PassCode.java
/** * Hash the given password./*w w w .j a v a 2s . co m*/ * * @param password * the master password or passphrase * * @param salt * the salt to be used * * @param entropy * the entropy to be used * * @return the byte-array representing the hash */ private byte[] hash(String password, String salt, int entropy) { // generate the hash try { PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), this.config.iterations, entropy + 12); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); return skf.generateSecret(spec).getEncoded(); } catch (GeneralSecurityException e) { System.out.println("Something went wrong!"); System.exit(0); } return null; }
From source file:com.fegor.alfresco.security.crypto.Crypto.java
/** * Decryption configuration/*from w w w. j a v a2 s . c o m*/ * * @param initvec * @param salt * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws DecoderException */ public void configDecrypt(String initvec, String salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, DecoderException { SecretKeyFactory factory = null; SecretKey tmp = null; SecretKey secret = null; salt_pos = Hex.decodeHex(salt.toCharArray()); if (logger.isDebugEnabled()) logger.debug(this.getClass().getName() + ": [salt: " + (new String(Hex.encodeHex(salt_pos))) + "]"); vector_init = Hex.decodeHex(initvec.toCharArray()); if (logger.isDebugEnabled()) logger.debug( this.getClass().getName() + ": [vector ini: " + (new String(Hex.encodeHex(vector_init))) + "]"); /* * http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files * .shtml */ factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt_pos, ITERATIONS, KEYLEN_BITS); tmp = factory.generateSecret(spec); secret = new SecretKeySpec(tmp.getEncoded(), "AES"); deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); deCipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(vector_init)); }
From source file:org.opennms.features.scv.impl.JCEKSSecureCredentialsVault.java
@Override public void setCredentials(String alias, Credentials credentials) { try {// w w w. jav a 2 s . c o m byte[] credentialBytes = toBase64EncodedByteArray(credentials); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE"); SecretKey generatedSecret = factory.generateSecret(new PBEKeySpec( new String(credentialBytes).toCharArray(), m_salt, m_iterationCount, m_keyLength)); KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(m_password); m_keystore.setEntry(alias, new KeyStore.SecretKeyEntry(generatedSecret), keyStorePP); writeKeystoreToDisk(); } catch (KeyStoreException | InvalidKeySpecException | NoSuchAlgorithmException | IOException e) { throw Throwables.propagate(e); } }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
public static boolean isValid(String plainText, String HMAC, String key) { try {// ww w . j ava 2s. com System.out.println("HMAC on = " + plainText); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider()); mac.init(secret); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(plainText.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String String check = new String(hexBytes, "UTF-8"); System.out.println("Checking = " + check); return check.equals(HMAC); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.filelocker.encryption.AES_Encryption.java
/** * this must be called after creating the initial Crypto object. It creates a salt of SALT_LEN bytes * and generates the salt bytes using secureRandom(). The encryption secret key is created * along with the initialization vectory. The member variable vEcipher is created to be used * by the class later on when either creating a CipherOutputStream, or encrypting a buffer * to be written to disk.//from w ww . j av a 2s .co m * * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws InvalidParameterSpecException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws UnsupportedEncodingException * @throws InvalidKeyException */ public void setupEncrypt() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeyException { SecretKeyFactory factory = null; SecretKey tmp = null; // crate secureRandom salt and store as member var for later use vSalt = new byte[SALT_LEN]; SecureRandom rnd = new SecureRandom(); rnd.nextBytes(vSalt); Db("generated salt :" + Hex.encodeHexString(vSalt)); factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); /* Derive the key, given password and salt. * * in order to do 256 bit crypto, you have to muck with the files for Java's "unlimted security" * The end user must also install them (not compiled in) so beware. * see here: http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files.shtml */ KeySpec spec = new PBEKeySpec(vPassword.toCharArray(), vSalt, ITERATIONS, KEYLEN_BITS); tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); /* Create the Encryption cipher object and store as a member variable */ vEcipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); vEcipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = vEcipher.getParameters(); // get the initialization vectory and store as member var vInitVec = params.getParameterSpec(IvParameterSpec.class).getIV(); Db("vInitVec is :" + Hex.encodeHexString(vInitVec)); }
From source file:org.eclipse.che.api.crypt.server.JCEEncryptTextService.java
private SecretKey generateSecret() throws NoSuchAlgorithmException, InvalidKeySpecException { final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm); final KeySpec keySpec = new PBEKeySpec(getMasterPassword(), this.salt, 65536, this.keySize); final SecretKey tempKey = keyFactory.generateSecret(keySpec); final SecretKey secret = new SecretKeySpec(tempKey.getEncoded(), this.cipher); return secret; }