// DECODE YOUR BASE64 STRING // REBUILD KEY USING SecretKeySpec byte[] encodedKey = Base64.decode(stringKey, Base64.DEFAULT); SecretKey originalKey = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
public AesCipher(byte[] key, byte[] iv) { this.key = key; this.iv = iv; this.zeroIv = new IvParameterSpec(iv); this.keySpec = new SecretKeySpec(key, KEY_ALGORITHM); }
public AlgorithmParameterSpec getParameterSpec(byte[] iv) { return this == CBC ? new IvParameterSpec(iv) : new GCMParameterSpec(128, iv); }
/** * Set initialization vector for use with AES algorithms * @param iv the raw IV * @return this Builder */ public Builder iv(byte[] iv) { ivSpec = new IvParameterSpec(iv); return this; }
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; } private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; }
public static byte[] encrypt(byte[] data, byte[] encryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(encryptKey, KEY_ALGORITHM); return encrypt(data, zeroIv, key); }
public AlgorithmParameterSpec getParameterSpec(byte[] iv) { return this == CBC ? new IvParameterSpec(iv) : new GCMParameterSpec(128, iv); }
protected SecretKey getSecretKey(byte[] key, int keyLength) { if (keyLength == 16 || keyLength == 32 || keyLength == 24) { return new SecretKeySpec(key, 0, keyLength, "AES"); } return null; }
public static byte[] decrypt(byte[] data, byte[] decryptKey, byte[] iv) { IvParameterSpec zeroIv = new IvParameterSpec(iv); SecretKeySpec key = new SecretKeySpec(decryptKey, KEY_ALGORITHM); return decrypt(data, zeroIv, key); }
@Override public SecretKey parse(byte[] encoded) { return new SecretKeySpec(encoded, 0, encoded.length, ALGORITHM); } }
private javax.crypto.Cipher initNewCipher(int jcaCipherMode, byte[] key, byte[] iv, boolean streaming) throws CryptoException { javax.crypto.Cipher cipher = newCipherInstance(streaming); java.security.Key jdkKey = new SecretKeySpec(key, getAlgorithmName()); IvParameterSpec ivSpec = null; if (iv != null && iv.length > 0) { ivSpec = new IvParameterSpec(iv); } init(cipher, jcaCipherMode, jdkKey, ivSpec, getSecureRandom()); return cipher; } }
private Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; }
/** * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the * SHA-256 (256 hash bits) hash function and a {@link SecretKeySpec} created from the given byte * array and the SHA-256 algorithm. * * * @param key the key material of the secret key * @since 20.0 */ public static HashFunction hmacSha256(byte[] key) { return hmacSha256(new SecretKeySpec(checkNotNull(key), "HmacSHA256")); }
/** * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the * SHA-512 (512 hash bits) hash function and a {@link SecretKeySpec} created from the given byte * array and the SHA-512 algorithm. * * * @param key the key material of the secret key * @since 20.0 */ public static HashFunction hmacSha512(byte[] key) { return hmacSha512(new SecretKeySpec(checkNotNull(key), "HmacSHA512")); }
/** * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the * MD5 (128 hash bits) hash function and a {@link SecretKeySpec} created from the given byte array * and the MD5 algorithm. * * * @param key the key material of the secret key * @since 20.0 */ public static HashFunction hmacMd5(byte[] key) { return hmacMd5(new SecretKeySpec(checkNotNull(key), "HmacMD5")); }
/** * Returns a hash function implementing the Message Authentication Code (MAC) algorithm, using the * SHA-1 (160 hash bits) hash function and a {@link SecretKeySpec} created from the given byte * array and the SHA-1 algorithm. * * * @param key the key material of the secret key * @since 20.0 */ public static HashFunction hmacSha1(byte[] key) { return hmacSha1(new SecretKeySpec(checkNotNull(key), "HmacSHA1")); }
public byte[] hmac(byte[] key, byte[] bytes) throws InvalidKeyException { mac.init(new SecretKeySpec(key, mac.getAlgorithm())); return mac.doFinal(bytes); }
public TestEncryptionMaterialsProvider() { encryptionMaterials = new EncryptionMaterials(new SecretKeySpec(new byte[] {1, 2, 3}, "AES")); }
public byte[] hi(byte[] str, byte[] salt, int iterations) throws InvalidKeyException { mac.init(new SecretKeySpec(str, mac.getAlgorithm())); mac.update(salt); byte[] u1 = mac.doFinal(new byte[]{0, 0, 0, 1}); byte[] prev = u1; byte[] result = u1; for (int i = 2; i <= iterations; i++) { byte[] ui = hmac(str, prev); result = xor(result, ui); prev = ui; } return result; }
@Test(expected = IllegalArgumentException.class) public void testSecretKeySpecAllowsEmtyKeys() { new SecretKeySpec(new byte[] {}, "HmacMD5"); }