use of java.security.spec.KeySpec in project ice by JBEI.
the class TokenHash method encrypt.
public String encrypt(String value, String salt) {
if (value == null || value.trim().isEmpty() || salt == null || salt.trim().isEmpty())
throw new NullPointerException("Cannot encrypt null value or salt");
KeySpec spec = new PBEKeySpec(value.toCharArray(), salt.getBytes(), PBKDF2_ITERATIONS, HASH_BYTE_SIZE);
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = keyFactory.generateSecret(spec).getEncoded();
return DatatypeConverter.printBase64Binary(hash);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
return null;
}
}
use of java.security.spec.KeySpec in project ice by JBEI.
the class AccountUtils method encryptNewUserPassword.
public static String encryptNewUserPassword(String password, String salt) {
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(salt))
throw new IllegalArgumentException("Password and salt cannot be empty");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 20000, 160);
try {
SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] hash = f.generateSecret(spec).getEncoded();
return bytesToHex(hash);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
Logger.error(e);
return null;
}
}
use of java.security.spec.KeySpec in project oxCore by GluuFederation.
the class StringEncrypter method encrypt.
/**
* Encrypt a string
*
* @param unencryptedString
* String to encrypt
* @return Encrypted string (using scheme and key specified at construction)
* @throws EncryptionException
*/
public String encrypt(final String unencryptedString, String encryptionKey) throws EncryptionException {
lock.lock();
try {
final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
KeySpec keySpec;
if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
keySpec = new DESedeKeySpec(keyAsBytes);
} else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
keySpec = new DESKeySpec(keyAsBytes);
} else {
throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
}
return encrypt(unencryptedString, keySpec);
} catch (final Exception e) {
throw new EncryptionException(e);
} finally {
lock.unlock();
}
}
use of java.security.spec.KeySpec in project inbot-utils by Inbot.
the class AESUtils method getKey.
private static SecretKey getKey(String salt, String password) {
try {
// https://tools.ietf.org/html/rfc2898
// sha1 with 1000 iterations and 256 bits is good enough here http://stackoverflow.com/questions/6126061/pbekeyspec-what-do-the-iterationcount-and-keylength-parameters-influence
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), 1000, 256);
SecretKey tmp = factory.generateSecret(spec);
return new SecretKeySpec(tmp.getEncoded(), "AES");
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new IllegalStateException("cannot create key: " + e.getMessage(), e);
}
}
Aggregations