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:aes_encryption.AES_Encryption.java

public static void setKey(String myKey) {

    MessageDigest sha = null;//ww  w  .ja  v a  2s.  c  o m
    try {
        key = myKey.getBytes("UTF-8");
        System.out.println(key.length);
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); //change the bits later 128 then 256
        System.out.println(key.length);
        System.out.println(new String(key, "UTF-8"));
        secretKey = new SecretKeySpec(key, "AES");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

From source file:encryptdecrypt.util.Security.java

public static String decrypt(String strToDecrypt) {
    try {/*from   w  ww  .  ja  va 2 s. co  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static Mac getMacForPassphrase(String passphrase, byte[] salt) throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt);
    byte[] pbkdf2 = key.getEncoded();
    SecretKeySpec hmacKey = new SecretKeySpec(pbkdf2, "HmacSHA1");
    Mac hmac = Mac.getInstance("HmacSHA1");
    hmac.init(hmacKey);//  w  w w .  j ava 2  s .  c o m

    return hmac;
}

From source file:Main.java

private static String des(String id, String key) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    int index = 0;
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(1, secretKeySpec);/*w ww.  j  a  v a 2 s. c om*/
    int i = id.length();
    if (i < 8) {
        i = 8 - i;
    } else {
        i %= 8;
        i = i != 0 ? 8 - i : 0;
    }
    while (index < i) {
        id = String.valueOf(id) + "\u0000";
        index++;
    }
    return a(cipher.doFinal(id.getBytes()));
}

From source file:Main.java

/**
 *
 * @throws InvalidKeySpecException/*from ww w .j a v a2 s .co m*/
 * @throws NoSuchAlgorithmException
 */
private static Key deriveKeyPbkdf2(byte[] salt, String password)
        throws InvalidKeySpecException, NoSuchAlgorithmException {

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH);
    SecretKeyFactory factory;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1And8bit");
    } else {
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    }

    byte[] keyBytes = factory.generateSecret(keySpec).getEncoded();

    return new SecretKeySpec(keyBytes, "AES");
}

From source file:Main.java

private static byte[] sign(String keyString, byte[] message) throws GeneralSecurityException {
    byte[] keyBytes = utf8(keyString);

    SecretKey key = new SecretKeySpec(keyBytes, MAC_NAME);
    Mac mac = Mac.getInstance(MAC_NAME);
    mac.init(key);//from   www.j ava 2 s  .  co m

    return mac.doFinal(message);
}

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

private static byte[] encryptData(byte[] content) {
    byte[] outBytes = null;

    try {/*from ww w. j  a v  a  2 s. c o m*/
        Key key = new SecretKeySpec(secret_key, "DESede");

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

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

        outBytes = cipher.doFinal(content);

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

    return outBytes;
}

From source file:com.sysfore.pos.licensemanagement.LicenceManagementUtil.java

public static String decrypt(String strToDecrypt) {
    try {// w w  w  .j a v  a2  s  .  co  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));

        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}

From source file:authentication.AES.java

public static String decrypt(String textoencriptado) throws Exception {
    Cipher decripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES");
    decripta.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(decripta.doFinal(Base64.decodeBase64(textoencriptado)), "UTF-8");
}

From source file:com.lwr.software.reporter.utils.EncryptionUtil.java

public static String encrypt(String value) {
    try {//from   w ww.j av  a2s .  c  o  m
        IvParameterSpec iv = new IvParameterSpec(
                DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(
                DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING),
                DashboardConstants.ALGORITHM);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}