Search in sources :

Example 71 with KeySpec

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;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 72 with KeySpec

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;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 73 with KeySpec

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;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 74 with KeySpec

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;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DataInputStream(java.io.DataInputStream) KeyFactory(java.security.KeyFactory)

Example 75 with KeySpec

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;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

KeySpec (java.security.spec.KeySpec)76 PBEKeySpec (javax.crypto.spec.PBEKeySpec)27 SecretKeyFactory (javax.crypto.SecretKeyFactory)25 KeyFactory (java.security.KeyFactory)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)20 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)19 SecretKeySpec (javax.crypto.spec.SecretKeySpec)19 SecretKey (javax.crypto.SecretKey)17 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)14 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)12 PrivateKey (java.security.PrivateKey)11 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 DESKeySpec (javax.crypto.spec.DESKeySpec)9 IOException (java.io.IOException)8 BigInteger (java.math.BigInteger)7 NoSuchProviderException (java.security.NoSuchProviderException)7 PublicKey (java.security.PublicKey)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6