List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:io.hawkcd.agent.services.SecurityService.java
private Key generateKey() throws Exception { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); char[] password = PASSWORD.toCharArray(); byte[] salt = getBytes(SALT); KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); byte[] encoded = tmp.getEncoded(); return new SecretKeySpec(encoded, "AES"); }
From source file:com.wms.studio.security.utils.Digests.java
public static byte[] desEncrypt(String pwd, byte[] key) throws Exception { // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher??//from w ww .j a v a 2 s .c om Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.ENCRYPT_MODE, securekey, random); return cipher.doFinal(pwd.getBytes()); }
From source file:org.eclipse.che.ide.ext.datasource.server.EncryptTextService.java
public String decryptText(String textToDecrypt) throws Exception { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(getMasterPassword())); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return new String(pbeCipher.doFinal(Base64.decodeBase64(textToDecrypt)), "UTF-8"); }
From source file:org.webical.dao.encryption.impl.DesEncryptor.java
/** * Creates the DesEncryptor//w w w . j a va2s . c o m * @param passPhrase the passphrase to use in encryption and decryption * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeySpecException */ public DesEncryptor(String passPhrase) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException { // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); encryptCipher = Cipher.getInstance(key.getAlgorithm()); decryptCipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers encryptCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); decryptCipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:cherry.goods.crypto.RSASignatureTest.java
private RSASignature create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);/*from w w w .j av a2s . co m*/ KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSASignature impl = new RSASignature(); impl.setAlgorithm("SHA256withRSA"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }
From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java
/** * Description ?/* w ww . jav a2s . co m*/ * * @param data * @param key byte * @return * @throws Exception */ private static byte[] encrypt(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.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
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:cherry.goods.crypto.RSACryptoTest.java
private RSACrypto create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);/*w w w . j av a2 s .c o m*/ KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSACrypto impl = new RSACrypto(); impl.setAlgorithm("RSA/ECB/PKCS1Padding"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }
From source file:net.mobid.codetraq.utils.PasswordProcessor.java
/** * Decrypts a text using the <code>passPhrase</code> above and an algorithm supported * by your virtual machine implementation. You can change the default algorithm with * another algorithm, but please make sure your virtual machine supports it. * @param valueToDecrypt - text to decrypt * @return a plain text// www.jav a2s. com */ public static String decryptString(String valueToDecrypt) { String output = null; try { KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations); SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations); cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec); // begin decrypting... byte[] encrypted = new Base64().decode(valueToDecrypt); byte[] utf8 = cipher.doFinal(encrypted); output = new String(utf8, "UTF8"); } catch (Exception ex) { Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:net.sf.jftp.config.Crypto.java
public static String Decrypt(String str) { // create cryptography object SecretKeyFactory keyFactory;//from w w w . j a va 2s . co m try { keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { // We could try another algorithm, but it is highly unlikely that this would be the case return ""; } // init key SecretKey key; try { key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); } catch (InvalidKeySpecException e) { return ""; } // init cipher Cipher pbeCipher; try { pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); } catch (NoSuchAlgorithmException e) { return ""; } catch (NoSuchPaddingException e) { return ""; } try { pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); } catch (InvalidKeyException e) { return ""; } catch (InvalidAlgorithmParameterException e) { return ""; } // decode & return decoded string String dec; try { dec = new String(pbeCipher.doFinal(base64Decode(str))); } catch (IllegalBlockSizeException e) { return ""; } catch (BadPaddingException e) { return ""; } catch (IOException e) { return ""; } return dec; }