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:name.martingeisse.common.util.HmacUtil.java

/**
 * Base function to generate a HMAC.//from   w w w .  j  a va2 s  . c  o  m
 * 
 * @param payload the payload data
 * @param secret the secret to sign the HMAC
 * @param algorithm the HMAC algorithm to use
 * @return the HMAC
 */
public static byte[] generateHmac(byte[] payload, byte[] secret, String algorithm) {
    try {
        final Mac mac = Mac.getInstance(algorithm);
        mac.init(new SecretKeySpec(secret, algorithm));
        return mac.doFinal(payload);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

private static byte[] hmacSha1(byte[] value, byte[] key) {
    try {/*from w w w . j  a  va  2 s. co  m*/
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        return mac.doFinal(value);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.liferay.sync.engine.util.Encryptor.java

public static String decrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }/*  w ww .j a v  a2 s . com*/

    SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM);

    Cipher cipher = Cipher.getInstance(_ALGORITHM);

    cipher.init(Cipher.DECRYPT_MODE, secretKey);

    String decryptedValue = value;

    for (int i = 0; i < _ITERATIONS; i++) {
        byte[] decodedBytes = Base64.decodeBase64(decryptedValue);

        byte[] decryptedBytes = cipher.doFinal(decodedBytes);

        decryptedValue = new String(decryptedBytes, _UTF8_CHARSET).substring(_SALT_LENGTH);
    }

    return decryptedValue;
}

From source file:com.centurylink.mdw.util.HmacSha1Signature.java

public static String getHMACHexdigestSignature(byte[] payloadBytes, String key)
        throws InvalidKeyException, NoSuchAlgorithmException {
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM);
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(keySpec);/*from w w w .  j  a v  a2s .  c o  m*/
    byte[] rawHmac = mac.doFinal(payloadBytes);
    return Hex.encodeHexString(rawHmac);
}

From source file:io.milton.http.http11.auth.HmacUtils.java

public static String calcShaHash(String data, String key) {
    String result = null;// ww w.  j a v a2s  .  c  o m
    try {
        Key signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes());
        result = Base64.encodeBase64URLSafeString(rawHmac);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(HMAC_SHA1_ALGORITHM, e);
    }

    return result;
}

From source file:com.opentok.util.Crypto.java

public static String signData(String data, String key)
        throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(signingKey);/*w  ww . java  2s. c om*/
    return toHexString(mac.doFinal(data.getBytes()));
}

From source file:com.amazonaws.sqs.util.AwsSignature.java

public static String calculate(String stringToSign, String secretKey) throws Exception {
    // get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes(), HMAC_SHA1_ALGORITHM);

    // get an hmac_sha1 Mac instance and initialize with the signing key
    Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
    mac.init(signingKey);// www  . ja  v  a 2  s .  co  m

    // compute the hmac on input data bytes
    byte[] rawHmac = mac.doFinal(stringToSign.getBytes());

    // base64-encode the hmac
    String result = new String(Base64.encodeBase64(rawHmac));
    return result;
}

From source file:Main.java

public static byte[] TriDesEncryption(byte[] byteKey, byte[] dec) {
    try {//from w ww . ja  va2  s. c  o  m
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKeySpec key = new SecretKeySpec(en_key, "DESede");
        Cipher ecipher = Cipher.getInstance("DESede/ECB/NoPadding");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] en_b = ecipher.doFinal(dec);
        return en_b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes)
        throws Exception {
    String macAlgorithm = "HmacMD5";
    Mac mac = Mac.getInstance(macAlgorithm);
    mac.init(new SecretKeySpec(userPassword.getBytes("UTF-8"), macAlgorithm));
    final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
    String responseAsString = userName + " " + toHex(messageAuthenticationCode);
    return responseAsString.getBytes();
}

From source file:Main.java

public static String decryptData(String ciphertext, String password) throws Exception {
    int iterationCount = 100; //because polaroid
    int keyLength = 256;

    String[] fields = ciphertext.split("]");
    byte[] iv = Base64.decode(fields[0], 0);
    byte[] salt = Base64.decode(fields[1], 0);
    byte[] cipherBytes = Base64.decode(fields[2], 0);

    Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length);

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    String plainStr = new String(plaintext, "UTF-8");

    return plainStr;
}