Refine search
private String encrypt(String property) throws GeneralSecurityException, UnsupportedEncodingException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return base64Encode(pbeCipher.doFinal(property.getBytes("UTF-8"))); }
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"))); 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");
byte[] salt = { (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c, (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99 }; int count = 20; PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); SealedObject sealed = new SealedObject(object, cipher); ...
private String decrypt(String property) throws GeneralSecurityException, IOException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(habridgeKey)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8"); }
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); // Prompt user for encryption password. // Collect user password as char array (using the // "readPassword" method from above), and convert // it into a SecretKey object, using a PBE key // factory. System.out.print("Enter encryption password: "); System.out.flush(); pbeKeySpec = new PBEKeySpec(readPassword(System.in)); 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(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); // Our cleartext byte[] cleartext = "This is another example".getBytes(); // Encrypt the cleartext byte[] ciphertext = pbeCipher.doFinal(cleartext);
SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); 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(Base64.decode(property)));
/** * Method to compute masked password based on class attributes. * * @return masked password prefixed with {link @PicketBoxSecurityVault.PASS_MASK_PREFIX}. * @throws Exception */ private String computeMaskedPassword() throws Exception { // Create the PBE secret key SecretKeyFactory factory = SecretKeyFactory.getInstance(VAULT_ENC_ALGORITHM); char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray(); PBEParameterSpec cipherSpec = new PBEParameterSpec(salt.getBytes(CHARSET), iterationCount); PBEKeySpec keySpec = new PBEKeySpec(password); SecretKey cipherKey = factory.generateSecret(keySpec); String maskedPass = PBEUtils.encode64(keystorePassword.getBytes(CHARSET), VAULT_ENC_ALGORITHM, cipherKey, cipherSpec); return PicketBoxSecurityVault.PASS_MASK_PREFIX + maskedPass; }
/** * Initializes a {@link Cipher} object with the given PBE parameters. * * @param algorithm the algorithm * @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); } }
private static Cipher getCipher(final String algorithm, final char[] initialKeyMaterial, final int iterationCount, final byte[] salt, final int mode) throws InvalidKeySpecException { try { // Create the factories first to fail fast final String pbeName = MaskedPassword.getPBEName(algorithm); Assert.assertNotNull(pbeName); final SecretKeyFactory factory = SecretKeyFactory.getInstance(pbeName); final Cipher cipher = Cipher.getInstance(pbeName); // Create the PBE secret key final PBEParameterSpec cipherSpec = new PBEParameterSpec(salt, iterationCount); final PBEKeySpec keySpec = new PBEKeySpec(initialKeyMaterial); final SecretKey cipherKey = factory.generateSecret(keySpec); cipher.init(mode, cipherKey, cipherSpec); return cipher; } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) { throw new InvalidKeySpecException(e); } }
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"); } 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; }
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFac = SecretKeyFactory.getInstance(MYPBEALG); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
public DesEncrypter(String passPhrase, byte[] newSalt) { try { if (newSalt != null) { salt = newSalt; } // Create the key KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); ecipher = Cipher.getInstance(key.getAlgorithm()); dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); // Create the ciphers ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException e) { } }
throw new GeneralSecurityException(JGitText.get().encryptionOnlyPBE); PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray(), SALT, ITERATIONS, KEY_SIZE); secretKey = SecretKeyFactory.getInstance(algo).generateSecret(keySpec); paramSpec = new PBEParameterSpec(SALT, ITERATIONS, paramIV); } else { paramSpec = new PBEParameterSpec(SALT, ITERATIONS);
public void init(String password, String algorithm, byte[] salt, int interationCount) throws NoSuchAlgorithmException, InvalidKeySpecException { this.algorithm = algorithm; this.salt = salt; this.iterationCount = interationCount; PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), this.salt, this.iterationCount, 32); SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm); this.key = skf.generateSecret(keyspec); this.algParamSpec = new PBEParameterSpec(this.salt, this.iterationCount); }
/** * Constructor for DefaultCryptor. */ public DefaultCryptor() throws NoSuchAlgorithmException,InvalidKeySpecException { // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt,count); pbeKeySpec = new PBEKeySpec("saagar".toCharArray()); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); pbeKey = keyFac.generateSecret(pbeKeySpec); }
/** * Constructor for DefaultCryptor. */ public DefaultCryptor() throws NoSuchAlgorithmException, InvalidKeySpecException { // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec("saagar".toCharArray()); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); pbeKey = keyFac.generateSecret(pbeKeySpec); }
/** * Constructor for DefaultCryptor. */ public DefaultCryptor() throws NoSuchAlgorithmException,InvalidKeySpecException { // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt,count); pbeKeySpec = new PBEKeySpec("saagar".toCharArray()); keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); pbeKey = keyFac.generateSecret(pbeKeySpec); }
public void init(String password, String algorithm, byte[] salt, int interationCount) throws NoSuchAlgorithmException, InvalidKeySpecException { this.algorithm = algorithm; this.salt = salt; this.iterationCount = interationCount; PBEKeySpec keyspec = new PBEKeySpec(password.toCharArray(), this.salt, this.iterationCount, 32); SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm); this.key = skf.generateSecret(keyspec); this.algParamSpec = new PBEParameterSpec(this.salt, this.iterationCount); }
private static byte[] transformWithPassword(byte[] bytes, byte[] iv, String password, int mode) throws Exception { // generate the key PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBE_ALGORITHM); SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(iv, PBE_ITERATIONS); // encrypt the input Cipher keyCipher = Cipher.getInstance(PBE_ALGORITHM); keyCipher.init(mode, pbeKey, pbeParamSpec); return keyCipher.doFinal(bytes); }
private static byte[] transformWithPassword(byte[] bytes, byte[] iv, String password, int mode) throws Exception { // generate the key PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBE_ALGORITHM); SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(iv, PBE_ITERATIONS); // encrypt the input Cipher keyCipher = Cipher.getInstance(PBE_ALGORITHM); keyCipher.init(mode, pbeKey, pbeParamSpec); return keyCipher.doFinal(bytes); }