List of usage examples for javax.crypto.spec PBEParameterSpec PBEParameterSpec
public PBEParameterSpec(byte[] salt, int iterationCount)
From source file:JavaTron.JTP.java
/** * Encrypts a string/* w w w . j av a2 s. com*/ * @param property * @return An encrypted string */ public static String encrypt(String property) { String p = new String(); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); p = base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (Exception e) { e.printStackTrace(); } return p; }
From source file:com.stimulus.archiva.store.MessageStore.java
public void init() throws MessageStoreException { tempfiles = Config.getFileSystem().getTempFiles(); byte[] salt = Config.getConfig().getSalt(); String passPhrase = getPassPhrase(); if (!isDefaultPassPhraseModified()) logger.warn("archiving is disabled. encryption password is not set."); int iterationCount = 17; String algorithm = Config.getConfig().getPBEAlgorithm(); // "PBEWithMD5AndDES") // Create the key try {/*from w w w .j a v a2 s. c o m*/ KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); key = SecretKeyFactory.getInstance(algorithm).generateSecret(keySpec); paramSpec = new PBEParameterSpec(salt, iterationCount); } catch (java.security.NoSuchAlgorithmException e) { throw new MessageStoreException( "failed to locate desired encryption algorithm {algorithm='" + algorithm + "'", logger); } catch (Exception e) { throw new MessageStoreException(e.toString(), e, logger); } }
From source file:bioLockJ.module.agent.MailAgent.java
private String decrypt(final String property) { String decryptedPassword = null; try {//from w w w.j a va 2 s . c o m final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); final Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); decryptedPassword = new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8"); } catch (final Exception ex) { Log.out.error(ex.getMessage(), ex); } return decryptedPassword; }
From source file:org.tdmx.core.system.env.StringEncrypter.java
/** * Constructor used to create this object. Responsible for setting and initializing this object's encrypter and * decrypter Cipher instances given a Pass Phrase and algorithm. * // w ww .j a v a 2 s . c o m * @param passPhrase * Pass Phrase used to initialize both the encrypter and decrypter instances. */ public StringEncrypter(String passPhrase) { // 8-bytes Salt byte[] salt = { (byte) 0xA4, (byte) 0x9B, (byte) 0xC8, (byte) 0x72, (byte) 0x46, (byte) 0x45, (byte) 0xE3, (byte) 0x93 }; // Iteration count int iterationCount = 1024; try { KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); key = SecretKeyFactory.getInstance("PBEWithSHA1AndDESede").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameters to the ciphers paramSpec = new PBEParameterSpec(salt, iterationCount); initCipher(); } catch (InvalidKeySpecException e) { log.error("EXCEPTION: InvalidKeySpecException", e); } catch (NoSuchPaddingException e) { log.error("EXCEPTION: NoSuchPaddingException", e); } catch (NoSuchAlgorithmException e) { log.error("EXCEPTION: NoSuchAlgorithmException", e); } }
From source file:com.ephesoft.dcma.encryption.core.EncryptorDecryptor.java
/** * This method is used to start the encryption process. * //from w ww. j a v a2 s .co m * @param data byte[] * @param salt byte[] * @param isEncryption boolean * @return byte[] * @throws CryptographyException {@link CryptographyException} */ public byte[] startCrypting(byte[] data, byte[] salt, boolean isEncryption) throws CryptographyException { KeySpec keySpec = new PBEKeySpec(EncryptionConstants.KEY.toCharArray(), salt, EncryptionConstants.ITERATION_COUNT); SecretKey key; byte[] finalBytes = null; try { key = SecretKeyFactory.getInstance(EncryptionConstants.ALGORITHM).generateSecret(keySpec); Cipher ecipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, EncryptionConstants.ITERATION_COUNT); if (isEncryption) { ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); } else { ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } finalBytes = ecipher.doFinal(data); } catch (InvalidKeySpecException e) { if (isEncryption) { LOGGER.error("Encryption : Key used is invalid", e); throw new CryptographyException("Key used is invalid", e); } else { LOGGER.error("Decryption : Key used is invalid", e); throw new CryptographyException("Key used is invalid", e); } } catch (NoSuchAlgorithmException e) { if (isEncryption) { LOGGER.error("Encryption : Algorithm used does not exist", e); throw new CryptographyException("Algorithm used does not exist", e); } else { LOGGER.error("Decryption : Algorithm used does not exist", e); throw new CryptographyException("Algorithm used does not exist", e); } } catch (NoSuchPaddingException e) { if (isEncryption) { LOGGER.error("Encryption : Padding used does not exist", e); throw new CryptographyException("Padding used does not exist", e); } else { LOGGER.error("Decryption : Padding used does not exist", e); throw new CryptographyException("Padding used does not exist", e); } } catch (InvalidKeyException e) { if (isEncryption) { LOGGER.error("Encryption : Key generated is invalid", e); throw new CryptographyException("Key generated is invalid", e); } else { LOGGER.error("Decryption : Key generated is invalid", e); throw new CryptographyException("Key generated is invalid", e); } } catch (InvalidAlgorithmParameterException e) { if (isEncryption) { LOGGER.error("Encryption : Algorithm parameter is invalid", e); throw new CryptographyException("Algorithm parameter is invalid", e); } else { LOGGER.error("Decryption : Algorithm parameter is invalid", e); throw new CryptographyException("Algorithm parameter is invalid", e); } } catch (IllegalBlockSizeException e) { if (isEncryption) { LOGGER.error("Encryption : Block size is illegal", e); throw new CryptographyException("Block size is illegal", e); } else { LOGGER.error("Decryption : Block size is illegal", e); throw new CryptographyException("Block size is illegal", e); } } catch (BadPaddingException e) { if (isEncryption) { LOGGER.error("Encryption : Padding done is invalid", e); throw new CryptographyException("Padding done is invalid", e); } else { LOGGER.error("Decryption : Padding done is invalid", e); throw new CryptographyException("Padding done is invalid", e); } } return finalBytes; }
From source file:org.kawanfw.commons.util.convert.Pbe.java
/** * Encrypt or decrypt a string using a password * //from w ww .j a va 2s .c o m * @param mode * Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE * @param in * the string to encrypt or Decrypt. if to decrypt: string must * be Hex encoded * @param password * the password to use * @return if Cipher.ENCRYPT_MODE: the encrypted string in hexadecimal if * Cipher.DECRYPT_MODE: the decrypted string in clear readable * format * * @throws Exception */ private String cipher(int mode, String in, char[] password) throws Exception { if (mode != Cipher.ENCRYPT_MODE && mode != Cipher.DECRYPT_MODE) { throw new IllegalArgumentException("mode is not Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE!"); } if (in == null) { throw new IllegalArgumentException("in string can not be null!"); } if (password == null) { throw new IllegalArgumentException("password can not be null!"); } PBEKeySpec pbeKeySpec; PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Salt byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; // Iteration count int count = 20; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(mode, pbeKey, pbeParamSpec); // Our cleartext byte[] inText = null; if (mode == Cipher.ENCRYPT_MODE) { inText = in.getBytes(); } else { inText = CodecHex.decodeHex(in.toCharArray()); } // Encrypt the cleartext byte[] ciphertext = pbeCipher.doFinal(inText); if (mode == Cipher.ENCRYPT_MODE) { return new String(CodecHex.encodeHex(ciphertext)); } else { return new String(ciphertext); } }
From source file:edu.ku.brc.helpers.Encryption.java
/** * Encrypts the string from its array of bytes * @param input the actual string (in bytes) that is to be encrypted * @param password a password, which is really any string, but must be the same string that was used to decrypt it. * @return a byte array of the encrypted chars * @throws Exception in case something goes wrong *///from w w w. ja va 2s . c o m public static byte[] encrypt(byte[] input, char[] password) throws Exception { /* * Get ourselves a random number generator, needed in a number of places for encrypting. */ SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); //$NON-NLS-1$ /* * A "salt" is considered an essential part of password-based encryption. The salt is * selected at random for each encryption. It is not considered "sensitive", so it is tacked * onto the generated ciphertext without any special processing. It doesn't matter if an * attacker actually gets the salt. The salt is used as part of the key, with the very * useful result that if you Encryption the same plaintext with the same password twice, you * get *different* ciphertexts. There are lots of pages on the 'net with information about * salts and password-based encryption, so read them if you want more details. Suffice to * say salt=good, no salt=bad. */ byte[] salt = new byte[SALT_LENGTH]; sr.nextBytes(salt); /* * We've now got enough information to build the actual key. We do this by encapsulating the * variables in a PBEKeySpec and using a SecretKeyFactory to transform the spec into a key. */ PBEKeySpec keyspec = new PBEKeySpec(password, salt, ITERATION_COUNT); SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = skf.generateSecret(keyspec); /* * We'll use a ByteArrayOutputStream to conveniently gather up data as it's encrypted. */ ByteArrayOutputStream baos = new ByteArrayOutputStream(); /* * We've to a key, but to actually Encryption something, we need a "cipher". The cipher is * created, then initialized with the key, salt, and iteration count. We use a * PBEParameterSpec to hold the salt and iteration count needed by the Cipher object. */ PBEParameterSpec paramspec = new PBEParameterSpec(salt, ITERATION_COUNT); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key, paramspec, sr); /* * First, in our output, we need to save the salt in plain unencrypted form. */ baos.write(salt); /* * Next, Encryption our plaintext using the Cipher object, and write it into our output buffer. */ baos.write(cipher.doFinal(input)); /* * We're done. For security reasons, we probably want the PBEKeySpec object to clear its * internal copy of the password, so it can't be stolen later. */ keyspec.clearPassword(); return baos.toByteArray(); }
From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java
/** * PKCS #8 encode and encrypt a private key. * * @return The encrypted encoding// www. jav a 2 s.co m * @param privateKey * The private key * @param pbeType * PBE algorithm to use for encryption * @param password * Encryption password * @throws CryptoException * Problem encountered while getting the encoded private key * @throws IOException * If an I/O error occurred */ public static byte[] getEncrypted(PrivateKey privateKey, Pkcs8PbeType pbeType, Password password) throws CryptoException, IOException { try { byte[] pkcs8 = get(privateKey); // Generate PBE secret key from password SecretKeyFactory keyFact = SecretKeyFactory.getInstance(pbeType.jce()); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKey pbeKey = keyFact.generateSecret(pbeKeySpec); // Generate random salt and iteration count byte[] salt = generateSalt(); int iterationCount = generateIterationCount(); // Store in algorithm parameters PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount); AlgorithmParameters params = AlgorithmParameters.getInstance(pbeType.jce()); params.init(pbeParameterSpec); // Create PBE cipher from key and params Cipher cipher = Cipher.getInstance(pbeType.jce()); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, params); // Encrypt key byte[] encPkcs8 = cipher.doFinal(pkcs8); // Create and return encrypted private key information EncryptedPrivateKeyInfo encPrivateKeyInfo = new EncryptedPrivateKeyInfo(params, encPkcs8); return encPrivateKeyInfo.getEncoded(); } catch (GeneralSecurityException ex) { throw new CryptoException("NoEncryptPkcs8PrivateKey.exception.message", ex); } }
From source file:JavaTron.JTP.java
/** * Decrypts an encrypted string/*from w w w .j av a 2 s . c om*/ * @param property * @return A plaintext string */ public static String decrypt(String property) { String p = new String(); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); p = new String(pbeCipher.doFinal(base64Decode(property))); } catch (Exception e) { e.printStackTrace(); } return p; }
From source file:org.apache.ranger.plugin.util.PasswordUtils.java
private String decrypt() throws IOException { String ret = null;/*from w ww .jav a 2 s . c o m*/ try { byte[] decodedPassword = Base64.decode(password); Cipher engine = Cipher.getInstance(CRYPT_ALGO); PBEKeySpec keySpec = new PBEKeySpec(encryptKey); SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO); SecretKey key = skf.generateSecret(keySpec); engine.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT)); String decrypted = new String(engine.doFinal(decodedPassword)); int foundAt = decrypted.indexOf(LEN_SEPARATOR_STR); if (foundAt > -1) { if (decrypted.length() > foundAt) { ret = decrypted.substring(foundAt + 1); } else { ret = ""; } } else { ret = null; } } catch (Throwable t) { LOG.error("Unable to decrypt password due to error", t); throw new IOException("Unable to decrypt password due to error", t); } return ret; }