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.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

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

    bytes = Arrays.copyOf(bytes, 16);

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

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

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);/*from  w  w w.  j a v a 2  s  .  co  m*/

    return encryptedToken;
}

From source file:cn.org.once.cstack.utils.CustomPasswordEncoder.java

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;/*from   w  w w. ja v  a  2  s  .  c om*/
}

From source file:com.gmu.uav.RadioSecurity.java

public static String hmacSha1(String value, byte[] key) {
    try {//from w w  w. j av a 2s .c om
        // Get an hmac_sha1 key from the raw key bytes    
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");

        // Get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);

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

        // Convert raw bytes to Hex
        byte[] hexBytes = new Hex().encode(rawHmac);

        //  Covert array of Hex bytes to a String
        return new String(hexBytes, "UTF-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.kylinolap.rest.security.PasswordPlaceholderConfigurer.java

public static String encrypt(String strToEncrypt) {
    try {//from w  w  w . j  a va2s .c o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
    }
    return null;

}

From source file:com.ro.ssc.app.client.licensing.TrialKeyValidator.java

public static String decodeKey(String encodedEncrypted) {
    String decoded = "";
    try {//  w w w .  j  a va  2  s . c  o  m
        byte[] saltDecrypt = SALT_DECRYPT.getBytes(StandardCharsets.UTF_8);
        SecretKeyFactory factoryKeyDecrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp2 = factoryKeyDecrypt.generateSecret(
                new PBEKeySpec(PASS_DECRYPT.toCharArray(), saltDecrypt, ITERATIONS_DECRYPT, KEY_LENGTH));
        SecretKeySpec decryptKey = new SecretKeySpec(tmp2.getEncoded(), ALGORITHM);
        Cipher aesCipherDecrypt = Cipher.getInstance(CIPHER);
        aesCipherDecrypt.init(Cipher.DECRYPT_MODE, decryptKey);
        byte[] e64bytes = StringUtils.getBytesUtf8(encodedEncrypted);
        byte[] eBytes = Base64.decodeBase64(e64bytes);
        byte[] cipherDecode = aesCipherDecrypt.doFinal(eBytes);
        decoded = StringUtils.newStringUtf8(cipherDecode);
    } catch (Exception e) {
        LOGGER.error("Error while decoding the trial key", e);
    }
    return decoded;
}

From source file:Main.java

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

private static byte[] decryptData(byte[] bInput) {
    byte[] outBytes = null;
    try {/*w  w  w  .  j  a v a 2s .c o m*/
        Key key = new SecretKeySpec(secret_key, "DESede");

        Cipher cipher = Cipher.getInstance("DESede", "SunJCE");

        cipher.init(Cipher.DECRYPT_MODE, key, cipher.getParameters());

        outBytes = cipher.doFinal(bInput);

    } catch (Exception ex) {
        //log.error("PacketUtil",ex.getMessage(), ex);
        return outBytes;
    }

    return outBytes;
}

From source file:com.imaginary.home.cloud.Configuration.java

static public @Nonnull String decrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {//from  w  w  w .j a v a 2 s  . c  o m
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, spec);

        byte[] b64 = value.getBytes("utf-8");
        byte[] raw = Base64.decodeBase64(b64);
        byte[] decrypted = cipher.doFinal(raw);
        return new String(decrypted, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hp.application.automation.tools.EncryptionUtils.java

public static String Decrypt(String text, String key) throws Exception {

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] keyBytes = new byte[16];
    byte[] b = key.getBytes("UTF-8");
    int len = b.length;
    if (len > keyBytes.length)
        len = keyBytes.length;/*from  w w w. j  av  a 2s.  c  o m*/
    System.arraycopy(b, 0, keyBytes, 0, len);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    byte[] results = cipher.doFinal(Base64.decodeBase64(text));

    return new String(results, "UTF-8");
}

From source file:com.jsmartframework.web.manager.CsrfEncrypter.java

private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_CSRF_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_CSRF_ENCRYPT_CIPHER, encryptCipher);
    }/*from  w w  w .j  a v  a  2 s . c o  m*/
    return encryptCipher;
}