List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:org.hawk.core.security.FileBasedCredentialsStore.java
private String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, 20)); return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8"))); }
From source file:com.github.sshw.crypt.EncryptionBean.java
public Key keyFromPassword(String password) throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1000, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); return secret; }
From source file:com.sldeditor.common.property.EncryptedPropertiesApache.java
@Override public void initialise(String password) { PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20); SecretKeyFactory kf; try {//from ww w . j a v a2 s. c o m kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray())); encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding"); decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding"); encrypter.init(Cipher.ENCRYPT_MODE, k, ps); decrypter.init(Cipher.DECRYPT_MODE, k, ps); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } }
From source file:adminpassword.Decryption.java
@SuppressWarnings("static-access") public String decrypt(String encryptedText, String idKey) throws Exception { String password = idKey;//from ww w .j a va 2s . c om Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //strip off the salt and iv ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText)); byte[] saltBytes = new byte[20]; buffer.get(saltBytes, 0, saltBytes.length); byte[] ivBytes1 = new byte[cipher.getBlockSize()]; buffer.get(ivBytes1, 0, ivBytes1.length); byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length]; buffer.get(encryptedTextBytes); // Deriving the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return new String(decryptedTextBytes); }
From source file:org.everit.password.encryptor.pbkdf2.PBKDF2PasswordEncryptorImpl.java
private String encryptSecure(final byte[] salt, final String plainPassword, final String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException { int deriverdKeyLenght = Algorithm.SUPPORTED_ALGORITHMS_AND_KEY_LENGTHS.get(algorithm); KeySpec spec = new PBEKeySpec(plainPassword.toCharArray(), salt, iterationCount, deriverdKeyLenght); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm); byte[] passwordDigest = secretKeyFactory.generateSecret(spec).getEncoded(); byte[] passwordDigestBase64 = Base64.encodeBase64(passwordDigest); String passwordDigestBase64StringUTF8 = StringUtils.newStringUtf8(passwordDigestBase64); byte[] saltBase64 = Base64.encodeBase64(salt); String saltBase64StringUTF8 = StringUtils.newStringUtf8(saltBase64); return SEPARATOR_START + algorithm + SEPARATOR_END + SEPARATOR_START + saltBase64StringUTF8 + SEPARATOR_END + passwordDigestBase64StringUTF8; }
From source file:com.rootcloud.ejb.RootCloudBean.java
public String encryptThreeDESECB(String src, String key) throws Exception { DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(1, securekey);/*from w w w. j a v a 2 s. co m*/ byte b[] = cipher.doFinal(src.getBytes()); Encoder ec = java.util.Base64.getEncoder(); return ec.encodeToString(b); }
From source file:de.alpharogroup.crypto.simple.SimpleDecryptor.java
/** * Initializes the {@link SimpleDecryptor} object. * * @throws InvalidAlgorithmParameterException * is thrown if initialization of the cypher object fails. * @throws NoSuchPaddingException/*from www .j a v a 2 s. c o m*/ * is thrown if instantiation of the cypher object fails. * @throws InvalidKeySpecException * is thrown if generation of the SecretKey object fails. * @throws NoSuchAlgorithmException * is thrown if instantiation of the SecretKeyFactory object fails. * @throws InvalidKeyException * is thrown if initialization of the cypher object fails. */ private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { if (!isInitialized()) { KeySpec keySpec = null; if (this.getPrivateKey() != null) { keySpec = new PBEKeySpec(this.getPrivateKey().toCharArray()); } if (this.getPrivateKey() == null) { keySpec = new PBEKeySpec(CryptConst.PRIVATE_KEY.toCharArray()); } final SecretKeyFactory factory = SecretKeyFactory.getInstance(CryptConst.PBEWITH_MD5AND_DES); final SecretKey key = factory.generateSecret(keySpec); this.cipher = Cipher.getInstance(key.getAlgorithm()); final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(CryptConst.SALT, CryptConst.ITERATIONCOUNT); this.cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); initialized = true; } }
From source file:de.alpharogroup.crypto.simple.SimpleEncryptor.java
/** * Initializes the {@link SimpleEncryptor} object. * /*w ww .j ava 2s . c o m*/ * @throws InvalidAlgorithmParameterException * is thrown if initialization of the cypher object fails. * @throws NoSuchPaddingException * is thrown if instantiation of the cypher object fails. * @throws InvalidKeySpecException * is thrown if generation of the SecretKey object fails. * @throws NoSuchAlgorithmException * is thrown if instantiation of the SecretKeyFactory object fails. * @throws InvalidKeyException * is thrown if initialization of the cypher object fails. */ private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { if (!isInitialized()) { final KeySpec keySpec; if (this.getPrivateKey() != null) { keySpec = new PBEKeySpec(this.getPrivateKey().toCharArray()); } else { keySpec = new PBEKeySpec(CryptConst.PRIVATE_KEY.toCharArray()); } final SecretKeyFactory factory = SecretKeyFactory.getInstance(CryptConst.PBEWITH_MD5AND_DES); final SecretKey key = factory.generateSecret(keySpec); this.cipher = Cipher.getInstance(key.getAlgorithm()); final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(CryptConst.SALT, CryptConst.ITERATIONCOUNT); this.cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); initialized = true; } }
From source file:de.codesourcery.eve.apiclient.utils.PasswordCipherProvider.java
@Override public Cipher createCipher(boolean decrypt) { final char[] password = getPassword(); if (ArrayUtils.isEmpty(password)) { log.warn("createCipher(): No password , returning NULL cipher."); return new NullCipher(); }//ww w . j av a 2 s .com try { final int iterations = 20; final byte[] salt = new byte[] { (byte) 0xab, (byte) 0xfe, 0x03, 0x47, (byte) 0xde, (byte) 0x99, (byte) 0xff, 0x1c }; final PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations); final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations); final SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5andDES"); final SecretKey secretKey; try { secretKey = fac.generateSecret(spec); } catch (InvalidKeySpecException e) { throw e; } final Cipher cipher = Cipher.getInstance("PBEWithMD5andDES"); if (decrypt) { cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeSpec); } else { cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeSpec); } return cipher; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.xml.security.test.encryption.EncryptContentTest.java
public void setUp() throws Exception { org.apache.xml.security.Init.init(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w w w.j a v a2 s.com db = dbf.newDocumentBuilder(); byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes(); DESedeKeySpec keySpec = new DESedeKeySpec(bits192); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); secretKey = keyFactory.generateSecret(keySpec); TransformerFactory tf = TransformerFactory.newInstance(); tf.newTransformer(); // Determine if we have ISO 10126 Padding - needed for Bulk AES or // 3DES encryption haveISOPadding = false; String algorithmId = JCEMapper .translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128); if (algorithmId != null) { try { if (Cipher.getInstance(algorithmId) != null) haveISOPadding = true; } catch (NoSuchAlgorithmException nsae) { } catch (NoSuchPaddingException nspe) { } } }