Example usage for javax.crypto.spec SecretKeySpec SecretKeySpec

List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec SecretKeySpec SecretKeySpec.

Prototype

public SecretKeySpec(byte[] key, String algorithm) 

Source Link

Document

Constructs a secret key from the given byte array.

Usage

From source file:Main.java

private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(encrypted);
}

From source file:Main.java

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, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

From source file:Main.java

private static Key toKey(byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
    return secretKey;
}

From source file:Main.java

public static byte[] decrypt(byte[] key, byte[] input) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] decrypted = cipher.doFinal(input);
    return decrypted;
}

From source file:Main.java

public static byte[] encrypt(byte[] key, byte[] input) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] encrypted = cipher.doFinal(input);
    return encrypted;
}

From source file:Main.java

public static String encodeHmac(String key, String data) throws Exception {
    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
    sha256_HMAC.init(secret_key);//from   www.j  av  a 2s  .  c  om

    return bytesToHex(sha256_HMAC.doFinal(data.getBytes()));
}

From source file:Main.java

private static byte[] Aes(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
    Cipher cipher = null;//from www.jav  a  2  s .c  om
    try {
        SecretKeySpec aesKey = new SecretKeySpec(byteKey, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(opmode, aesKey);

        return cipher.doFinal(byteData);
    } finally {
        cipher = null;
    }
}

From source file:Main.java

static byte[] encodeHMAC(String key, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "RAW");

    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(keySpec);// w  ww  . jav a  2 s. co m
    return mac.doFinal(message);
}

From source file:Main.java

private static Key generateKey() throws Exception {
    if (key == null)
        key = new SecretKeySpec(keyValue, ALGO);

    return key;//from www.j av  a2  s .  c o m
}

From source file:Main.java

public static String encrypt(String data, String key) throws Exception {
    try {/*from w  w  w.  ja va  2 s  .  co  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}