use of javax.crypto.SecretKeyFactory in project UltimateAndroid by cymcsg.
the class DefaultAppLock method decryptPassword.
private String decryptPassword(String encryptedPwd) {
try {
DESKeySpec keySpec = new DESKeySpec(PASSWORD_ENC_SECRET.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
return new String(plainTextPwdBytes);
} catch (Exception e) {
}
return encryptedPwd;
}
use of javax.crypto.SecretKeyFactory in project newsrob by marianokamp.
the class EntryManager method getSecretKey.
private static SecretKey getSecretKey() throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException {
DESKeySpec keySpec = new DESKeySpec("EntryManager.class".getBytes("UTF8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
}
use of javax.crypto.SecretKeyFactory in project cloudstack by apache.
the class Vnc33Authentication 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 ByteBuffer encodePassword(ByteBuffer challenge, String password) {
if (challenge.length != 16)
throw new RuntimeException("Challenge must be exactly 16 bytes long.");
// 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.US_ASCII_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]);
}
try {
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);
ByteBuffer buf = new ByteBuffer(cipher.doFinal(challenge.data, challenge.offset, challenge.length));
return buf;
} catch (Exception e) {
throw new RuntimeException("Cannot encode password.", e);
}
}
use of javax.crypto.SecretKeyFactory in project jackrabbit-oak by apache.
the class PasswordUtil method generatePBKDF2.
@Nonnull
private static String generatePBKDF2(@Nonnull String pwd, @Nonnull String salt, @Nonnull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException {
// for example PBKDF2WithHmacSHA1
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
byte[] saltBytes = convertHexToBytes(salt);
KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength);
try {
Key key = factory.generateSecret(keyspec);
byte[] bytes = key.getEncoded();
return convertBytesToHex(bytes);
} catch (InvalidKeySpecException e) {
throw new NoSuchAlgorithmException(algorithm, e);
}
}
use of javax.crypto.SecretKeyFactory in project wildfly by wildfly.
the class VaultSession method computeMaskedPassword.
/**
* Method to compute masked password based on class attributes.
*
* @return masked password prefixed with {link @PicketBoxSecurityVault.PASS_MASK_PREFIX}.
* @throws Exception
*/
private String computeMaskedPassword() throws Exception {
// Create the PBE secret key
SecretKeyFactory factory = SecretKeyFactory.getInstance(VAULT_ENC_ALGORITHM);
char[] password = "somearbitrarycrazystringthatdoesnotmatter".toCharArray();
PBEParameterSpec cipherSpec = new PBEParameterSpec(salt.getBytes(), iterationCount);
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKey cipherKey = factory.generateSecret(keySpec);
String maskedPass = PBEUtils.encode64(keystorePassword.getBytes(), VAULT_ENC_ALGORITHM, cipherKey, cipherSpec);
return PicketBoxSecurityVault.PASS_MASK_PREFIX + maskedPass;
}
Aggregations