List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:org.apache.xml.security.test.encryption.BobKeyResolver.java
/** * Method engineResolveSecretKey/*from ww w .j av a 2 s. co m*/ * * @param element * @param BaseURI * @param storage * * @throws KeyResolverException */ public javax.crypto.SecretKey engineLookupAndResolveSecretKey(Element element, String BaseURI, StorageResolver storage) throws KeyResolverException { if (engineCanResolve(element, BaseURI, storage)) { try { DESedeKeySpec keySpec = new DESedeKeySpec("abcdefghijklmnopqrstuvwx".getBytes("ASCII")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey key = keyFactory.generateSecret(keySpec); return key; } catch (Exception e) { throw new KeyResolverException("Something badly wrong in creation of bob's key"); } } return null; }
From source file:Crypt.java
private Crypt() { DESKeySpec dk;// w w w . java 2 s . c o m try { dk = new DESKeySpec(new Long(serialVersionUID).toString().getBytes()); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); secretKey = kf.generateSecret(dk); } catch (Exception e) { log.error("", e); } }
From source file:org.apache.ranger.plugin.util.PasswordUtils.java
private String encrypt() throws IOException { String ret = null;/*from w w w. j a va 2 s. co m*/ String strToEncrypt = null; if (password == null) { strToEncrypt = ""; } else { strToEncrypt = password.length() + LEN_SEPARATOR_STR + password; } try { Cipher engine = Cipher.getInstance(CRYPT_ALGO); PBEKeySpec keySpec = new PBEKeySpec(encryptKey); SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO); SecretKey key = skf.generateSecret(keySpec); engine.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT)); byte[] encryptedStr = engine.doFinal(strToEncrypt.getBytes()); ret = new String(Base64.encode(encryptedStr)); } catch (Throwable t) { LOG.error("Unable to encrypt password due to error", t); throw new IOException("Unable to encrypt password due to error", t); } return ret; }
From source file:org.apache.ranger.plugin.util.PasswordUtils.java
private String decrypt() throws IOException { String ret = null;/*w w w. j av a2 s .c om*/ try { byte[] decodedPassword = Base64.decode(password); Cipher engine = Cipher.getInstance(CRYPT_ALGO); PBEKeySpec keySpec = new PBEKeySpec(encryptKey); SecretKeyFactory skf = SecretKeyFactory.getInstance(CRYPT_ALGO); SecretKey key = skf.generateSecret(keySpec); engine.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, ITERATION_COUNT)); String decrypted = new String(engine.doFinal(decodedPassword)); int foundAt = decrypted.indexOf(LEN_SEPARATOR_STR); if (foundAt > -1) { if (decrypted.length() > foundAt) { ret = decrypted.substring(foundAt + 1); } else { ret = ""; } } else { ret = null; } } catch (Throwable t) { LOG.error("Unable to decrypt password due to error", t); throw new IOException("Unable to decrypt password due to error", t); } return ret; }
From source file:com.ubipass.middleware.web.action.LicenceMgtAction.java
private String getDate(String userName, String licenceKey) throws Exception { // DES????// ww w .j a v a2 s . c o m SecureRandom sr = new SecureRandom(); byte rawKeyData[] = (userName + "midware").getBytes(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(rawKeyData); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance("DES"); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, key, sr); // ?? licenceKey = new String(cipher.doFinal(Base64.decodeBase64(licenceKey.getBytes()))); String[] tmpStr = licenceKey.split("-"); if (tmpStr.length == 2) return tmpStr[1]; throw new InvalidLicenseException(); }
From source file:com.titilink.common.app.EncryptDecryptUtil.java
public void testDES() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException { ////w w w. j a va 2 s. c om DESKeySpec desKeySpec = new DESKeySpec("SECURITY".getBytes(Charset.forName("UTF-8"))); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); //?? Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); byte[] cipherData = cipher .doFinal("this is a security text from server".getBytes(Charset.forName("UTF-8"))); //? Cipher cipher1 = Cipher.getInstance("DES"); cipher1.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); byte[] plainData = cipher1.doFinal(cipherData); System.out.println(new String(plainData, Charset.forName("UTF-8"))); }
From source file:components.security.Password.java
/** * Hashes the password using pbkdf2 and the given salt. * * @param password the plain text password to hash. * @param salt the salt for the password * @return the hashed password//ww w. j a v a 2 s. c om */ public char[] hash(char[] password, byte[] salt) { checkNotNull(password); checkArgument(password.length != 0); checkNotNull(salt); checkArgument(salt.length != 0); try { SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = f.generateSecret(new PBEKeySpec(password, salt, iterations, desiredKeyLenght)); return Base64.encodeBase64String(key.getEncoded()).toCharArray(); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { Logger.error("Problem during password hashing", e); throw new IllegalStateException("Could not hash the password of the user.", e); } }
From source file:adminpassword.Encryption.java
public String encrypt(String word, String idKey) throws Exception { byte[] ivBytes; String password = idKey; //you can give whatever you want. This is for testing purpose SecureRandom random = new SecureRandom(); byte bytes[] = new byte[20]; random.nextBytes(bytes);/*from ww w . j a v a 2s. com*/ byte[] saltBytes = bytes; // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); //encrypting the word Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8")); //prepend salt and vi byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length]; System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length); System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length); System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length, encryptedTextBytes.length); return new Base64().encodeToString(buffer); }
From source file:org.zuinnote.flink.office.common.FlinkKeyStoreManager.java
/** * Sets the password in the currently openend keystore. Do not forget to store it afterwards * /*w w w . j a v a 2 s . c o m*/ * @param alias * @param password to store * @param passwordPassword password for encrypting password. You can use the same as the keystore password * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws KeyStoreException */ public void setPassword(String alias, String password, String passwordPassword) throws NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException { SecretKeyFactory skf = SecretKeyFactory.getInstance("PBE"); SecretKey pSecret = skf.generateSecret(new PBEKeySpec(password.toCharArray())); KeyStore.PasswordProtection kspp = new KeyStore.PasswordProtection(passwordPassword.toCharArray()); this.keystore.setEntry(alias, new KeyStore.SecretKeyEntry(pSecret), kspp); }
From source file:org.hawk.core.security.FileBasedCredentialsStore.java
private String decrypt(String property) throws GeneralSecurityException, IOException { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(encryptionKey)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(salt, 20)); return new String(pbeCipher.doFinal(base64Decode(property)), "UTF-8"); }