List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:io.stallion.utils.Encrypter.java
private static SecretKeySpec makeKeySpec(String password, String salt) { byte[] saltBytes = new byte[0]; try {/*from ww w . j a v a 2 s . c om*/ saltBytes = Hex.decodeHex(salt.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), saltBytes, ITERATIONS, KEY_LENGTH); SecretKey secretKey; try { SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); secretKey = factory.generateSecret(keySpec); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("Not a valid encryption algorithm", e); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException("Not a valid secret key", e); } SecretKeySpec skeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); return skeySpec; }
From source file:com.thoughtworks.go.domain.AccessToken.java
static String digestToken(String originalToken, String salt) { try {/*from w w w . j av a2s . c om*/ ACCESS_TOKEN_LOGGER.debug( "Generating secret using algorithm: {} with spec: DEFAULT_ITERATIONS: {}, DESIRED_KEY_LENGTH: {}", KEY_ALGORITHM, DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH); SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM); SecretKey key = factory.generateSecret(new PBEKeySpec(originalToken.toCharArray(), salt.getBytes(), DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH)); return Hex.encodeHexString(key.getEncoded()); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new RuntimeException(e); } }
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Read a TripleDES secret key from the specified file * //from w ww.j a v a 2 s. com * @param keyFile key file to read * @return secret key appropriate for encryption/decryption with 3DES * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws InvalidKeySpecException */ public static SecretKey readKey(final File keyFile) throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException { // Read the raw bytes from the keyfile final DataInputStream in = new DataInputStream(new FileInputStream(keyFile)); final byte[] rawkey = new byte[(int) keyFile.length()]; in.readFully(rawkey); in.close(); // Convert the raw bytes to a secret key like this final DESedeKeySpec keyspec = new DESedeKeySpec(rawkey); final SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede"); final SecretKey key = keyfactory.generateSecret(keyspec); return key; }
From source file:fi.ilmoeuro.membertrack.util.Crypto.java
public static String hash(String candidate, String salt) { try {/*from w w w . ja va 2s. com*/ SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec ks = new PBEKeySpec(candidate.toCharArray(), salt.getBytes(StandardCharsets.US_ASCII), 1024, 128); SecretKey sk = skf.generateSecret(ks); Key k = new SecretKeySpec(sk.getEncoded(), "AES"); return Hex.encodeHexString(k.getEncoded()); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new RuntimeException("Error while hashing", ex); } }
From source file:JavaTron.JTP.java
/** * Encrypts a string/* w w w. ja va2 s.c o m*/ * @param property * @return An encrypted string */ public static String encrypt(String property) { String p = new String(); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); p = base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (Exception e) { e.printStackTrace(); } return p; }
From source file:JavaTron.JTP.java
/** * Decrypts an encrypted string//from w w w. ja v a 2s.co m * @param property * @return A plaintext string */ public static String decrypt(String property) { String p = new String(); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD)); Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20)); p = new String(pbeCipher.doFinal(base64Decode(property))); } catch (Exception e) { e.printStackTrace(); } return p; }
From source file:com.asquareb.kaaval.MachineKey.java
/** * Method to encrypt a string. Accepts the string to be encrypted and the * name of the file to store the key vale which can be used for decryption *//*from w ww. ja va 2 s. co m*/ public static String encrypt(String property, String app) throws IOException, KaavalException { InetAddress ip = null; String ipAddress = null; ObjectOutputStream os = null; NetworkInterface macAddress = null; byte[] macId = null; Cipher pbeCipher = null; Random rand = new Random(); rand.nextBytes(salt); try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password)); ip = InetAddress.getLocalHost(); ipAddress = ip.getHostAddress(); macAddress = NetworkInterface.getByInetAddress(ip); macId = macAddress.getHardwareAddress(); MachineKey mKey = new MachineKey(); mKey.api = ipAddress; mKey.macad = new String(macId); mKey.yek = key; mKey.tlas = salt; mKey.eti = rand.nextInt(1000); os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(app))); os.writeObject(mKey); os.close(); pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(salt, mKey.eti)); return base64Encode(pbeCipher.doFinal(property.getBytes())); } catch (IOException e) { throw new KaavalException(1, "Error in key file during encryption", e); } catch (Exception e) { throw new KaavalException(2, "Errors during encryption", e); } finally { if (os != null) os.close(); } }
From source file:org.jamwiki.utils.Encryption.java
/** * Create the encryption key value./*from w ww. jav a2 s . c o m*/ * * @return An encryption key value implementing the DES encryption algorithm. */ private static SecretKey createKey() throws GeneralSecurityException, UnsupportedEncodingException { byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8"); DESKeySpec spec = new DESKeySpec(bytes); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM); return keyFactory.generateSecret(spec); }
From source file:com.shenit.commons.codec.DesUtils.java
/** * /* w w w.j a va 2 s. c o m*/ * @param rawData * @param rawKey * @param mode */ private static byte[] crypt(byte[] rawData, KeySpec keySpec, int mode) { // ?DESKeySpec byte[] result = null; try { // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CODEC_DES); Key key = keyFactory.generateSecret(keySpec); if (key == null) { if (LOG.isWarnEnabled()) LOG.warn("[crypt] No key generated!"); return null; } // Cipher?? Cipher cipher = Cipher.getInstance(CODEC_DES); // DES???? cipher.init(mode, key, new SecureRandom()); // ?? // ?? result = cipher.doFinal(rawData); } catch (Exception ex) { LOG.warn("[crypt] crypt with exceptions", ex); } return result; }
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * DES bytKey8?// w w w .ja va 2 s. co m * * @param bytP * @param bytKey * @return * @throws Exception */ public static byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception { DESKeySpec dks = new DESKeySpec(bytKey); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(dks); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.ENCRYPT_MODE, sk); return cip.doFinal(bytP); }