List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:org.talend.commons.utils.PasswordEncryptUtil.java
private static SecretKey getSecretKey() throws Exception { if (key == null) { byte rawKeyData[] = rawKey.getBytes(); DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); //$NON-NLS-1$ key = keyFactory.generateSecret(dks); }//from w w w. j a v a 2 s.com return key; }
From source file:rs.htec.cms.cms_bulima.helper.Password.java
private static String hash(String password, byte[] salt) throws Exception { if (password == null || password.length() == 0) { throw new IllegalArgumentException("Password must have at least one character"); }//from www .j a v a 2s.c o m SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); return Base64.encodeBase64String(key.getEncoded()); }
From source file:Main.java
private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException { PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, 100); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); return skf.generateSecret(keyspec); }
From source file:Main.java
private static PKCS8EncodedKeySpec decryptPrivateKey(byte[] encryptedPrivateKey) throws GeneralSecurityException { EncryptedPrivateKeyInfo epkInfo; try {/*from w w w .j av a 2s. co m*/ epkInfo = new EncryptedPrivateKeyInfo(encryptedPrivateKey); } catch (IOException ex) { // Probably not an encrypted key. return null; } char[] password = System.console().readPassword("Password for the private key file: "); SecretKeyFactory skFactory = SecretKeyFactory.getInstance(epkInfo.getAlgName()); Key key = skFactory.generateSecret(new PBEKeySpec(password)); Arrays.fill(password, '\0'); Cipher cipher = Cipher.getInstance(epkInfo.getAlgName()); cipher.init(Cipher.DECRYPT_MODE, key, epkInfo.getAlgParameters()); try { return epkInfo.getKeySpec(cipher); } catch (InvalidKeySpecException ex) { System.err.println("Password may be bad."); throw ex; } }
From source file:MainClass.java
private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception { int MD5_ITERATIONS = 1000; byte[] salt = new byte[8]; SecureRandom random = new SecureRandom(); random.nextBytes(salt);/* w ww . j av a 2s . c o m*/ PBEKeySpec keySpec = new PBEKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC"); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(salt, MD5_ITERATIONS); Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); byte[] ciphertext = cipher.doFinal(plaintext); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(salt); baos.write(ciphertext); return baos.toByteArray(); }
From source file:org.beangle.security.codec.DESEncrypt.java
public byte[] doEncrypt(byte plainText[]) throws Exception { SecureRandom sr = new SecureRandom(); byte rawKeyData[] = desKey; DESKeySpec dks = new DESKeySpec(rawKeyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); javax.crypto.SecretKey key = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DES"); cipher.init(1, key, sr);//from w w w.jav a 2s. co m byte data[] = plainText; byte encryptedData[] = cipher.doFinal(data); return encryptedData; }
From source file:Main.java
private static byte[] doFinal(String key, int opmode, byte[] input) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { key = checkNull(key) ? DEFAULT_KEY : key; if (checkNull(key)) { return null; }//from w w w .j a v a 2 s . c o m SecureRandom sr = new SecureRandom(); DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(MODE); cipher.init(opmode, securekey, sr); return cipher.doFinal(input); }
From source file:DesEncrypter.java
DesEncrypter(String passPhrase) throws Exception { int iterationCount = 2; KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:ch.helmchen.camlapse.user.control.Encryption.java
/** * Verschlsselt den bergebenen String.//w w w. ja v a 2s .com * * @param aString zu verschlsselnder String. */ public static String encrypt(final String aString) throws GeneralSecurityException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(aString.getBytes())); }
From source file:org.craftercms.commons.crypto.impl.PbkAesTextEncryptor.java
private static Key generateKey(String password, String salt) throws CryptoException { try {//from w ww. j a va 2 s . c om KeySpec keySpec = new PBEKeySpec(password.toCharArray(), Base64.decodeBase64(salt), PBK_ITER, PBK_LEN); SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK_ALGORITHM); return new SecretKeySpec(factory.generateSecret(keySpec).getEncoded(), CryptoUtils.AES_CIPHER_ALGORITHM); } catch (GeneralSecurityException e) { throw new CryptoException("Unable to generate PBK key", e); } }