List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:egovframework.com.ext.jfile.security.service.CipherServiceImpl.java
/** * ?./* www . j av a2s .c o m*/ * @param keyAlgorithm . * @param algorithm . * @param keyData ??. * @return Key . * @throws NoSuchAlgorithmException ? ? . * @throws InvalidKeyException ? key ? * @throws InvalidKeySpecException ? keySpec ? */ private static Key generateKey(String keyAlgorithm, String algorithm, byte[] keyData) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { if (keyAlgorithm == null || "".equals(keyAlgorithm)) throw new NoSuchAlgorithmException("algorithm is nessary"); String upper = keyAlgorithm.toUpperCase(); if ("DES".equals(upper)) { KeySpec keySpec = new DESKeySpec(keyData); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); return secretKey; } else if (upper.indexOf("DESEDE") > -1 || upper.indexOf("TRIPLEDES") > -1) { KeySpec keySpec = new DESedeKeySpec(keyData); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(keyAlgorithm); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); return secretKey; } else { SecretKeySpec keySpec = new SecretKeySpec(keyData, keyAlgorithm); return keySpec; } }
From source file:org.starnub.utilities.crypto.PasswordHash.java
/** * using PBKDF2 from Sun, an alternative is https://github.com/wg/scrypt * cf. http://www.unlimitednovelty.com/2012/03/dont-use-bcrypt.html * @param password String password to be salted * @param salt Byte[] of the salt//from www .j a va 2 s . c o m * @return String of the hashed password * @throws Exception */ private String hash(String password, byte[] salt) throws Exception { if (password == null || password.length() == 0) throw new IllegalArgumentException("Empty passwords are not supported."); SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); SecretKey key = f.generateSecret(new PBEKeySpec(password.toCharArray(), salt, iterations, desiredKeyLen)); return Base64.encodeBase64String(key.getEncoded()); }
From source file:com.invariantproperties.sandbox.springentitylistener.service.EncryptorBean.java
/** * Constructor creates secret key. In production we may want to avoid * keeping the secret key hanging around in memory for very long. */// w ww . j a v a2 s. c o m public EncryptorBean() { try { // create the PBE key KeySpec spec = new PBEKeySpec(PASSWORD, Base64.decode(SALT), 10000, 128); key = SecretKeyFactory.getInstance(PBE_ALGORITHM).generateSecret(spec); } catch (SecurityException ex) { // handle appropriately... System.out.println("encryptor bean ctor exception: " + ex.getMessage()); } catch (NoSuchAlgorithmException ex) { // handle appropriately... System.out.println("encryptor bean ctor exception: " + ex.getMessage()); } catch (InvalidKeySpecException ex) { // handle appropriately... System.out.println("encryptor bean ctor exception: " + ex.getMessage()); } }
From source file:net.bioclipse.encryption.Encrypter.java
public String decrypt(String encrypted) { SecretKeyFactory keyFac;/* w w w .j a v a2 s . co m*/ SecretKey pbeKey; Cipher pbeCipher; try { keyFac = SecretKeyFactory.getInstance(METHOD); pbeKey = keyFac.generateSecret(pbeKeySpec); pbeCipher = Cipher.getInstance(METHOD); pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); return new String(pbeCipher.doFinal(Base64.decodeBase64(encrypted.getBytes()))); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (InvalidKeySpecException e) { throw new IllegalStateException(e); } catch (NoSuchPaddingException e) { throw new IllegalStateException(e); } catch (InvalidKeyException e) { throw new IllegalStateException(e); } catch (InvalidAlgorithmParameterException e) { throw new IllegalStateException(e); } catch (IllegalBlockSizeException e) { throw new IllegalStateException(e); } catch (BadPaddingException e) { throw new IllegalStateException(e); } }
From source file:org.apache.xml.security.test.encryption.EncryptContentTest.java
public void setUp() throws Exception { org.apache.xml.security.Init.init(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*w w w. j a va 2s . c om*/ db = dbf.newDocumentBuilder(); byte[] bits192 = "abcdefghijklmnopqrstuvwx".getBytes(); DESedeKeySpec keySpec = new DESedeKeySpec(bits192); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); secretKey = keyFactory.generateSecret(keySpec); TransformerFactory tf = TransformerFactory.newInstance(); tf.newTransformer(); // Determine if we have ISO 10126 Padding - needed for Bulk AES or // 3DES encryption haveISOPadding = false; String algorithmId = JCEMapper .translateURItoJCEID(org.apache.xml.security.utils.EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128); if (algorithmId != null) { try { if (Cipher.getInstance(algorithmId) != null) haveISOPadding = true; } catch (NoSuchAlgorithmException nsae) { } catch (NoSuchPaddingException nspe) { } } }
From source file:br.com.vizzatech.cryptocipher.CryptoXCipher.java
/** * Gera chave e os parametros para um determinado algoritmo. * //w w w .j a v a 2 s . c om * @param algoritmo * @param sal * @return {@link SecretKey} * @throws NoSuchAlgorithmException * Algorismo no existente * @throws InvalidKeySpecException * @throws InvalidParameterSpecException */ private SecretKey getKey(String algoritmo, String sal) throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidParameterSpecException { KeySpec keySpec; if (algoritmo.indexOf("PBE") != -1) { byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x34, (byte) 0xE3, (byte) 0x03 }; int iteracoes = 16; keySpec = new PBEKeySpec(sal.toCharArray(), salt, iteracoes); paramSpec = new PBEParameterSpec(salt, iteracoes); return SecretKeyFactory.getInstance(algoritmo).generateSecret(keySpec); } keySpec = new SecretKeySpec(sal.getBytes(charset), algoritmo); return (SecretKey) keySpec; }
From source file:com.networknt.light.util.HashUtil.java
public static String generateStorngPasswordHash(String password) throws NoSuchAlgorithmException, InvalidKeySpecException { int iterations = 1000; char[] chars = password.toCharArray(); byte[] salt = getSalt().getBytes(); PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 64 * 8); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] hash = skf.generateSecret(spec).getEncoded(); return iterations + ":" + toHex(salt) + ":" + toHex(hash); }
From source file:com.glaf.core.security.SecurityUtils.java
/** * DES/* w w w.j av a 2s.co m*/ * * @param data * * @param key * ???8? * @return ? */ public static String encode(String key, String data) { if (data == null) { return null; } try { DESKeySpec dks = new DESKeySpec(key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // key??8? Key secretKey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance(ALGORITHM_DES); IvParameterSpec iv = new IvParameterSpec("12345678".getBytes()); AlgorithmParameterSpec paramSpec = iv; cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); byte[] bytes = cipher.doFinal(data.getBytes()); return byte2hex(bytes); } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:com.cyberninjas.xerobillableexpenses.util.Settings.java
public Settings() { try {//from w ww . ja va 2s . com String parentClass = new Exception().getStackTrace()[1].getClassName(); this.prefs = Preferences.userNodeForPackage(Class.forName(parentClass)); Random r = new SecureRandom(); //Set IV this.iv = prefs.getByteArray("DRUGS", null); //Pick Random PWD byte[] b = new byte[128]; r.nextBytes(b); MessageDigest sha = MessageDigest.getInstance("SHA-1"); sha.update(b); String sHash = new String(Base64.encodeBase64(sha.digest())); String password = prefs.get("LAYOUT", sHash); if (password.equals(sHash)) prefs.put("LAYOUT", sHash); //Keep 'em Guessing r.nextBytes(b); sha.update(b); prefs.put("PASSWORD", new String(Base64.encodeBase64(sha.digest()))); //Set Random Salt byte[] tSalt = new byte[8]; r.nextBytes(tSalt); byte[] salt = prefs.getByteArray("HIMALAYAN", tSalt); if (Arrays.equals(salt, tSalt)) prefs.putByteArray("HIMALAYAN", salt); /* Derive the key, given password and salt. */ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); SecretKey tmp = factory.generateSecret(spec); this.secret = new SecretKeySpec(tmp.getEncoded(), "AES"); cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidKeySpecException ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(RSAx509CertGen.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.aqnote.shared.cryptology.symmetric.DES.java
private static void generateCipher(String rawKey) { try {//w w w . ja va 2 s. co m DESKeySpec dks = new DESKeySpec(rawKey.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey deskey = keyFactory.generateSecret(dks); encodeCipher = Cipher.getInstance(ALGORITHM); encodeCipher.init(Cipher.ENCRYPT_MODE, deskey); decodeCipher = Cipher.getInstance(ALGORITHM); decodeCipher.init(Cipher.DECRYPT_MODE, deskey); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } }