List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
From source file:org.apache.nifi.processors.standard.util.crypto.OpenSSLPKCS5CipherProvider.java
protected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, boolean encryptMode) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { if (encryptionMethod == null) { throw new IllegalArgumentException("The encryption method must be specified"); }//from www .ja v a 2 s .c o m if (StringUtils.isEmpty(password)) { throw new IllegalArgumentException("Encryption with an empty password is not supported"); } validateSalt(encryptionMethod, salt); String algorithm = encryptionMethod.getAlgorithm(); String provider = encryptionMethod.getProvider(); // Initialize secret key from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider); SecretKey tempKey = factory.generateSecret(pbeKeySpec); final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, getIterationCount()); Cipher cipher = Cipher.getInstance(algorithm, provider); cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec); return cipher; }
From source file:eap.util.EDcodeUtil.java
private static byte[] des(byte[] data, byte[] key, int opMode) { try {/* ww w . j a v a 2 s . c o m*/ DESKeySpec desKey = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES", provider); SecretKey secureKey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance("DES", provider); // SecureRandom secureRandom = new SecureRandom(); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); // provider cipher.init(opMode, secureKey, secureRandom); return cipher.doFinal(data); } catch (Exception e) { throw new IllegalArgumentException(e.getMessage(), e); } }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ??//from www.ja va 2 s . co m * * @param ctx * * @param envelope * ? * @param privateKey * ? * @return key */ public static Key openDigitalEnvelope(SecurityContext ctx, String envelope, Key privateKey) { try { Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm(), ctx.getJceProvider()); cipher.init(Cipher.DECRYPT_MODE, privateKey); envelope = StringTools.replaceIgnoreCase(envelope, " ", ""); byte[] key = cipher.doFinal(Base64.decodeBase64(envelope)); SecretKeyFactory skf = SecretKeyFactory.getInstance(ctx.getSymmetryKeyAlgorithm(), ctx.getJceProvider()); DESKeySpec keySpec = new DESKeySpec(key); Key symmetryKey = skf.generateSecret(keySpec); return symmetryKey; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:org.apache.nifi.security.util.crypto.CipherUtility.java
/** * Initializes a {@link Cipher} object with the given PBE parameters. * * @param algorithm the algorithm//w ww . ja va2 s. c o m * @param provider the JCA provider * @param password the password * @param salt the salt * @param iterationCount the KDF iteration count * @param encryptMode true to encrypt; false to decrypt * @return the initialized Cipher * @throws IllegalArgumentException if any parameter is invalid */ public static Cipher initPBECipher(String algorithm, String provider, String password, byte[] salt, int iterationCount, boolean encryptMode) throws IllegalArgumentException { try { // Initialize secret key from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, provider); SecretKey tempKey = factory.generateSecret(pbeKeySpec); final PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterationCount); Cipher cipher = Cipher.getInstance(algorithm, provider); cipher.init(encryptMode ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, tempKey, parameterSpec); return cipher; } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IllegalArgumentException("One or more parameters to initialize the PBE cipher were invalid", e); } }
From source file:org.ejbca.util.StringTools.java
public static String pbeDecryptStringWithSha256Aes192(final String in) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, UnsupportedEncodingException { if (CryptoProviderTools.isUsingExportableCryptography()) { log.warn("De-obfuscation not possible due to weak crypto policy."); return in; }/*from w w w .j av a 2 s.c om*/ final String algorithm = "PBEWithSHA256And192BitAES-CBC-BC"; final Cipher c = Cipher.getInstance(algorithm, "BC"); final PBEKeySpec keySpec = new PBEKeySpec(p, getSalt(), iCount); final SecretKeyFactory fact = SecretKeyFactory.getInstance(algorithm, "BC"); c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec)); final byte[] dec = c.doFinal(Hex.decode(in.getBytes("UTF-8"))); return new String(dec); }
From source file:org.cesecore.util.StringTools.java
public static String pbeDecryptStringWithSha256Aes192(final String in) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, UnsupportedEncodingException { CryptoProviderTools.installBCProviderIfNotAvailable(); if (CryptoProviderTools.isUsingExportableCryptography()) { log.warn("De-obfuscation not possible due to weak crypto policy."); return in; }/* www .j a va 2 s . c om*/ final String algorithm = "PBEWithSHA256And192BitAES-CBC-BC"; final Cipher c = Cipher.getInstance(algorithm, "BC"); final PBEKeySpec keySpec = new PBEKeySpec(p, getSalt(), iCount); final SecretKeyFactory fact = SecretKeyFactory.getInstance(algorithm, "BC"); c.init(Cipher.DECRYPT_MODE, fact.generateSecret(keySpec)); final byte[] dec = c.doFinal(Hex.decode(in.getBytes("UTF-8"))); return new String(dec); }