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 android_frameworks_base by ResurrectionRemix.
the class BackupManagerService method buildCharArrayKey.
private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
return keyFactory.generateSecret(ks);
} catch (InvalidKeySpecException e) {
Slog.e(TAG, "Invalid key spec for PBKDF2!");
} catch (NoSuchAlgorithmException e) {
Slog.e(TAG, "PBKDF2 unavailable!");
}
return null;
}
use of java.security.spec.KeySpec in project cloudstack by apache.
the class RSAHelper method readKey.
private static RSAPublicKey readKey(String key) throws Exception {
byte[] encKey = Base64.decodeBase64(key.split(" ")[1]);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(encKey));
byte[] header = readElement(dis);
String pubKeyFormat = new String(header);
if (!pubKeyFormat.equals("ssh-rsa"))
throw new RuntimeException("Unsupported format");
byte[] publicExponent = readElement(dis);
byte[] modulus = readElement(dis);
KeySpec spec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME);
RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(spec);
return pubKey;
}
use of java.security.spec.KeySpec in project cloudstack by apache.
the class VncClient method encodePassword.
/**
* Encode password using DES encryption with given challenge.
*
* @param challenge
* a random set of bytes.
* @param password
* a password
* @return DES hash of password and challenge
*/
public byte[] encodePassword(byte[] challenge, String password) throws Exception {
// VNC password consist of up to eight ASCII characters.
// Padding
byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET);
System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8));
// Flip bytes (reverse bits) in key
for (int i = 0; i < key.length; i++) {
key[i] = flipByte(key[i]);
}
KeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] response = cipher.doFinal(challenge);
return response;
}
Aggregations