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

public static byte[] hmacSha1(String key, String value)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    String type = "HmacSHA1";
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
    Mac mac = Mac.getInstance(type);
    mac.init(secret);//from  w  ww.  ja va  2 s  .  c  om
    return mac.doFinal(value.getBytes());
}

From source file:Main.java

private static byte[] encryptHMAC(String data, String secret) throws IOException {
    byte[] bytes = null;
    try {/*from w  ww . j a  va 2 s . c o  m*/
        SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacMD5");
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        bytes = mac.doFinal(data.getBytes("UTF-8"));
    } catch (GeneralSecurityException gse) {
        String msg = getStringFromException(gse);
        throw new IOException(msg);
    }
    return bytes;
}

From source file:Main.java

private static SecretKeySpec generateAESKey(String password) {
    byte[] data = null;
    StringBuilder sb = new StringBuilder();
    sb.append(password);/*ww w.  j  ava2s .com*/
    while (sb.length() < 16)
        sb.append("0");
    if (sb.length() > 16)
        sb.setLength(16);
    try {
        data = sb.toString().getBytes("UTF-8");
        return new SecretKeySpec(data, "AES");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:Main.java

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(decryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);

    return new String(decryptBytes);
}

From source file:Main.java

public static String doSignWithSHA1(String toSign, String keyString) throws Exception {
    SecretKeySpec key = new SecretKeySpec((keyString).getBytes(UTF_8), HMAC_SHA1);
    Mac mac = Mac.getInstance(HMAC_SHA1);
    mac.init(key);/*from   w  ww.jav  a2  s.  com*/
    byte[] bytes = mac.doFinal(toSign.getBytes(UTF_8));
    return Base64.encodeToString(bytes, Base64.CRLF).replace(CARRIAGE_RETURN, EMPTY_STRING);
}

From source file:Main.java

public static byte[] getDecCode(byte[] byteD, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;/*from w  w  w.  j  a  va 2 s. co  m*/

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");
        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:Main.java

public static byte[] getEncCode(byte[] byteE, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;//from  w  ww  .  j  a va  2s.  co m

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");

        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteE);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:Main.java

public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {/*from w  ww. j  a  va  2  s . c o m*/
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
    } catch (Exception e) {
        Log.e(TAG, "Error during encryption: " + e.toString());
        return errorbyte;
    }
}

From source file:Main.java

public static String sign(String paramString1, String paramString2, String paramString3) {
    if (paramString3 == null)
        paramString3 = "";
    try {/*  w  ww.  j  av  a 2 s.  co  m*/
        SecretKeySpec localSecretKeySpec = new SecretKeySpec(
                (urlencode(paramString2, "UTF8") + '&' + urlencode(paramString3, "UTF8")).getBytes("UTF8"),
                "HmacSHA1");
        Mac localMac = Mac.getInstance("HmacSHA1");
        localMac.init(localSecretKeySpec);
        String str = urlencode(
                new String(Base64.encode(localMac.doFinal(paramString1.getBytes("UTF8")), 0), "UTF8"), "UTF8");
        return str;
    } catch (InvalidKeyException localInvalidKeyException) {
        return "";
    } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
        return "";
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
    }
    return "";
}