Search in sources :

Example 21 with SecretKey

use of javax.crypto.SecretKey in project Talon-for-Twitter by klinker24.

the class TwitterDMPicHelper method computeSignature.

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKey secretKey = null;
    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);
    byte[] text = baseString.getBytes();
    return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Example 22 with SecretKey

use of javax.crypto.SecretKey in project Talon-for-Twitter by klinker24.

the class TwitterMultipleImageHelper method computeSignature.

private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {
    SecretKey secretKey = null;
    byte[] keyBytes = keyString.getBytes();
    secretKey = new SecretKeySpec(keyBytes, "HmacSHA1");
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(secretKey);
    byte[] text = baseString.getBytes();
    return new String(BASE64Encoder.encode(mac.doFinal(text))).trim();
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(javax.crypto.Mac)

Example 23 with SecretKey

use of javax.crypto.SecretKey in project android-pbe by nelenkov.

the class Crypto method deriveKeyPkcs12.

public static SecretKey deriveKeyPkcs12(byte[] salt, String password) {
    try {
        long start = System.currentTimeMillis();
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PKCS12_DERIVATION_ALGORITHM);
        SecretKey result = keyFactory.generateSecret(keySpec);
        long elapsed = System.currentTimeMillis() - start;
        Log.d(TAG, String.format("PKCS#12 key derivation took %d [ms].", elapsed));
        return result;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 24 with SecretKey

use of javax.crypto.SecretKey in project android-pbe by nelenkov.

the class Crypto method deriveKeyPad.

// Illustration code only: don't use in production!
public static SecretKey deriveKeyPad(String password) {
    try {
        long start = System.currentTimeMillis();
        byte[] keyBytes = new byte[KEY_LENGTH / 8];
        // explicitly fill with zeros
        Arrays.fill(keyBytes, (byte) 0x0);
        // if password is shorter then key length, it will be zero-padded
        // to key length
        byte[] passwordBytes = password.getBytes("UTF-8");
        int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
        System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
        SecretKey result = new SecretKeySpec(keyBytes, "AES");
        long elapsed = System.currentTimeMillis() - start;
        Log.d(TAG, String.format("Padding key derivation took %d [ms].", elapsed));
        return result;
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 25 with SecretKey

use of javax.crypto.SecretKey in project android-pbe by nelenkov.

the class Crypto method deriveKeyPbkdf2.

public static SecretKey deriveKeyPbkdf2(byte[] salt, String password) {
    try {
        long start = System.currentTimeMillis();
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBKDF2_DERIVATION_ALGORITHM);
        byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
        Log.d(TAG, "key bytes: " + toHex(keyBytes));
        SecretKey result = new SecretKeySpec(keyBytes, "AES");
        long elapsed = System.currentTimeMillis() - start;
        Log.d(TAG, String.format("PBKDF2 key derivation took %d [ms].", elapsed));
        return result;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKey (javax.crypto.SecretKey)437 Cipher (javax.crypto.Cipher)160 SecretKeySpec (javax.crypto.spec.SecretKeySpec)127 KeyGenerator (javax.crypto.KeyGenerator)112 SecretKeyFactory (javax.crypto.SecretKeyFactory)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 SecureRandom (java.security.SecureRandom)58 InvalidKeyException (java.security.InvalidKeyException)57 PBEKeySpec (javax.crypto.spec.PBEKeySpec)53 IvParameterSpec (javax.crypto.spec.IvParameterSpec)42 IOException (java.io.IOException)41 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)34 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)32 KeyStore (java.security.KeyStore)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 Test (org.junit.Test)30 BadPaddingException (javax.crypto.BadPaddingException)29 PrivateKey (java.security.PrivateKey)28 Mac (javax.crypto.Mac)28 GeneralSecurityException (java.security.GeneralSecurityException)26