List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:net.alegen.datpass.library.crypto.CryptoManager.java
public SecretKey derivateKey(KeyDerivationFunctions function, String password, byte[] salt, int length, int iterations) { try {//from w w w . j a v a 2 s .c o m SecretKeyFactory factory = SecretKeyFactory.getInstance(function.toString()); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, length); return factory.generateSecret(spec); } catch (NoSuchAlgorithmException e) { log.error("The required algorithm is not supported by the current JVM."); return null; } catch (InvalidKeySpecException e) { log.error("The key spec is invalid."); return null; } }
From source file:org.kuali.rice.core.impl.encryption.DemonstrationGradeEncryptionServiceImpl.java
private SecretKey unwrapEncodedKey(String key) throws Exception { KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey desKey = keygen.generateKey(); // Create the cipher Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init((Cipher.UNWRAP_MODE), desKey); byte[] bytes = Base64.decodeBase64(key.getBytes()); SecretKeyFactory desFactory = SecretKeyFactory.getInstance("DES"); DESKeySpec keyspec = new DESKeySpec(bytes); SecretKey k = desFactory.generateSecret(keyspec); return k;/* w ww. j av a2s . co m*/ }
From source file:com.cyberninjas.xerobillableexpenses.util.Settings.java
public Settings() { try {//from w ww . j a v a 2s .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:org.openmrs.module.clinicalsummary.io.UploadSummariesTask.java
/** * Method to initialize the cipher object with the correct encryption algorithm. * * @throws Exception/*from w w w .j a va 2 s .com*/ */ protected void initializeCipher() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance(TaskConstants.SECRET_KEY_FACTORY); KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128); SecretKey tmp = factory.generateSecret(spec); if (log.isDebugEnabled()) log.debug("Secret Key Length: " + tmp.getEncoded().length); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), TaskConstants.KEY_SPEC); cipher = Cipher.getInstance(TaskConstants.CIPHER_CONFIGURATION); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(initVector)); }
From source file:mitm.common.security.KeyEncoderTest.java
@Test public void testSerializeSecretKey() throws Exception { PBEKeySpec keySpec = new PBEKeySpec("test".toCharArray(), new byte[] { 1 }, 1); SecretKeyFactory secretKeyFactory = securityFactory .createSecretKeyFactory("PBEWITHSHA256AND128BITAES-CBC-BC"); Key secretKey = secretKeyFactory.generateSecret(keySpec); byte[] serialized = KeyEncoder.encode(secretKey, encryptor); assertNotNull(serialized);/* ww w. j a va2 s.c o m*/ Key key = KeyEncoder.decode(serialized, encryptor); assertTrue(key instanceof SecretKey); assertTrue(ArrayUtils.isEquals(secretKey.getEncoded(), key.getEncoded())); }
From source file:org.eclipse.che.api.crypt.server.JCEEncryptTextService.java
private SecretKey generateSecret() throws NoSuchAlgorithmException, InvalidKeySpecException { final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(this.secretKeyFactoryAlgorithm); final KeySpec keySpec = new PBEKeySpec(getMasterPassword(), this.salt, 65536, this.keySize); final SecretKey tempKey = keyFactory.generateSecret(keySpec); final SecretKey secret = new SecretKeySpec(tempKey.getEncoded(), this.cipher); return secret; }
From source file:org.apache.nifi.processors.standard.util.PasswordBasedEncryptor.java
public PasswordBasedEncryptor(final String algorithm, final String providerName, final char[] password, KeyDerivationFunction kdf) {/*from w w w . ja v a 2s . c o m*/ super(); try { // initialize cipher this.cipher = Cipher.getInstance(algorithm, providerName); this.kdf = kdf; if (isOpenSSLKDF()) { this.saltSize = OPENSSL_EVP_SALT_SIZE; this.iterationsCount = OPENSSL_EVP_KDF_ITERATIONS; } else { int algorithmBlockSize = cipher.getBlockSize(); this.saltSize = (algorithmBlockSize > 0) ? algorithmBlockSize : DEFAULT_SALT_SIZE; } // initialize SecretKey from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, providerName); this.secretKey = factory.generateSecret(pbeKeySpec); } catch (Exception e) { throw new ProcessException(e); } }
From source file:org.cryptonode.jncryptor.AES256v2Cryptor.java
@Override public SecretKey keyForPassword(char[] password, byte[] salt) throws CryptorException { Validate.notNull(salt, "Salt value cannot be null."); Validate.isTrue(salt.length == SALT_LENGTH, "Salt value must be %d bytes.", SALT_LENGTH); try {/*from w ww .j a v a2 s. c o m*/ SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_DERIVATION_ALGORITHM); SecretKey tmp = factory .generateSecret(new PBEKeySpec(password, salt, PBKDF_ITERATIONS, AES_256_KEY_SIZE * 8)); return new SecretKeySpec(tmp.getEncoded(), AES_NAME); } catch (GeneralSecurityException e) { throw new CryptorException( String.format("Failed to generate key from password using %s.", KEY_DERIVATION_ALGORITHM), e); } }
From source file:com.jwt.security.auth.cryptographics.Crypto.java
private SecretKey generateKey(String salt, String passphrase) throws InvalidKeySpecException { try {/* w w w . j a v a 2 s. c o m*/ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), cryptoProps.getIterationCount(), cryptoProps.getKeySize()); SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); return key; } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw fail(e); } }
From source file:com.nextep.designer.core.services.impl.RepositoryService.java
public RepositoryService() { pbeParamSpec = new PBEParameterSpec(salt, iterations); pbeKeySpec = new PBEKeySpec(encryptionPassword.toCharArray()); try {/*ww w .j a v a 2 s .c o m*/ SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM); key = factory.generateSecret(pbeKeySpec); } catch (NoSuchAlgorithmException e) { LOGGER.error(CoreMessages.getString("repositoryService.encryptionNotFound"), e); //$NON-NLS-1$ } catch (InvalidKeySpecException e) { LOGGER.error(CoreMessages.getString("repositoryService.encryptionKeyFail"), e); //$NON-NLS-1$ } }