List of usage examples for javax.crypto.spec PBEParameterSpec PBEParameterSpec
public PBEParameterSpec(byte[] salt, int iterationCount)
From source file:org.artifactory.security.crypto.CryptoHelper.java
public static String encryptSymmetric(String plainText, SecretKey pbeKey, boolean master) { try {/*from ww w .j a va2 s . com*/ Cipher pbeCipher = Cipher.getInstance(SYM_ALGORITHM); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(PBE_SALT, PBE_ITERATION_COUNT); pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); byte[] encrypted = pbeCipher.doFinal(stringToBytes(plainText)); return convertToString(encrypted, master); } catch (Exception e) { throw new UnsupportedOperationException(e); } }
From source file:edu.ku.brc.helpers.Encryption.java
/** * Decrypt the string from its array of bytes * @param input the actual string (in bytes) that is to be decrypted * @param password a password, which is really any string, but must be the same string that was used to encrypt it. * @return a byte array of the decrypted chars * @throws Exception in case something goes wrong *//*from w ww . j a v a 2 s.c o m*/ public static byte[] decrypt(final byte[] input, final char[] password) throws Exception { /* * The first SALT_LENGTH bytes of the input ciphertext are actually the salt, not the * ciphertext. */ byte[] salt = new byte[SALT_LENGTH]; System.arraycopy(input, 0, salt, 0, SALT_LENGTH); /* * We can now create a key from our salt (extracted just above), password, and iteration * count. Same procedure to create the key as in Encryption(). */ PBEKeySpec keyspec = new PBEKeySpec(password, salt, ITERATION_COUNT); SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM); SecretKey key = skf.generateSecret(keyspec); /* * Once again, create a PBEParameterSpec object and a Cipher object. */ PBEParameterSpec paramspec = new PBEParameterSpec(salt, ITERATION_COUNT); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key, paramspec); /* * Decrypt the data. The parameters we pass into doFinal() instruct it to skip the first * SALT_LENGTH bytes of input (which are actually the salt), and then to Encryption the next * (length - SALT_LENGTH) bytes, which are the real ciphertext. */ byte[] output = cipher.doFinal(input, SALT_LENGTH, input.length - SALT_LENGTH); /* Clear the password and return the generated plaintext. */ keyspec.clearPassword(); return output; }
From source file:org.linagora.linshare.core.utils.SymmetricEnciphermentPBEwithAES.java
/** * PBE specification (pkcs5 standard)/*from www . j av a2s .c om*/ * @param salt * @return */ private static AlgorithmParameterSpec getPBEParameterSpec(byte[] salt, int iterations) { PBEParameterSpec param_spec = new PBEParameterSpec(salt, iterations); return param_spec; }
From source file:org.artifactory.security.CryptoHelper.java
public static String decryptSymmetric(String encrypted, SecretKey pbeKey) { try {/*from w w w. j a v a 2 s . co m*/ String stripped; if (encrypted.startsWith(ESCAPED_DEFAULT_ENCRYPTION_PREFIX)) { stripped = StringUtils.removeStart(encrypted, ESCAPED_DEFAULT_ENCRYPTION_PREFIX); } else { stripped = StringUtils.removeStart(encrypted, getEncryptionPrefix()); } byte[] decoded = fromBase64(stripped); Cipher pbeCipher = Cipher.getInstance(SYM_ALGORITHM); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(PBE_SALT, PBE_ITERATION_COUNT); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] decryptedBytes = pbeCipher.doFinal(decoded); return bytesToString(decryptedBytes); } catch (Exception e) { throw new UnsupportedOperationException(e); } }
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to decrypt a string. Accepts the string to be decrypted and the * name of the file which stores the key values *//*from w w w . j av a2 s .c om*/ public static String decrypt(String property, String app) throws IOException, KaavalException { SecretKey key = null; ObjectInputStream is = null; MachineKey mKey = null; int eti = 0; Cipher pbeCipher = null; InetAddress ip = InetAddress.getLocalHost(); NetworkInterface macAddress = NetworkInterface.getByInetAddress(ip); byte[] macId = macAddress.getHardwareAddress(); try { is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(app))); mKey = (MachineKey) is.readObject(); key = mKey.yek; salt = mKey.tlas; eti = mKey.eti; String ipa = ip.getHostAddress(); if (!ipa.equals(mKey.api) || !new String(macId).equals(mKey.macad)) throw new KaavalException(5, "Key file is not for this machine"); is.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, eti)); return new String(pbeCipher.doFinal(base64Decode(property))); } catch (IOException e) { throw new KaavalException(3, "Error in reading key file during decryption", e); } catch (KaavalException e) { throw e; } catch (Exception e) { throw new KaavalException(4, "Error during decryption", e); } finally { if (is != null) is.close(); } }
From source file:org.jboss.ejb3.examples.ch05.encryption.EncryptionBean.java
/** * Initializes this service before it may handle requests * /*from w w w . jav a 2s . com*/ * @throws Exception If some unexpected error occurred */ @PostConstruct public void initialize() throws Exception { // Log that we're here log.info("Initializing, part of " + PostConstruct.class.getName() + " lifecycle"); /* * Symmetric Encryption */ // Obtain parameters used in initializing the ciphers final String cipherAlgorithm = DEFAULT_ALGORITHM_CIPHER; final byte[] ciphersSalt = DEFAULT_SALT_CIPHERS; final int ciphersIterationCount = DEFAULT_ITERATION_COUNT_CIPHERS; final String ciphersPassphrase = this.getCiphersPassphrase(); // Obtain key and param spec for the ciphers final KeySpec ciphersKeySpec = new PBEKeySpec(ciphersPassphrase.toCharArray(), ciphersSalt, ciphersIterationCount); final SecretKey ciphersKey = SecretKeyFactory.getInstance(cipherAlgorithm).generateSecret(ciphersKeySpec); final AlgorithmParameterSpec paramSpec = new PBEParameterSpec(ciphersSalt, ciphersIterationCount); // Create and init the ciphers this.encryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); this.decryptionCipher = Cipher.getInstance(ciphersKey.getAlgorithm()); encryptionCipher.init(Cipher.ENCRYPT_MODE, ciphersKey, paramSpec); decryptionCipher.init(Cipher.DECRYPT_MODE, ciphersKey, paramSpec); // Log log.info("Initialized encryption cipher: " + this.encryptionCipher); log.info("Initialized decryption cipher: " + this.decryptionCipher); /* * One-way Hashing */ // Get the algorithm for the MessageDigest final String messageDigestAlgorithm = this.getMessageDigestAlgorithm(); // Create the MessageDigest try { this.messageDigest = MessageDigest.getInstance(messageDigestAlgorithm); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Could not obtain the " + MessageDigest.class.getSimpleName() + " for algorithm: " + messageDigestAlgorithm, e); } log.info("Initialized MessageDigest for one-way hashing: " + this.messageDigest); }
From source file:org.artifactory.security.crypto.CryptoHelper.java
public static String decryptSymmetric(String encrypted, SecretKey pbeKey, boolean master) { try {//from w w w .ja va 2 s .co m byte[] bytes = convertToBytes(encrypted, master); Cipher pbeCipher = Cipher.getInstance(SYM_ALGORITHM); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(PBE_SALT, PBE_ITERATION_COUNT); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] decryptedBytes = pbeCipher.doFinal(bytes); return bytesToString(decryptedBytes); } catch (Exception e) { throw new UnsupportedOperationException(e); } }
From source file:org.kawanfw.commons.util.convert.Pbe.java
/** * Encrypt or decrypt a file using a password * /*from w w w . j a v a 2s . c o m*/ * @param mode * Cipher.ENCRYPT_MODE or Cipher.DECRYPT_MODE * @param fileIn * the file to encrypt or Decrypt. * @param fileOut * the resulting encrypted/decrypted file * @param password * the password to use * * @throws Exception */ private void cipher(int mode, File fileIn, File fileOut, 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 (fileIn == null) { throw new IllegalArgumentException("in File can not be null!"); } if (fileOut == null) { throw new IllegalArgumentException("out File 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 = 1; // 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); InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(fileIn)); out = new BufferedOutputStream(new FileOutputStream(fileOut)); byte[] input = new byte[2048 * 10]; int bytesRead; while ((bytesRead = in.read(input)) != -1) { byte[] output = pbeCipher.update(input, 0, bytesRead); if (output != null) out.write(output); } byte[] output = pbeCipher.doFinal(); if (output != null) out.write(output); out.flush(); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } }
From source file:org.coronastreet.gpxconverter.AccountManager.java
public static String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException { 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)); return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8"))); }
From source file:org.coronastreet.gpxconverter.AccountManager.java
public static String decrypt(String property) throws GeneralSecurityException, IOException { 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)); return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8"); }