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:com.formatAdmin.utils.UtilsSecurity.java

public static String cifrarMD5(String texto) {
    String base64EncryptedString = "";
    try {//w w w.  j  a v a 2 s.  com
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
    }
    return base64EncryptedString;
}

From source file:com.konakart.bl.modules.payment.barclaycardsmartpayhosted.BarclaycardSmartPayHostedHMACTools.java

private static SecretKey getMacKey(String secret) {
    try {/*w ww. j  av a2 s. c om*/
        return new SecretKeySpec(secret.getBytes("ASCII"), HMAC_ALGORITHM);
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}

From source file:Main.java

private static Key KeyGenerator(byte input[])
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {

    Key key = new SecretKeySpec(input, desAlgorithm);
    return key;//from   w w  w. j a  v a 2s  . c o  m
}

From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java

public static String encrypt(String key1, String iv1, String value) {
    try {/*from w ww .  java 2s . c om*/
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        //System.out.println("encrypted string:" + Base64.encodeBase64String(encrypted));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.alibaba.openapi.client.util.SignatureUtil.java

public static SecretKeySpec buildKey(byte[] key) {
    return new SecretKeySpec(key, HMAC_SHA1);
}

From source file:me.buom.shiro.util.HmacSha1.java

public static byte[] hash(byte[] privateKey, String stringToSign)
        throws NoSuchAlgorithmException, InvalidKeyException {
    // Get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(privateKey, 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);/*w  w w . j  a  va  2  s.c  o m*/

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

    // Convert raw bytes to Hex
    return new Hex().encode(rawHmac);
}

From source file:Main.java

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

From source file:com.messagemedia.restapi.client.v1.internal.http.interceptors.HMACUtils.java

/**
 * This method encrypts data with secret using HmacSHA1. The strings are converted to bytes using the UTF-8 encoding.
 *
 * @param secret - the secret which is being used
 * @param data   - the data which is encrypted
 * @return the base64 encoded result/*w  ww.  ja v  a2 s  .  co m*/
 */
static String sha1(String secret, String data) {
    try {
        SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("UTF-8"), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
        return new String(Base64.encodeBase64(rawHmac), "UTF-8");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt data", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Can not find the UTF-8 charset", e);
    }
}

From source file:Main.java

private static byte[] hmacTemplate(byte[] data, byte[] key, String algorithm) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {//w  w  w  .  ja v  a2  s  .co m
        SecretKeySpec secretKey = new SecretKeySpec(key, algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(secretKey);
        return mac.doFinal(data);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.avbravo.avbravoutils.crypto.Encriptador.java

public static String decrypt(String key, String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}