List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
private static Cipher genCipher(char[] pass, byte[] salt, String factory, int iterations, int mode) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { PBEKeySpec keySpec = new PBEKeySpec(pass, salt, iterations, 128); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(factory); SecretKey key = keyFactory.generateSecret(keySpec); AlgorithmParameterSpec spec = new PBEParameterSpec(salt, iterations); Cipher cipher = Cipher.getInstance(factory); cipher.init(mode, key, spec);//from ww w .j a va2 s . c om return cipher; }
From source file:com.cisco.oss.foundation.directory.utils.ObfuscatUtil.java
/** * Encrypt the password in PBKDF2 algorithm. * * @param password/*from w ww. j a v a 2 s. c om*/ * the password to obfuscate. * @param salt * the randomly sequence of bits of the user. * @return * the obfuscated password byte array. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static byte[] pBKDF2ObfuscatePassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { String algorithm = "PBKDF2WithHmacSHA1"; int derivedKeyLength = 160; int iterations = 20000; KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength); SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm); return f.generateSecret(spec).getEncoded(); }
From source file:org.opentravel.pubs.config.PasswordHelper.java
/** * Encrypts the given password./*from w ww. j a va 2 s .c o m*/ * * @param clearTextPassword the clear-text password to encrypt * @return String */ public static String encrypt(String clearTextPassword) { try { String encryptionPassword = ConfigSettingsFactory.getConfig().getEncryptionPassword(); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionPassword.toCharArray())); Cipher pbeCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return new String(new Base64().encode(pbeCipher.doFinal(clearTextPassword.getBytes("UTF-8")))); } catch (GeneralSecurityException | UnsupportedEncodingException e) { throw new RuntimeException("Error during password encryption.", e); } }
From source file:de.fhdo.helper.DES.java
public static String decrypt(String Text) { try {/*from w w w .j av a2s . c o m*/ DESKeySpec keySpec = new DESKeySpec("schluessel_stdrepository15".getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); byte[] encrypedPwdBytes = Base64.decodeBase64(Text); Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes)); return new String(plainTextPwdBytes); } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:com.tesora.dve.common.PECryptoUtils.java
private static Cipher createCrypter(int mode) throws Exception { // Create the key KeySpec keySpec = new PBEKeySpec(settings.getPassword().toCharArray(), settings.getSalt(), settings.getIterations());/*from w ww .ja va 2 s . com*/ SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key, new PBEParameterSpec(settings.getSalt(), settings.getIterations())); return cipher; }
From source file:io.manasobi.utils.CryptoUtils.java
/** * ? ?. ?? ? ?? ? ?.//from w w w . j a v a2s. co m * * @return ?? ?? Hex ? */ public static String generateHexDESKey() { byte[] rawKey = null; try { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecretKey secretKey = keyGenerator.generateKey(); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM); DESedeKeySpec desEdeSpec = (DESedeKeySpec) secretKeyFactory.getKeySpec(secretKey, javax.crypto.spec.DESedeKeySpec.class); rawKey = desEdeSpec.getKey(); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return new String(Hex.encodeHex(rawKey)); }
From source file:adminpassword.Encryption.java
public String encrypt(String word, String idKey) throws Exception { byte[] ivBytes; String password = idKey; //you can give whatever you want. This is for testing purpose SecureRandom random = new SecureRandom(); byte bytes[] = new byte[20]; random.nextBytes(bytes);//from w w w . j ava 2 s . co m byte[] saltBytes = bytes; // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); //encrypting the word 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(word.getBytes("UTF-8")); //prepend salt and vi byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length); return new Base64().encodeToString(buffer); }
From source file:org.demo.show.providers.ThreeDes2.java
/** * ?//w ww . j ava2 s .c om * * @param key * * @return Key * */ public static Key toKey(byte[] key) throws Exception { // Des DESedeKeySpec dks = new DESedeKeySpec(key); // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM); // ? SecretKey secretKey = keyFactory.generateSecret(dks); return secretKey; }
From source file:passworddecoder.DesEncrypter.java
DesEncrypter(String passPhrase) { KeySpec keySpec;//from www . j av a 2 s . c o m try { keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); this.dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount); this.dcipher.init(2, key, paramSpec); } catch (InvalidAlgorithmParameterException e) { } catch (InvalidKeySpecException e) { } catch (NoSuchPaddingException e) { } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } }
From source file:org.casbah.provider.KeyHelper.java
public static PrivateKey readKey(String keypass, byte[] keyData) throws CAProviderException { try {/*from w ww . j av a2s .co m*/ EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyData); PBEKeySpec keySpec = new PBEKeySpec(keypass.toCharArray()); SecretKeyFactory pbeKeyFactory = SecretKeyFactory.getInstance(pkInfo.getAlgName()); PKCS8EncodedKeySpec encodedKeySpec = pkInfo.getKeySpec(pbeKeyFactory.generateSecret(keySpec)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return (RSAPrivateCrtKey) keyFactory.generatePrivate(encodedKeySpec); } catch (Exception e) { throw new CAProviderException("Could not decode private key", e); } }