List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.ethercamp.harmony.keystore.KeystoreFormat.java
private byte[] hash(String encryptedData, byte[] salt, int iterations) throws Exception { char[] chars = encryptedData.toCharArray(); PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 256); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); return skf.generateSecret(spec).getEncoded(); }
From source file:netinf.common.security.impl.CryptoAlgorithmImpl.java
@Override public SecretKey getSecretKeyFromString(String contentAlgorithmName, String password) throws NetInfCheckedSecurityException { try {//w w w. j a va 2s .c om DESedeKeySpec desKeySpec = new DESedeKeySpec(password.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(contentAlgorithmName); return keyFactory.generateSecret(desKeySpec); } catch (Exception e) { throw new NetInfCheckedSecurityException("Unable to create SecretKey. " + e.getMessage()); } }
From source file:org.everit.osgi.password.encryptor.pbkdf2.internal.PBKDF2PasswordEncryptorComponent.java
private String encryptSecure(final byte[] salt, final String plainPassword, final String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException { int deriverdKeyLenght = PBKDF2PasswordEncryptorConstants.SUPPORTED_ALGORITHMS_AND_KEY_LENGTHS .get(algorithm);/*from ww w. j a va 2s. co m*/ 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:adminpassword.AESDemo.java
public String encrypt(String plainText) throws Exception { //get salt/* w w w .j av a 2 s.c o m*/ salt = generateSalt(); byte[] saltBytes = salt.getBytes("UTF-8"); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); //encrypt the message Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8")); return new Base64().encodeAsString(encryptedTextBytes); }
From source file:adminpassword.AESDemo.java
@SuppressWarnings("static-access") public String decrypt(String encryptedText) throws Exception { byte[] saltBytes = salt.getBytes("UTF-8"); byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); // Decrypt the message Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes)); byte[] decryptedTextBytes = null; try {/*from w w w. j av a 2s .c o m*/ decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); }
From source file:de.phoenix.security.Encrypter.java
/** * Salt a given password with a random salt * /*from w ww . j av a 2s .c om*/ * @param password * The password to salt * @param salt * The random generated salt * @param iterations * The number of iterations the password is salted by the * algorithmn * @param bytes * How many bytes the passwords contains * @return A salted password * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ private SaltedPassword saltPassword(char[] password, byte[] salt, int iterations, int bytes) throws NoSuchAlgorithmException, InvalidKeySpecException { PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bytes * 8); SecretKeyFactory skf = SecretKeyFactory.getInstance(KEY_ALGORITHM); String saltString = Hex.encodeHexString(salt); String saltedHash = Hex.encodeHexString(skf.generateSecret(spec).getEncoded()); return new SaltedPassword(saltedHash, saltString, iterations); }
From source file:org.jajim.utilidades.cifrado.Cifrador.java
/** * Constructor de la clase. Inicializa el cifrador. * <p>/*from w w w. j a v a2s . com*/ * @throws ImposibleCifrarDescifrarException Si no se puede instanciar un ci frador adecuado. */ public Cifrador() throws ImposibleCifrarDescifrarException { try { // Inicializacin de la clave SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); String clave = "cjliamve"; DESKeySpec kspec = new DESKeySpec(clave.getBytes()); sk = skf.generateSecret(kspec); // Inicializacin del cifrador cifrado = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException | NoSuchPaddingException e) { // En caso de que se produzca un error se escribe en el fichero // de log y se lanza una excepcin ManejadorDeLogs mdl = ManejadorDeLogs.getManejadorDeLogs(); mdl.escribir("No se puede crear un cifrador de DES"); throw new ImposibleCifrarDescifrarException(); } }
From source file:org.openmrs.module.reportingsummary.api.io.download.DownloadProcessor.java
/** * Method to initialize the cipher object with the correct encryption algorithm. * * @throws Exception//w w w .j a v a2s . c o m */ private Cipher initializeCipher() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance(InputOutputConstants.SECRET_KEY_FACTORY); KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128); SecretKey secretKey = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), InputOutputConstants.KEY_SPEC); if (log.isDebugEnabled()) log.debug("Encrypting with: " + secret.getAlgorithm()); Cipher cipher = Cipher.getInstance(InputOutputConstants.CIPHER_CONFIGURATION); cipher.init(Cipher.ENCRYPT_MODE, secret); return cipher; }
From source file:org.apache.nifi.processors.standard.util.crypto.OpenSSLPKCS5CipherProvider.java
protected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, boolean encryptMode) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { if (encryptionMethod == null) { throw new IllegalArgumentException("The encryption method must be specified"); }/*from ww w . j a v a 2 s.c o m*/ if (StringUtils.isEmpty(password)) { throw new IllegalArgumentException("Encryption with an empty password is not supported"); } validateSalt(encryptionMethod, salt); String algorithm = encryptionMethod.getAlgorithm(); String provider = encryptionMethod.getProvider(); // Initialize secret key from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider); SecretKey tempKey = factory.generateSecret(pbeKeySpec); final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, getIterationCount()); Cipher cipher = Cipher.getInstance(algorithm, provider); cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec); return cipher; }
From source file:com.gvmax.common.util.Enc.java
public Enc(String password, int keyLength) { if (password == null || password.trim().equals("")) { enabled = false;/*from ww w .j a v a 2s. c om*/ } else { enabled = true; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 1024, keyLength); key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); c = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (Exception e) { logger.error(e); } } }