List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:ie.peternagy.jcrypto.algo.AesWrapper.java
/** * Generate secret key from iv, salt, baseKey * *//*from w ww . j a v a 2s.co m*/ protected final void generateSecretKey() { try { SecretKeyFactory factory = SecretKeyFactory.getInstance(KEYGEN_ALGORITHM); KeySpec keySpec = new PBEKeySpec(new String(baseKey).toCharArray(), salt, 4096, 256); SecretKey generalSecret = factory.generateSecret(keySpec); secretKey = new SecretKeySpec(generalSecret.getEncoded(), ALGORITHM_NAME); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { Logger.getLogger(AesWrapper.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException("Invalid environment, check max key size", ex); } }
From source file:com.networknt.utility.HashUtil.java
public static boolean validatePassword(char[] originalPassword, String storedPassword) throws NoSuchAlgorithmException, InvalidKeySpecException { String[] parts = storedPassword.split(":"); int iterations = Integer.parseInt(parts[0]); byte[] salt = fromHex(parts[1]); byte[] hash = fromHex(parts[2]); PBEKeySpec spec = new PBEKeySpec(originalPassword, salt, iterations, hash.length * 8); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] testHash = skf.generateSecret(spec).getEncoded(); int diff = hash.length ^ testHash.length; for (int i = 0; i < hash.length && i < testHash.length; i++) { diff |= hash[i] ^ testHash[i];//from w ww. j a v a2 s. c o m } return diff == 0; }
From source file:bit.changepurse.wdk.util.CheckedExceptionMethods.java
public static SecretKeyFactory getSecretKeyFactory(KeyDerivationAlgorithm algorithm) { try {//from www . j a va 2 s. c om return SecretKeyFactory.getInstance(algorithm.toString()); } catch (NoSuchAlgorithmException e) { throw new UncheckedException(e); } }
From source file:org.casbah.provider.SSLeayEncoder.java
private static byte[] performCipherOperation(byte[] data, byte[] salt, String keypass, int opMode) throws GeneralSecurityException, IOException { Cipher cipher = Cipher.getInstance(JAVA_ENC_ALGORITHM); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(JAVA_KEY_TYPE); SecretKey secretKey = secretKeyFactory.generateSecret(calculateKeyFromPassKey(keypass.getBytes(), salt)); IvParameterSpec iv = new IvParameterSpec(salt); cipher.init(opMode, secretKey, iv);/*from w w w. j av a 2s . c o m*/ return cipher.doFinal(data); }
From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java
/** * Description ?/* ww w .ja va2 s .c om*/ * * @param data * @param key byte * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityException { // ???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
From source file:com.haulmont.cuba.core.sys.encryption.Sha1EncryptionModule.java
protected String apply(String content, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { KeySpec keySpec = getKeySpec(content, salt); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); byte[] encoded = keyFactory.generateSecret(keySpec).getEncoded(); return new String(Hex.encodeHex(encoded)); }
From source file:org.apache.xml.security.test.encryption.BobKeyResolver.java
/** * Method engineResolveSecretKey// w w w .j av a 2s . c o m * * @param element * @param BaseURI * @param storage * * @throws KeyResolverException */ public javax.crypto.SecretKey engineLookupAndResolveSecretKey(Element element, String BaseURI, StorageResolver storage) throws KeyResolverException { if (engineCanResolve(element, BaseURI, storage)) { try { DESedeKeySpec keySpec = new DESedeKeySpec("abcdefghijklmnopqrstuvwx".getBytes("ASCII")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey key = keyFactory.generateSecret(keySpec); return key; } catch (Exception e) { throw new KeyResolverException("Something badly wrong in creation of bob's key"); } } return null; }
From source file:com.ccstats.crypto.AESWorker.java
/** * Through the power of the advanced encryption standard, a plaintext will be encrypted with a parameter-specified * password, an extra protective layer (salt), and a specified key length. Make sure to acquire the salt and ivBytes * as they are necessary for decrypting the encrypted result. * * Firstly, The password is obtained and instantly overridden with the hashed version of the password, allowing * for stronger security as the plaintext password will not be used. Second, an arbitrary salt is securely * generated. Finally, the encryption standard is carried out and the encrypted text is obtained. * * @param password the password as a char array. * @param text The plaintext bytes to be encrypted. * * @return The Encrypted text in hexadecimal format. *///from w w w . ja v a2 s . c o m public char[] encrypt(char[] password, byte[] text) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, BadPaddingException, IllegalBlockSizeException { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); if (Cipher.getMaxAllowedKeyLength("AES") < this.keyLength) { this.keyLength = Cipher.getMaxAllowedKeyLength("AES"); System.err.printf( "WARNING: YOUR MAXIMUM AES KEY LENGTH POLICY IS %d BITS. KEY LENGTH LIMITED TO %d BITS.\n", this.keyLength, this.keyLength); } // hash the password and acquire a securely and randomly generated salt password = hash(new String(password).getBytes(StandardCharsets.UTF_8)); byte[] salt = new byte[20]; new SecureRandom().nextBytes(salt); // acquire the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password, salt, 16384, this.keyLength); SecretKey key = factory.generateSecret(spec); SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES"); // init the cipher and process the encryption cipher.init(Cipher.ENCRYPT_MODE, keySpec); AlgorithmParameters ap = cipher.getParameters(); byte[] ivBytes = ap.getParameterSpec(IvParameterSpec.class).getIV(); byte[] result = cipher.doFinal(text); return Hex.encodeHex(mergeByteArrays(ivBytes, result, salt)); }
From source file:com.titilink.camel.rest.util.PasswordUtils.java
/** * Rabiitsalt?AES????SHA256??.//from w ww . j av a 2 s . c o m * * @param Rabiit ?? * @param salt ? * @return */ public synchronized static Key generateKey(char[] Rabiit, byte[] salt) { SecretKeyFactory factory; SecretKey tmpkey = null; SecretKey secret = null; try { factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); //AES128??AES128 int aeskeylen = AppProperties.getAsInt("AES_KEY_LEN", AES_KEY_LEN); KeySpec keyspec = new PBEKeySpec(Rabiit, salt, ITERATION_COUNT, aeskeylen); tmpkey = factory.generateSecret(keyspec); //AES?? secret = new SecretKeySpec(tmpkey.getEncoded(), ENCODER_AES); } catch (NoSuchAlgorithmException e) { LOGGER.error("generateKey error, no such method exception."); } // "PBKDF2WithHmacSHA256" JDK8?? catch (InvalidKeySpecException e) { LOGGER.error("generateKey error, invalid key exception."); } return secret; }
From source file:com.almende.util.EncryptionUtil.java
/** * Decrypt an encrypted string./* www . j av a 2 s .c o m*/ * * @param encryptedText * the encrypted text * @return text * @throws InvalidKeyException * the invalid key exception * @throws InvalidAlgorithmParameterException * the invalid algorithm parameter exception * @throws NoSuchAlgorithmException * the no such algorithm exception * @throws InvalidKeySpecException * the invalid key spec exception * @throws NoSuchPaddingException * the no such padding exception * @throws IllegalBlockSizeException * the illegal block size exception * @throws BadPaddingException * the bad padding exception * @throws UnsupportedEncodingException * the unsupported encoding exception */ public static String decrypt(final String encryptedText) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C); final PBEKeySpec pbeKeySpec = new PBEKeySpec(P); final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC); final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); final Cipher pbeCipher = Cipher.getInstance(ENC); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); final byte[] text = pbeCipher.doFinal(Base64.decodeBase64(encryptedText)); return new String(text, "UTF-8").intern(); }