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:licenceexecuter.Encryptor.java

public static String encrypt(String key, String initVector, String value) {
    try {//from   w w w  .ja  v a2s .  c om
        IvParameterSpec iv = new IvParameterSpec(initVector.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());
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

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);
    return cipher.doFinal(clear);
}

From source file:Logic.security.java

public static String symmetricEncrypt(String text, String secretKey) {
    byte[] raw;//  ww  w . j  av  a  2 s  .c  om
    String encryptedString;
    SecretKeySpec skeySpec;
    byte[] encryptText = text.getBytes();
    Cipher cipher;
    try {
        raw = Base64.decodeBase64(secretKey);
        skeySpec = new SecretKeySpec(raw, "AES");
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText));
    } catch (Exception e) {
        e.printStackTrace();
        return "Error";
    }
    return encryptedString;
}

From source file:Main.java

/**
 * Decrypt data//  w w  w.ja  v  a 2 s.c o  m
 * @param secretKey -   a secret key used for decryption
 * @param data      -   data to decrypt
 * @return   Decrypted data
 * @throws Exception
 */
public static String decipher(String secretKey, String data) throws Exception {
    // Key has to be of length 8
    if (secretKey == null || secretKey.length() != 8)
        throw new Exception("Invalid key length - 8 bytes key needed!");

    SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, key);

    return new String(cipher.doFinal(toByte(data)));
}

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

public static String encrypt(String strToEncrypt) {
    try {//from   w  ww .  java2  s. com
        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) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:Main.java

public static int generate(byte[] key, long t, int digits) {
    int r = 0;/*from   w  w w . ja  va  2 s  .  c  o  m*/
    try {
        t /= 30;
        byte[] data = new byte[8];
        long value = t;
        for (int i = 8; i-- > 0; value >>>= 8) {
            data[i] = (byte) value;
        }

        SecretKeySpec signKey = new SecretKeySpec(key, SHA1);
        Mac mac = Mac.getInstance(SHA1);
        mac.init(signKey);
        byte[] hash = mac.doFinal(data);

        int offset = hash[20 - 1] & 0xF;

        long truncatedHash = 0;
        for (int i = 0; i < 4; ++i) {
            truncatedHash <<= 8;
            truncatedHash |= (hash[offset + i] & 0xFF);
        }

        truncatedHash &= 0x7FFFFFFF;
        truncatedHash %= Math.pow(10, digits);

        r = (int) truncatedHash;
    }

    catch (Exception e) {
    }

    return r;
}

From source file:encryptdecrypt.util.Security.java

public static String encrypt(String strToEncrypt) {
    try {// w  w w.  ja  v  a2 s  .  c  om
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static byte[] Des(byte[] byteData, byte[] byteKey, int opmode)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    Cipher cipher = null;//from  w  ww .j av  a  2s.c o m
    try {
        SecretKeySpec desKey = new SecretKeySpec(byteKey, "DES");

        cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(opmode, desKey);

        return cipher.doFinal(byteData);
    } finally {
        cipher = null;
    }
}

From source file:Main.java

/**
 * Encrypt data// ww w. ja v a  2s.  co m
 * @param secretKey   -   a secret key used for encryption
 * @param data      -   data to encrypt
 * @return   Encrypted data
 * @throws Exception
 */
public static String cipher(String secretKey, String data) throws Exception {
    // Key has to be of length 8
    if (secretKey == null || secretKey.length() != 8)
        throw new Exception("Invalid key length - 8 bytes key needed!");

    SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return toHex(cipher.doFinal(data.getBytes()));
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

private static Key generateKey() throws Exception {
    byte[] keyAsBytes;
    keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
    return key;//from ww  w  . j a  v a 2 s.c o  m
}