Example usage for javax.crypto SecretKey getAlgorithm

List of usage examples for javax.crypto SecretKey getAlgorithm

Introduction

In this page you can find the example usage for javax.crypto SecretKey getAlgorithm.

Prototype

public String getAlgorithm();

Source Link

Document

Returns the standard algorithm name for this key.

Usage

From source file:us.the.mac.android.jni.helpers.EncryptedStringTest.java

@Test
public void javaDecryptWithSecretKey() throws Exception {

    String contents = TestConstants.TEST_ENCRYPTED_RESOURCE;
    byte[] keyBytes = TestConstants.TEST_ENCRYPTION_KEY.getBytes();
    String[] parts = contents.split(":");

    byte[] decodedKeyBytes = Base64.decodeBase64(keyBytes);
    byte[] initVector = Base64.decodeBase64(parts[0].getBytes());
    byte[] decodedValueBytes = Base64.decodeBase64(parts[1].getBytes());

    SecretKey originalKey = new SecretKeySpec(decodedKeyBytes, 0, decodedKeyBytes.length, "AES");
    Cipher cipher = Cipher.getInstance(originalKey.getAlgorithm() + "/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, originalKey, new IvParameterSpec(initVector));
    byte[] decryptedBytes = cipher.doFinal(decodedValueBytes);

    assertEquals(TestConstants.TEST_DECRYPT, new String(decryptedBytes));

}

From source file:us.the.mac.android.jni.helpers.EncryptedStringTest.java

@Test
public void nativeDecryptWithSecretKey() throws Exception {

    String contents = TestConstants.TEST_ENCRYPTED_RESOURCE;
    String[] parts = contents.split(":");

    byte[] initVector = Base64.decodeBase64(parts[0].getBytes());
    byte[] decodedValueBytes = Base64.decodeBase64(parts[1].getBytes());

    SecretKey originalKey = createSecretKeySpec();
    Cipher cipher = Cipher.getInstance(originalKey.getAlgorithm() + "/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, originalKey, new IvParameterSpec(initVector));
    byte[] decryptedBytes = cipher.doFinal(decodedValueBytes);

    assertEquals(TestConstants.TEST_DECRYPT, new String(decryptedBytes));

}