List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:adminpassword.AESDemo.java
public String encrypt(String plainText) throws Exception { //get salt/*from w w w . j a va 2 s . co 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:com.ikon.util.SecureStore.java
/** * DES encoder/*from w w w.j a v a 2 s. co m*/ */ public static byte[] desEncode(String key, byte[] src) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey sKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe cipher.init(Cipher.ENCRYPT_MODE, sKey); byte[] dst = cipher.doFinal(src); return dst; }
From source file:com.intera.roostrap.util.EncryptionUtil.java
private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os) throws InvalidKeyException, IOException { DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey)); SecretKey key = null;/*from w ww . j a v a 2 s . c om*/ Cipher cipher = null; try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); key = secretKeyFactory.generateSecret(keySpec); cipher = Cipher.getInstance("DES"); } catch (Exception e) { throw new RuntimeException(e); } if (mode == Cipher.ENCRYPT_MODE) { cipher.init(Cipher.ENCRYPT_MODE, key); CipherInputStream cis = new CipherInputStream(is, cipher); doCopy(cis, os); } else if (mode == Cipher.DECRYPT_MODE) { cipher.init(Cipher.DECRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(os, cipher); doCopy(is, cos); } }
From source file:Authentication.HashPassword.java
public HashPassword() throws Exception { myEncryptionKey = "ThisIsSpartaThisIsSparta"; myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(myEncryptionScheme); cipher = Cipher.getInstance(myEncryptionScheme); key = skf.generateSecret(ks);//from www. j a v a2 s. c om }
From source file:org.openchain.certification.utility.PasswordUtil.java
private static String hash(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = factory//from w w w . j a va 2s. co m .generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLength)); return Base64.encodeBase64String(key.getEncoded()); }
From source file:com.paypal.utilities.AESDecrypt.java
public AESDecrypt(String encryptedString) throws Exception { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH); SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec); SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES"); encrypt = Base64.decodeBase64(encryptedString.getBytes()); eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); eCipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] iv = extractIV(); dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); }
From source file:com.creditcloud.common.security.impl.DESTextCipher.java
public DESTextCipher() { try {/* w w w . j a va2s. co m*/ encryptCipher = Cipher.getInstance("DES"); decryptCipher = Cipher.getInstance("DES"); keyFactory = SecretKeyFactory.getInstance("DES"); } catch (GeneralSecurityException ex) { logger.error("I don't think this will happen...", ex); } }
From source file:net.sf.jftp.config.Crypto.java
public static String Encrypt(String str) { // create cryptography object SecretKeyFactory factory;// w ww.j a va 2 s . co m try { factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } // init key SecretKey key; try { key = factory.generateSecret(new PBEKeySpec(PASSWORD)); } catch (InvalidKeySpecException e) { // The password is hard coded - this exception can't be the case return ""; } // init cipher Cipher pbeCipher; try { pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); } catch (NoSuchPaddingException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } try { pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); } catch (InvalidKeyException e) { return ""; } catch (InvalidAlgorithmParameterException e) { return ""; } // encode & return encoded string try { return base64Encode(pbeCipher.doFinal(str.getBytes())); } catch (IllegalBlockSizeException e) { return ""; } catch (BadPaddingException e) { return ""; } }
From source file:org.underworldlabs.util.DesEncrypter.java
public static SecretKey getSecretKey(String key) throws Exception { byte[] keyAsBytes = key.getBytes("UTF8"); KeySpec keySpec = new DESKeySpec(keyAsBytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); return keyFactory.generateSecret(keySpec); }
From source file:com.rdonasco.security.utils.EncryptionUtil.java
public static SecretKeyFactory getKeyFactory() throws Exception { if (null == keyFactory) { keyFactory = SecretKeyFactory.getInstance(CIPHER_KEY_SPEC); }// ww w . j ava 2 s . c o m return keyFactory; }