Search in sources :

Example 26 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project OpenAttestation by OpenAttestation.

the class Diagnostic method tryMacWithPassword.

private static void tryMacWithPassword(String algorithmName, String message, String password) {
    try {
        SecretKeySpec key = new SecretKeySpec(password.getBytes(), algorithmName);
        // a string like "HmacSHA256"
        Mac mac = Mac.getInstance(algorithmName, "BC");
        mac.init(key);
        byte[] digest = mac.doFinal(message.getBytes());
        System.out.println("Created " + algorithmName + " digest of length " + digest.length);
    } catch (NoSuchProviderException e) {
        System.err.println("Cannot use provider: BC: " + e.toString());
    } catch (NoSuchAlgorithmException e) {
        System.err.println("Cannot use algorithm: " + algorithmName + ": " + e.toString());
    } catch (InvalidKeyException e) {
        System.err.println("Cannot use key: " + password + ": " + e.toString());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 27 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.

the class AsymmetricMasterCipher method getDigestedBytes.

private byte[] getDigestedBytes(byte[] secretBytes, int iteration) {
    try {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secretBytes, "HmacSHA256"));
        return mac.doFinal(Conversions.intToByteArray(iteration));
    } catch (NoSuchAlgorithmException | java.security.InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) Mac(javax.crypto.Mac)

Example 28 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method generateMasterSecret.

public static MasterSecret generateMasterSecret(Context context, String passphrase) {
    try {
        byte[] encryptionSecret = generateEncryptionSecret();
        byte[] macSecret = generateMacSecret();
        byte[] masterSecret = Util.combine(encryptionSecret, macSecret);
        byte[] encryptionSalt = generateSalt();
        int iterations = generateIterationCount(passphrase, encryptionSalt);
        byte[] encryptedMasterSecret = encryptWithPassphrase(encryptionSalt, iterations, masterSecret, passphrase);
        byte[] macSalt = generateSalt();
        byte[] encryptedAndMacdMasterSecret = macWithPassphrase(macSalt, iterations, encryptedMasterSecret, passphrase);
        save(context, "encryption_salt", encryptionSalt);
        save(context, "mac_salt", macSalt);
        save(context, "passphrase_iterations", iterations);
        save(context, "master_secret", encryptedAndMacdMasterSecret);
        save(context, "passphrase_initialized", true);
        return new MasterSecret(new SecretKeySpec(encryptionSecret, "AES"), new SecretKeySpec(macSecret, "HmacSHA1"));
    } catch (GeneralSecurityException e) {
        Log.w("keyutil", e);
        return null;
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException)

Example 29 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project remusic by aa112901.

the class AESTools method encrpty.

public static String encrpty(String paramString) {
    MessageDigest messageDigest = null;
    try {
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    messageDigest.update(INPUT.getBytes());
    byte[] stringBytes = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder(stringBytes.length * 2);
    for (int i = 0; i < stringBytes.length; i++) {
        stringBuilder.append(CHARS[((stringBytes[i] & 0xF0) >>> 4)]);
        stringBuilder.append(CHARS[(stringBytes[i] & 0xF)]);
    }
    String str = stringBuilder.toString();
    SecretKeySpec localSecretKeySpec = new SecretKeySpec(str.substring(str.length() / 2).getBytes(), "AES");
    Cipher localCipher;
    try {
        localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        localCipher.init(1, localSecretKeySpec, new IvParameterSpec(IV.getBytes()));
        return URLEncoder.encode(new String(BytesHandler.getChars(localCipher.doFinal(paramString.getBytes()))), "utf-8");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest)

Example 30 with SecretKeySpec

use of javax.crypto.spec.SecretKeySpec in project remusic by aa112901.

the class Aes method decrypt.

private static byte[] decrypt(byte[] content, String password) {
    try {
        byte[] keyStr = getKey(password);
        SecretKeySpec key = new SecretKeySpec(keyStr, "AES");
        //algorithmStr
        Cipher cipher = Cipher.getInstance(algorithmStr);
        //   ΚΌ
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        //
        return result;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

SecretKeySpec (javax.crypto.spec.SecretKeySpec)432 Cipher (javax.crypto.Cipher)165 SecretKey (javax.crypto.SecretKey)128 Mac (javax.crypto.Mac)101 IvParameterSpec (javax.crypto.spec.IvParameterSpec)95 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)80 InvalidKeyException (java.security.InvalidKeyException)55 IOException (java.io.IOException)39 SecureRandom (java.security.SecureRandom)28 UnsupportedEncodingException (java.io.UnsupportedEncodingException)27 Key (java.security.Key)27 GeneralSecurityException (java.security.GeneralSecurityException)25 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)25 BadPaddingException (javax.crypto.BadPaddingException)23 MessageDigest (java.security.MessageDigest)22 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)21 Test (org.junit.Test)19 PrivateKey (java.security.PrivateKey)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 PublicKey (java.security.PublicKey)16