List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
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); SecretKeyFactory keyFact = SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTripleDES", "BC"); Cipher cDec = Cipher.getInstance("PBEWithSHAAnd3KeyTripleDES", "BC"); Key sKey = keyFact.generateSecret(pbeSpec); cDec.init(Cipher.DECRYPT_MODE, sKey, new PBEParameterSpec(salt, iterationCount)); 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: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 w w w . j ava 2 s . com 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:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java
public static String decrypt(String encryptedData, String password) throws Exception { Security.addProvider(new BouncyCastleProvider()); byte[] encrypted = Base64.decode(encryptedData); Cipher cipher0 = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC"); // Openssl puts SALTED__ then the 8 byte salt at the start of the file. // We simply copy it out. byte[] salt = new byte[SALT_SIZE]; System.arraycopy(encrypted, SALT_SIZE, salt, 0, SALT_SIZE); SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC"); cipher0.init(Cipher.DECRYPT_MODE, fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE))); // Decrypt the rest of the byte array (after stripping off the salt) byte[] decValue = cipher0.doFinal(encrypted, SALT_STRIP_LENGTH, encrypted.length - SALT_STRIP_LENGTH); return new String(decValue); }
From source file:com.sirius.utils.encrypt.DESEncryptor.java
private Key getKey(Object key) throws Exception { if (key instanceof String) { DESKeySpec dks = new DESKeySpec(((String) key).getBytes()); SecretKeyFactory skf = SecretKeyFactory.getInstance(ALGORITHM, security_provider); return skf.generateSecret(dks); } else {/*from ww w. ja v a 2 s . c om*/ throw new EncryptException("Invilid key."); } }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
private static Key generateKeySpec(String key) throws NoSuchAlgorithmException, InvalidKeySpecException { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); return secret; //To change body of generated methods, choose Tools | Templates. }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
public static String hmacSha256(String key, String value) { try {//from w w w . ja va 2 s . co m //System.out.println(Base64.getEncoder().encodeToString(keyBytes)); // Get an hmac_sha256 key from the raw key bytes System.out.println("First HMAC on = " + value); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256"); // Get an hmac_sha256 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider()); mac.init(secret); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(value.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String return new String(hexBytes, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.nifi.processors.standard.util.PasswordBasedEncryptor.java
public PasswordBasedEncryptor(final String algorithm, final String providerName, final char[] password, KeyDerivationFunction kdf) {//from w ww . j a v a 2 s.co m super(); try { // initialize cipher this.cipher = Cipher.getInstance(algorithm, providerName); this.kdf = kdf; if (isOpenSSLKDF()) { this.saltSize = OPENSSL_EVP_SALT_SIZE; this.iterationsCount = OPENSSL_EVP_KDF_ITERATIONS; } else { int algorithmBlockSize = cipher.getBlockSize(); this.saltSize = (algorithmBlockSize > 0) ? algorithmBlockSize : DEFAULT_SALT_SIZE; } // initialize SecretKey from password final PBEKeySpec pbeKeySpec = new PBEKeySpec(password); final SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm, providerName); this.secretKey = factory.generateSecret(pbeKeySpec); } catch (Exception e) { throw new ProcessException(e); } }
From source file:edu.vt.middleware.crypt.CryptProvider.java
/** * <p>This finds a <code>SecretKeyFactory</code> using the known providers and * the supplied algorithm parameter.</p> * * @param algorithm <code>String</code> name * * @return <code>SecretKeyFactory</code> * * @throws CryptException if the algorithm is not available from any * provider or if the provider is not available in the environment *//*from w w w . j a va 2 s. co m*/ public static SecretKeyFactory getSecretKeyFactory(final String algorithm) throws CryptException { final Log logger = LogFactory.getLog(CryptProvider.class); SecretKeyFactory kf = null; for (int i = 0; i < providers.length; i++) { try { kf = SecretKeyFactory.getInstance(algorithm, providers[i]); } catch (NoSuchAlgorithmException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find algorithm " + algorithm + " in " + providers[i]); } } catch (NoSuchProviderException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find provider " + providers[i]); } } finally { if (kf != null) { break; } } } if (kf == null) { try { kf = SecretKeyFactory.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find algorithm " + algorithm); } throw new CryptException(e.getMessage()); } } return kf; }
From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java
public static boolean isValid(String plainText, String HMAC, String key) { try {/* w ww. j a v a 2 s .co m*/ System.out.println("HMAC on = " + plainText); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider()); char password[] = key.toCharArray(); byte salt[] = "salt".getBytes(); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); SecretKey tmp = factory.generateSecret(spec); SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "HmacSHA256"); // Get an hmac_sha1 Mac instance and initialize with the signing key Mac mac = Mac.getInstance("HmacSHA256", new BouncyCastleProvider()); mac.init(secret); // Compute the hmac on input data bytes byte[] rawHmac = mac.doFinal(plainText.getBytes()); // Convert raw bytes to Hex byte[] hexBytes = new Hex().encode(rawHmac); // Covert array of Hex bytes to a String String check = new String(hexBytes, "UTF-8"); System.out.println("Checking = " + check); return check.equals(HMAC); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.openintents.safe.CryptoHelper.java
/** * Initialize the class. Sets the encryption level for the instance * and generates the secret key factory. * * @param Strength/*from ww w . java 2s . c o m*/ */ private void initialize(int Strength) { switch (Strength) { case EncryptionMedium: algorithm = algorithmMedium; break; case EncryptionStrong: algorithm = algorithmStrong; break; } pbeParamSpec = new PBEParameterSpec(salt, count); try { keyFac = SecretKeyFactory.getInstance(algorithm, "BC"); } catch (NoSuchAlgorithmException e) { Log.e(TAG, "CryptoHelper(): " + e.toString()); } catch (NoSuchProviderException e) { Log.e(TAG, "CryptoHelper(): " + e.toString()); } }