List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:club.jmint.crossing.specs.Security.java
public static String desDecrypt(String data, String key) throws CrossException { String ret = null;//from w w w . j av a 2 s .c o m try { DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, securekey); ret = new String(cipher.doFinal(Base64.decodeBase64(data))); } catch (Exception e) { CrossLog.printStackTrace(e); throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(), ErrorCode.COMMON_ERR_DECRYPTION.getInfo()); } return ret; }
From source file:com.liusoft.dlog4j.upgrade.StringUtils.java
/** * /*from w w w . j ava 2 s. c o m*/ * @param src ?? * @param key 8? * @return ?? * @throws Exception */ public static byte[] decrypt(byte[] src, byte[] key) throws Exception { // DES???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec?? // SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.DECRYPT_MODE, securekey, sr); // ?? // ?? return cipher.doFinal(src); }
From source file:org.noroomattheinn.utils.PWUtils.java
public byte[] getEncryptedPassword(String password, byte[] salt) { // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST // specifically names SHA-1 as an acceptable hashing algorithm for PBKDF2 String algorithm = "PBKDF2WithHmacSHA1"; // SHA-1 generates 160 bit hashes, so that's what makes sense here int derivedKeyLength = 160; // Pick an iteration count that works for you. The NIST recommends at // least 1,000 iterations: // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf // iOS 4.x reportedly uses 10,000: // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/ int iterations = 20000; KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength); try {//from www .j a va 2 s.c o m SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm); return f.generateSecret(spec).getEncoded(); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { Logger.getLogger(PWUtils.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:com.example.license.DESUtil.java
public static String encryptBase64(String data, String key) throws Exception { DESKeySpec desKey = new DESKeySpec(key.getBytes()); // ?DESKeySpec?? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey securekey = keyFactory.generateSecret(desKey); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, securekey); byte[] results = cipher.doFinal(data.getBytes("UTF-8")); // Encrypt/* w ww .j a va 2 s . c o m*/ // byte[] unencryptedByteArray = data.getBytes("UTF8"); // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray); // Encode bytes to base64 to get a string byte[] encodedBytes = Base64.encodeBase64(results); return new String(encodedBytes); }
From source file:ch.rgw.tools.PasswordEncryptionService.java
public byte[] getEncryptedPassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST // specifically names SHA-1 as an acceptable hashing algorithm for PBKDF2 String algorithm = "PBKDF2WithHmacSHA1"; // SHA-1 generates 160 bit hashes, so that's what makes sense here int derivedKeyLength = 160; // Pick an iteration count that works for you. The NIST recommends at // least 1,000 iterations: // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf // iOS 4.x reportedly uses 10,000: // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/ int iterations = 20000; KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength); SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm); return f.generateSecret(spec).getEncoded(); }
From source file:com.jwt.security.auth.cryptographics.Crypto.java
private SecretKey generateKey(String salt, String passphrase) throws InvalidKeySpecException { try {/* ww w .j a v a 2 s. co m*/ SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), cryptoProps.getIterationCount(), cryptoProps.getKeySize()); SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); return key; } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw fail(e); } }
From source file:pl.kotcrab.crypto.CryptoUtils.java
/** Generates AES key from provided password and salt. Used algorithm is PBKDF2WithHmacSHA1. * @param password password in char array, using {@link CryptoUtils#fillZeros(char[])} is recommend after generating key * @param salt salt for this key//from w w w. j a v a 2s. c o m * @return SecretKeySpec */ public static SecretKeySpec getAESKeyFromPassword(char[] password, byte[] salt) { try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(password, salt, 65536, 256); return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); } catch (GeneralSecurityException e) { e.printStackTrace(); } return null; }
From source file:org.datacleaner.util.convert.EncodedStringConverter.java
@Override public String toString(String password) { if (password == null) { return null; }/* w w w .j a v a2 s . c o m*/ try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORHITM); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(_secret)); Cipher pbeCipher = Cipher.getInstance(ALGORHITM); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(_salt, 20)); byte[] bytes = pbeCipher.doFinal(password.getBytes()); bytes = Base64.encodeBase64(bytes, false); return new String(bytes, "UTF-8"); } catch (Exception e) { throw new IllegalStateException("Unable to encode password", e); } }
From source file:com.rootcloud.ejb.RootCloudBean.java
public String encryptThreeDESECB(String src, String key) throws Exception { DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); javax.crypto.SecretKey securekey = keyFactory.generateSecret(dks); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(1, securekey);// ww w.j ava2 s.com byte b[] = cipher.doFinal(src.getBytes()); Encoder ec = java.util.Base64.getEncoder(); return ec.encodeToString(b); }
From source file:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java
/** * Encodes a byte array and return the encoded data as a byte array. * * @param source Data to be encoded/*w ww. ja v a2 s. c o m*/ * @return ?byte.source, null * @throws org.apache.commons.codec.EncoderException thrown if the Encoder encounters a failure condition during the encoding process. */ @Override public byte[] encode(byte[] source) throws EncoderException { if (source == null) { return null; } try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_NAME); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameters); return cipher.doFinal(source); } catch (Exception e) { LOG.error(":" + key + ":" + source, e); throw new EncoderException(":" + key + ":" + source, e); } }