List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:org.alfresco.encryption.KeyStoreTests.java
protected Key generateSecretKey(String keyAlgorithm) { try {/* ww w .j a v a2 s.com*/ DESedeKeySpec keySpec = new DESedeKeySpec(generateKeyData()); SecretKeyFactory kf = SecretKeyFactory.getInstance(keyAlgorithm); SecretKey secretKey = kf.generateSecret(keySpec); return secretKey; } catch (Throwable e) { fail("Unexpected exception: " + e.getMessage()); return null; } }
From source file:tds.itemrenderer.security.Encryption.java
/** * initializes ciphers and adds jce provider if provided * * @throws TDS.Shared.Security.TDSEncryptionException *//*from w w w . j a v a 2 s . c o m*/ @PostConstruct protected void init() { if (encryptionKey == null || StringUtils.isBlank(encryptionKey) || encryptionKey.length() < MINIMUM_KEY_LENGTH) { throw new TDSEncryptionException( String.format("Number of characters for key must be greater than %s", MINIMUM_KEY_LENGTH)); } try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBE_KEY_ALGORITHM); KeySpec keySpec = new PBEKeySpec(encryptionKey.toCharArray(), PBE_SALT, PBE_NUM_ITERATIONS, PBE_KEY_LENGTH); SecretKey secretKeyTemp; secretKeyTemp = secretKeyFactory.generateSecret(keySpec); secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), CIPHER_ALGORITHM); encryptCipher = Cipher.getInstance(TRANSFORMATION); decryptCipher = Cipher.getInstance(TRANSFORMATION); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); } catch (NoSuchAlgorithmException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Algorithm is not available"); } catch (InvalidKeySpecException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key specification is not valid"); } catch (NoSuchPaddingException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Padding is not valid"); } catch (InvalidKeyException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key is not valid"); } }
From source file:org.apache.spark.network.crypto.AuthEngine.java
private SecretKeySpec generateKey(String kdf, int iterations, byte[] salt, int keyLength) throws GeneralSecurityException { SecretKeyFactory factory = SecretKeyFactory.getInstance(kdf); PBEKeySpec spec = new PBEKeySpec(secret, salt, iterations, keyLength); long start = System.nanoTime(); SecretKey key = factory.generateSecret(spec); long end = System.nanoTime(); LOG.debug("Generated key with {} iterations in {} us.", conf.keyFactoryIterations(), (end - start) / 1000); return new SecretKeySpec(key.getEncoded(), conf.keyAlgorithm()); }
From source file:ai.serotonin.backup.Base.java
SecretKey createSecretKey(final byte[] salt) throws Exception { final String password = getArchivePassword(); final SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); final KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 256); final SecretKey tmp = factory.generateSecret(spec); final SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); return secret; }
From source file:com.microsoft.aad.adal.CordovaAdalPlugin.java
private SecretKey createSecretKey(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC"); SecretKey tempkey = keyFactory .generateSecret(new PBEKeySpec(key.toCharArray(), "abcdedfdfd".getBytes("UTF-8"), 100, 256)); SecretKey secretKey = new SecretKeySpec(tempkey.getEncoded(), "AES"); return secretKey; }
From source file:org.yes.cart.web.support.util.cookie.impl.CookieTuplizerImpl.java
/** * Default Constructor./* www . j a v a2 s .c om*/ * * @param keyRingPassword key ring password to use. * @param chunkSize Base64 chunk size. * @param secretKeyFactoryName Secret Key Factory Name. * @param cipherName Cipher name. */ public CookieTuplizerImpl(final String keyRingPassword, final int chunkSize, final String secretKeyFactoryName, final String cipherName) { this.chunkSize = chunkSize; try { final DESKeySpec desKeySpec = new DESKeySpec(keyRingPassword.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretKeyFactoryName); secretKey = keyFactory.generateSecret(desKeySpec); // Create Cipher desCipher = Cipher.getInstance(cipherName); desCipher.init(Cipher.ENCRYPT_MODE, secretKey); // create uncipher desUnCipher = Cipher.getInstance(cipherName); desUnCipher.init(Cipher.DECRYPT_MODE, secretKey); } catch (Exception ike) { ShopCodeContext.getLog(this).error(ike.getMessage(), ike); throw new RuntimeException("Unable to load Cipher for CookieTuplizer", ike); } }
From source file:org.alfresco.repo.lotus.ws.impl.auth.LtpaAuthenticator.java
private byte[] decrypt(byte[] token, byte[] key, String algorithm) throws Exception { SecretKey sKey = null;//w ww .j av a 2 s . com if (algorithm.indexOf("AES") != -1) { sKey = new SecretKeySpec(key, 0, 16, "AES"); } else { DESedeKeySpec kSpec = new DESedeKeySpec(key); SecretKeyFactory kFact = SecretKeyFactory.getInstance("DESede"); sKey = kFact.generateSecret(kSpec); } Cipher cipher = Cipher.getInstance(algorithm); if (algorithm.indexOf("ECB") == -1) { if (algorithm.indexOf("AES") != -1) { IvParameterSpec ivs16 = generateIvParameterSpec(key, 16); cipher.init(Cipher.DECRYPT_MODE, sKey, ivs16); } else { IvParameterSpec ivs8 = generateIvParameterSpec(key, 8); cipher.init(Cipher.DECRYPT_MODE, sKey, ivs8); } } else { cipher.init(Cipher.DECRYPT_MODE, sKey); } return cipher.doFinal(token); }
From source file:bioLockJ.module.agent.MailAgent.java
private String decrypt(final String property) { String decryptedPassword = null; try {// ww w . jav a 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:com.microsoft.aad.adal.example.userappwithbroker.MainActivity.java
/** * To call broker, you have to ensure the following: * 1) You have to call {@link AuthenticationSettings#INSTANCE#setUseBroker(boolean)} * and the supplied value has to be true * 2) You have to have to correct set of permissions. * If target API version is lower than 23: * i) You have to have GET_ACCOUNTS, USE_CREDENTIAL, MANAGE_ACCOUNTS declared * in manifest.// ww w .j a v a 2 s .c o m * If target API version is 23: * i) USE_CREDENTIAL and MANAGE_ACCOUNTS is already deprecated. * ii) GET_ACCOUNTS permission is now at protection level "dangerous" calling app * is responsible for requesting it. * 3) If you're talking to the broker app without PRT support, you have to have an * WPJ account existed in broker(enroll with intune, or register with Azure * Authentication app). * 4) The two broker apps(Company Portal or Azure Authenticator) cannot go through * broker auth. */ private void setUpADALForCallingBroker() { // Set the calling app will talk to broker // Note: Starting from version 1.1.14, calling app has to explicitly call // AuthenticationSettings.Instance.setUserBroker(true) to call broker. // AuthenticationSettings.Instance.setSkipBroker(boolean) is already deprecated. AuthenticationSettings.INSTANCE.setUseBroker(true); // Provide secret key for token encryption. try { // For API version lower than 18, you have to provide the secret key. The secret key // needs to be 256 bits. You can use the following way to generate the secret key. And // use AuthenticationSettings.Instance.setSecretKey(secretKeyBytes) to supply us the key. // For API version 18 and above, we use android keystore to generate keypair, and persist // the keypair in AndroidKeyStore. Current investigation shows 1)Keystore may be locked with // a lock screen, if calling app has a lot of background activity, keystore cannot be // accessed when locked, we'll be unable to decrypt the cache items 2) AndroidKeystore could // be reset when gesture to unlock the device is changed. // We do recommend the calling app the supply us the key with the above two limitations. if (AuthenticationSettings.INSTANCE.getSecretKeyData() == null) { // use same key for tests SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC"); SecretKey tempkey = keyFactory.generateSecret( new PBEKeySpec("test".toCharArray(), "abcdedfdfd".getBytes("UTF-8"), 100, 256)); SecretKey secretKey = new SecretKeySpec(tempkey.getEncoded(), "AES"); AuthenticationSettings.INSTANCE.setSecretKey(secretKey.getEncoded()); } } catch (NoSuchAlgorithmException | InvalidKeySpecException | UnsupportedEncodingException ex) { showMessage("Fail to generate secret key:" + ex.getMessage()); } ApplicationInfo appInfo = getApplicationContext().getApplicationInfo(); Log.v(TAG, "App info:" + appInfo.uid + " package:" + appInfo.packageName); // If you're directly talking to ADFS server, you should set validateAuthority=false. SampleTelemetry telemetryDispatcher = new SampleTelemetry(); Telemetry.getInstance().registerDispatcher(telemetryDispatcher, true); }
From source file:om.edu.squ.squportal.portlet.dps.security.CryptoAES.java
/** * * method name : generateKey//from w w w .j a v a2 s . c o m * @param salt * @param passphrase * @return * CryptoAES * return type : SecretKey * * purpose : * * Date : Nov 15, 2017 7:30:08 PM */ private SecretKey generateKey(String salt, String passphrase) { SecretKey secretKey = null; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize); SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); secretKey = key; } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { logger.error(":: Crypto Error :: Error in key generation : {}", e.getMessage()); } return secretKey; }