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.ait.tooling.server.core.security.SimpleKeyStringSigningProvider.java

public SimpleKeyStringSigningProvider(final String sign) {
    try {//from   ww  w  . j  a  v a 2s  .  c o m
        m_secret = new SecretKeySpec(Objects.requireNonNull(sign).getBytes(IHTTPConstants.CHARSET_UTF_8),
                HMAC_ALGORITHM);
    } catch (Exception e) {
        logger.error("hmac error", e);

        throw new IllegalArgumentException(e);
    }
}

From source file:com.vab.iflex.gateway.paybillservice.Stub.java

private static String calculateRFC2104HMAC(String data, String key) throws Exception {
    String result;//  www.  ja  va2  s.  co  m
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.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);

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

        // base64-encode the hmac
        //            result = new BASE64Encoder().encode(rawHmac);
        result = new Base64().encodeAsString(rawHmac);

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
    }
    return result;
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String decryptLanToken(String lanTokenKey, String encryptedToken) throws Exception {

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    return new String(cipher.doFinal(Base64.decodeBase64(encryptedToken)), Charset.forName("UTF-8"));
}

From source file:com.wso2telco.proxy.util.DecryptAES.java

public static String decrypt(String encryptedText) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException, ConfigurationException {
    if (encryptionKey != null) {
        byte[] encryptionKeyByteValue = encryptionKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(encryptionKeyByteValue, AuthProxyConstants.ASE_KEY);
        String decryptedText = null;
        if (encryptedText != null) {
            byte[] encryptedTextByte = Base64.decodeBase64(encryptedText);
            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
            decryptedText = new String(decryptedByte);
        }/*from ww  w .j av  a  2 s. c om*/
        return decryptedText;
    } else {
        throw new ConfigurationException("MSISDN EncryptionKey could not be found in mobile-connect.xml");
    }
}

From source file:spring.travel.site.auth.Signer.java

public String sign(String data) throws AuthException {
    try {/*  w ww .j  a v  a  2s  .  c o m*/
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM);
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);
        byte[] raw = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return toHex(raw);
    } catch (InvalidKeyException | NoSuchAlgorithmException e) {
        throw new AuthException("Failed signing data", e);
    }
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided/*from  ww w .  java2 s  .c om*/
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:com.amazon.pay.impl.Util.java

/**
 * Helper method to calculate base64 encoded signature using specified secret key
 *
 *//*w  ww .  j  a va 2 s  .co  m*/
public static String getSignature(String stringToSign, String secretKey) throws IllegalStateException,
        InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256"));
    byte[] signature = mac.doFinal(stringToSign.getBytes("UTF-8"));
    String signatureBase64 = new String(Base64.encodeBase64(signature), "UTF-8");
    return signatureBase64;
}

From source file:com.os.util.PasswordDecoderEncoder.java

public static String encrypt(String plainPassword) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    key = convertHexToBytes(keyst);/*from   ww w . j  a  v  a2 s  . com*/
    final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    final String encryptedString = Base64.encodeBase64String(cipher.doFinal(plainPassword.getBytes("UTF8")));
    System.out.println(encryptedString);
    String passwordEncrypted = encryptedString.trim();

    return passwordEncrypted;

}

From source file:com.haulmont.cuba.web.test.PasswordEncryptionTest.java

protected String encryptPassword(String password) {
    SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes());
    String result;//w  ww.ja v a 2s . c  om
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
        result = new String(Hex.encodeHex(cipher.doFinal(password.getBytes())));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:encrypt.algorithms.AESCBC.java

public String decrypt(String key1, String key2, String encrypted) {
    try {//from   ww w . j  av a2 s. c  o  m
        IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));

        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}