List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec
public PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength)
From source file:com.cyberninjas.xerobillableexpenses.util.Settings.java
public Settings() { try {/* ww w . j a va2 s . c o m*/ String parentClass = new Exception().getStackTrace()[1].getClassName(); this.prefs = Preferences.userNodeForPackage(Class.forName(parentClass)); Random r = new SecureRandom(); //Set IV this.iv = prefs.getByteArray("DRUGS", null); //Pick Random PWD byte[] b = new byte[128]; r.nextBytes(b); MessageDigest sha = MessageDigest.getInstance("SHA-1"); sha.update(b); String sHash = new String(Base64.encodeBase64(sha.digest())); String password = prefs.get("LAYOUT", sHash); if (password.equals(sHash)) prefs.put("LAYOUT", sHash); //Keep 'em Guessing r.nextBytes(b); sha.update(b); prefs.put("PASSWORD", new String(Base64.encodeBase64(sha.digest()))); //Set Random Salt byte[] tSalt = new byte[8]; r.nextBytes(tSalt); byte[] salt = prefs.getByteArray("HIMALAYAN", tSalt); if (Arrays.equals(salt, tSalt)) prefs.putByteArray("HIMALAYAN", salt); /* Derive the key, given password and salt. */ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); this.secret = new SecretKeySpec(tmp.getEncoded(), "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidKeySpecException ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.haulmont.cuba.core.sys.encryption.Sha1EncryptionModule.java
protected KeySpec getKeySpec(String content, byte[] salt) { return new PBEKeySpec(content.toCharArray(), salt, ITERATIONS, DERIVED_KEY_LENGTH_BITS); }
From source file:org.gravidence.gravifon.util.PasswordUtils.java
/** * Generates salted PBKDF2 hash for given password and salt. * /*from www .java2 s .c o m*/ * @param password plaintext password * @param salt cryptographic salt * @return salted PBKDF2 hash */ private static String hash(String password, byte[] salt) { if (StringUtils.isEmpty(password)) { throw new IllegalArgumentException("password"); } if (ArrayUtils.isEmpty(salt)) { throw new IllegalArgumentException("salt"); } PBEKeySpec pbeks = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS_NUMBER, HASH_LENGTH); byte[] hash; try { hash = skf.generateSecret(pbeks).getEncoded(); } catch (InvalidKeySpecException ex) { throw new GravifonException(GravifonError.INTERNAL, "Failed to generate hash.", ex); } return Base64.encodeBase64String(hash); }
From source file:com.fegor.alfresco.security.crypto.Crypto.java
/** * Encryption configuration//from ww w . j ava 2 s. c om * * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws InvalidParameterSpecException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws UnsupportedEncodingException * @throws InvalidKeyException */ public void configEncrypt() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidKeyException { SecretKeyFactory factory = null; SecretKey tmp = null; salt_pos = new byte[SALT_LEN]; SecureRandom rnd = new SecureRandom(); rnd.nextBytes(salt_pos); if (logger.isDebugEnabled()) logger.debug(this.getClass().getName() + ": [salt: " + (new String(Hex.encodeHex(salt_pos))) + "]"); factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); /* * http://www.javamex.com/tutorials/cryptography/unrestricted_policy_files * .shtml */ KeySpec spec = new PBEKeySpec(password.toCharArray(), salt_pos, ITERATIONS, KEYLEN_BITS); tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); eCipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = eCipher.getParameters(); vector_init = params.getParameterSpec(IvParameterSpec.class).getIV(); if (logger.isDebugEnabled()) logger.debug( this.getClass().getName() + ": [vector ini: " + (new String(Hex.encodeHex(vector_init))) + "]"); }
From source file:org.entrystore.repository.security.Password.java
private static String hash(String password, byte[] salt) { if (password == null || password.length() == 0) { throw new IllegalArgumentException("Empty passwords are not supported"); }// ww w.j av a 2 s. c o m try { long before = new Date().getTime(); SecretKey key = secretKeyFactory .generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); log.info("Password hashing took " + (new Date().getTime() - before) + " ms"); return Base64.encodeBase64String(key.getEncoded()); } catch (GeneralSecurityException gse) { log.error(gse.getMessage()); } return null; }
From source file:spacetraffic.kiv.zcu.cz.gameelement.MinigamePasswordHasher.java
/** * Method for generating key./*from w w w . j av a 2 s . c om*/ * @return key * @throws Exception */ private Key generateKey() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM); char[] pdkbf2Password = PDKBF2_PASSWORD.toCharArray(); byte[] salt = SALT.getBytes(CHARSET); KeySpec keySpec = new PBEKeySpec(pdkbf2Password, salt, 65536, 128); SecretKey secretKey = factory.generateSecret(keySpec); byte[] encodedKey = secretKey.getEncoded(); return new SecretKeySpec(encodedKey, "AES"); }
From source file:com.redsqirl.workflow.utils.FileStream.java
private static SecretKey generateKey() throws Exception { PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); return keyFactory.generateSecret(keySpec); }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
private static Key generateKeySpec(String key) throws NoSuchAlgorithmException, InvalidKeySpecException { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); return secret; //To change body of generated methods, choose Tools | Templates. }
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];/* w w w .j a v a 2s . co m*/ } return diff == 0; }
From source file:tds.itemrenderer.security.Encryption.java
/** * initializes ciphers and adds jce provider if provided * * @throws TDS.Shared.Security.TDSEncryptionException *//*from w ww . jav a 2s.c om*/ @PostConstruct protected void init() { if (encryptionKey == null || StringUtils.isBlank(encryptionKey) || encryptionKey.length() < MINIMUM_KEY_LENGTH) { throw new TDSEncryptionException( String.format("Number of characters for key must be greater than %s", MINIMUM_KEY_LENGTH)); } try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBE_KEY_ALGORITHM); KeySpec keySpec = new PBEKeySpec(encryptionKey.toCharArray(), PBE_SALT, PBE_NUM_ITERATIONS, PBE_KEY_LENGTH); SecretKey secretKeyTemp; secretKeyTemp = secretKeyFactory.generateSecret(keySpec); secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), CIPHER_ALGORITHM); encryptCipher = Cipher.getInstance(TRANSFORMATION); decryptCipher = Cipher.getInstance(TRANSFORMATION); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); } catch (NoSuchAlgorithmException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Algorithm is not available"); } catch (InvalidKeySpecException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key specification is not valid"); } catch (NoSuchPaddingException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Padding is not valid"); } catch (InvalidKeyException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key is not valid"); } }