List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java
public static String generateKey(String toEncode) { String encoded = ""; try {//from ww w . j a v a 2 s . com byte[] saltEncrypt = SALT_ENCRYPT.getBytes(); SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY); SecretKey tmp = factoryKeyEncrypt.generateSecret( new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH)); SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM); Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER); aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey); byte[] bytes = StringUtils.getBytesUtf8(toEncode); byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes); encoded = Base64.encodeBase64URLSafeString(encryptBytes); } catch (Exception e) { e.printStackTrace(); } return encoded; }
From source file:com.connorbrezinsky.msg.security.Hash.java
private static String hash(String password, byte[] salt) throws Exception { if (password == null || password.length() == 0) throw new IllegalArgumentException("Empty passwords are not supported."); SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); return Base64.encodeBase64String(key.getEncoded()); }
From source file:org.kitodo.data.encryption.DesEncrypter.java
private void initialise(String passPhrase) { int iterationCount = 19; KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), defaultSalt, iterationCount); try {/*ww w. j ava2 s. c o m*/ SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); encryptionCipher = Cipher.getInstance(secretKey.getAlgorithm()); decryptionCipher = Cipher.getInstance(secretKey.getAlgorithm()); AlgorithmParameterSpec algorithmParameterSpec = new PBEParameterSpec(defaultSalt, iterationCount); encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, algorithmParameterSpec); decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameterSpec); } catch (InvalidKeySpecException e) { logger.info("Catched InvalidKeySpecException with message: " + e.getMessage()); } catch (NoSuchAlgorithmException e) { logger.info("Catched NoSuchAlgorithmException with message: " + e.getMessage()); } catch (NoSuchPaddingException e) { logger.info("Catched NoSuchPaddingException with message: " + e.getMessage()); } catch (InvalidAlgorithmParameterException e) { logger.info("Catched InvalidAlgorithmParameterException with message: " + e.getMessage()); } catch (InvalidKeyException e) { logger.info("Catched InvalidKeyException with message: " + e.getMessage()); } }
From source file:energy.usef.environment.tool.security.VaultService.java
private static String computeMaskedPassword(String keystorePassword) throws Exception { // Create the PBE secret key SecretKeyFactory factory = SecretKeyFactory.getInstance(ToolConfig.VAULT_ENC_ALGORITHM); char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray(); PBEParameterSpec cipherSpec = new PBEParameterSpec(ToolConfig.VAULT_SALT.getBytes(), ToolConfig.VAULT_ITERATION); PBEKeySpec keySpec = new PBEKeySpec(password); SecretKey cipherKey = factory.generateSecret(keySpec); String maskedPass = PBEUtils.encode64(keystorePassword.getBytes(), ToolConfig.VAULT_ENC_ALGORITHM, cipherKey, cipherSpec);/*from w w w. j a va 2 s .c o m*/ return PicketBoxSecurityVault.PASS_MASK_PREFIX + maskedPass; }
From source file:org.j2free.security.DESEncrypter.java
/** * /* w w w . j av a 2 s .com*/ * @param passPhrase */ public DESEncrypter(String passPhrase) { try { // Create the key based on the passPhrase KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, ITERATION_COUNT); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); encoder = Cipher.getInstance(key.getAlgorithm()); decoder = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATION_COUNT); // Create the ciphers encoder.init(Cipher.ENCRYPT_MODE, key, paramSpec); decoder.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (Exception e) { throw LaunderThrowable.launderThrowable(e); } }
From source file:com.ikon.util.SecureStore.java
/** * DES decoder//from ww w . j a va 2 s .com */ public static byte[] desDecode(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.DECRYPT_MODE, sKey); byte[] dst = cipher.doFinal(src); return dst; }
From source file:org.opentravel.pubs.config.PasswordHelper.java
/** * Decrypts the given encrypted password. * //from w w w. ja v a 2s.c om * @param encryptedPassword the encrypted password to decrypt * @return */ public static String decrypt(String encryptedPassword) { try { String encryptionPassword = ConfigSettingsFactory.getConfig().getEncryptionPassword(); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionPassword.toCharArray())); Cipher pbeCipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return new String(pbeCipher.doFinal(new Base64().decode(encryptedPassword)), "UTF-8"); } catch (GeneralSecurityException | IOException e) { throw new RuntimeException("Error during password decryption.", e); } }
From source file:CipherProvider.java
/** * Create cipher for encrypt or decrypt backup content * * @param passwd passwd for encryption/*from w ww . ja v a 2 s . c o m*/ * @param mode encrypt/decrypt mode * * @return instance of cipher */ private static Cipher getCipher(final String passwd, final int mode) { /* Derive the key, given password and salt. */ Cipher cipher = null; try { SecretKeyFactory factory = null; factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); String salt = "slNadZlato#%^^&(&(5?@#5166?1561?#%^^*^&54431"; // only pseudorandom salt KeySpec spec = new PBEKeySpec(passwd.toCharArray(), salt.getBytes(), 65536, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), CIPHER_TYPE); // initialization vector byte[] iv = Arrays.copyOfRange(DigestUtils.md5(passwd), 0, 16); AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); // Cipher for encryption cipher = Cipher.getInstance(CIPHER_TYPE + "/CBC/PKCS5Padding"); cipher.init(mode, secret, paramSpec); } catch (Exception e) { e.printStackTrace(); //Todo implementovat } return cipher; }
From source file:com.liusoft.dlog4j.upgrade.StringUtils.java
/** * //from w w w. jav a2s . c o m * @param src ?? * @param key 8? * @return ?? * @throws Exception */ public static byte[] encrypt(byte[] src, byte[] key) throws Exception { // DES???? 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.ENCRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:it.scoppelletti.security.keygen.DESKeyToPropertySetProvider.java
public Properties toProperties(Key key) { byte[] data;/*from w w w.j a va2 s. c o m*/ SecretKey desKey; SecretKeyFactory keyFactory; DESKeySpec param; Properties props; if (!(key instanceof SecretKey)) { return null; } try { keyFactory = SecretKeyFactory.getInstance(DESKeyFactory.ALGORITHM); } catch (NoSuchAlgorithmException ex) { return null; } try { desKey = keyFactory.translateKey((SecretKey) key); } catch (InvalidKeyException ex) { return null; } try { param = (DESKeySpec) keyFactory.getKeySpec(desKey, DESKeySpec.class); } catch (InvalidKeySpecException ex) { return null; } props = new Properties(); props.setProperty(CryptoUtils.PROP_KEYFACTORY, DESKeyFactory.class.getName()); data = param.getKey(); props.setProperty(DESKeyFactory.PROP_KEY, Hex.encodeHexString(data)); Arrays.fill(data, Byte.MIN_VALUE); return props; }