List of usage examples for javax.crypto.spec PBEKeySpec PBEKeySpec
public PBEKeySpec(char[] password, byte[] salt, int iterationCount)
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x73, 0x2f, 0x2d, 0x33, (byte) 0xc8, 0x01, 0x73, 0x2b, 0x72, 0x06, 0x75, 0x6c, (byte) 0xbd, 0x44, (byte) 0xf9, (byte) 0xc1, (byte) 0xc1, 0x03, (byte) 0xdd, (byte) 0xd9, 0x7c, 0x7c, (byte) 0xbe, (byte) 0x8e }; byte[] ivBytes = new byte[] { (byte) 0xb0, 0x7b, (byte) 0xf5, 0x22, (byte) 0xc8, (byte) 0xd6, 0x08, (byte) 0xb8 }; // encrypt the data using precalculated keys Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC"); cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DESede"), new IvParameterSpec(ivBytes)); byte[] out = cEnc.doFinal(input); // decrypt the data using PBE char[] password = "password".toCharArray(); byte[] salt = new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae }; int iterationCount = 2048; PBEKeySpec pbeSpec = new PBEKeySpec(password, salt, iterationCount); SecretKeyFactory keyFact = SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTripleDES"); Cipher cDec = Cipher.getInstance("PBEWithSHAAnd3KeyTripleDES"); Key sKey = keyFact.generateSecret(pbeSpec); cDec.init(Cipher.DECRYPT_MODE, sKey); System.out.println("cipher : " + new String(out)); System.out.println("gen key: " + new String(sKey.getEncoded())); System.out.println("gen iv : " + new String(cDec.getIV())); System.out.println("plain : " + new String(cDec.doFinal(out))); }
From source file:Main.java
private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt, int iterations) throws GeneralSecurityException { PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, iterations); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); return skf.generateSecret(keyspec); }
From source file:Main.java
private static int generateIterationCount(String passphrase, byte[] salt) { int TARGET_ITERATION_TIME = 50; //ms int MINIMUM_ITERATION_COUNT = 100; //default for low-end devices int BENCHMARK_ITERATION_COUNT = 10000; //baseline starting iteration count try {/*from w ww.j a v a 2 s. co m*/ PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, BENCHMARK_ITERATION_COUNT); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); long startTime = System.currentTimeMillis(); skf.generateSecret(keyspec); long finishTime = System.currentTimeMillis(); int scaledIterationTarget = (int) (((double) BENCHMARK_ITERATION_COUNT / (double) (finishTime - startTime)) * TARGET_ITERATION_TIME); if (scaledIterationTarget < MINIMUM_ITERATION_COUNT) return MINIMUM_ITERATION_COUNT; else return scaledIterationTarget; } catch (NoSuchAlgorithmException e) { Log.w("MasterSecretUtil", e); return MINIMUM_ITERATION_COUNT; } catch (InvalidKeySpecException e) { Log.w("MasterSecretUtil", e); return MINIMUM_ITERATION_COUNT; } }
From source file:Main.java
private static SecretKey getKeyFromPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException { PBEKeySpec keyspec = new PBEKeySpec(passphrase.toCharArray(), salt, 100); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWITHSHA1AND128BITAES-CBC-BC"); return skf.generateSecret(keyspec); }
From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java
public static String encrypt(String data, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); final Random r = new SecureRandom(); byte[] salt = new byte[SALT_SIZE]; r.nextBytes(salt);/*from www. j ava 2 s . c o m*/ SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC"); cipher.init(Cipher.ENCRYPT_MODE, fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE))); byte[] encVal = cipher.doFinal(data.getBytes()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); // writing encrypted data along with the salt in the format readable by // open ssl api bos.write("Salted__".getBytes()); bos.write(salt); bos.write(encVal); String encryptedValue = new String(Base64.encode(bos.toByteArray())); bos.close(); return encryptedValue; }
From source file:DesEncrypter.java
DesEncrypter(String passPhrase) throws Exception { int iterationCount = 2; 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()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); }
From source file:com.tesora.dve.common.PECryptoUtils.java
private static Cipher createCrypter(int mode) throws Exception { // Create the key KeySpec keySpec = new PBEKeySpec(settings.getPassword().toCharArray(), settings.getSalt(), settings.getIterations());/* www . jav a2s .c om*/ SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(mode, key, new PBEParameterSpec(settings.getSalt(), settings.getIterations())); return cipher; }
From source file:passworddecoder.DesEncrypter.java
DesEncrypter(String passPhrase) { KeySpec keySpec;/* w w w .j av a 2s . c om*/ try { keySpec = new PBEKeySpec(passPhrase.toCharArray(), this.salt, this.iterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); this.dcipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(this.salt, this.iterationCount); this.dcipher.init(2, key, paramSpec); } catch (InvalidAlgorithmParameterException e) { } catch (InvalidKeySpecException e) { } catch (NoSuchPaddingException e) { } catch (NoSuchAlgorithmException e) { } catch (InvalidKeyException e) { } }
From source file:com.aerohive.nms.engine.admin.task.licensemgr.common.AerohiveEncryptTool.java
public AerohiveEncryptTool(String arg_Key) { if (arg_Key != null && !arg_Key.trim().equals("")) m_Str_Key = arg_Key; try {/* w w w . j a v a 2s. c o m*/ // Create the key KeySpec keySpec = new PBEKeySpec(m_Str_Key.toCharArray(), m_Byte_Salt, m_Int_IterationCount); SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); m_Cipher_Ecipher = Cipher.getInstance(key.getAlgorithm()); m_Cipher_Dcipher = Cipher.getInstance(key.getAlgorithm()); // Prepare the parameter to the ciphers AlgorithmParameterSpec paramSpec = new PBEParameterSpec(m_Byte_Salt, m_Int_IterationCount); // Create the ciphers m_Cipher_Ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); m_Cipher_Dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (Exception e) { //DebugUtil.commonDebugWarn(e.getMessage()); } }
From source file:DesEncrypter.java
private DesEncrypter(String passPhrase) { try {/*from w w w . j a va 2 s. c o m*/ // 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 (java.security.InvalidAlgorithmParameterException e) { } catch (java.security.spec.InvalidKeySpecException e) { } catch (javax.crypto.NoSuchPaddingException e) { } catch (java.security.NoSuchAlgorithmException e) { } catch (java.security.InvalidKeyException e) { } }