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:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String encrypt(final String unsecureText, final String key) throws PunchOutCipherException {
    String encrypted = null;/*from  www .  ja va  2s .  c o  m*/
    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        final byte[] encryptedValue = cipher.doFinal(unsecureText.getBytes());
        encrypted = new Base64().encodeAsString(encryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during encryption." + e.getMessage();
        LOG.error(msg);
        throw new PunchOutCipherException(msg, e);
    }

    return encrypted;
}

From source file:com.tydic.dbp.utils.ThreeDesUtils.java

public static String encryptMode(String Src) {
    try {/*from w  ww  .  j  av a  2s.com*/
        // ?
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        return Hex.encodeHexString(c1.doFinal(Src.getBytes()));
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void decrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(fileIn);
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);/*from  www  .java 2 s. c  o  m*/
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}

From source file:crocserver.app.CrocSecurity.java

public static Mac createHmac(byte[] secret) throws Exception {
    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec signKey = new SecretKeySpec(secret, "HmacSHA1");
    mac.init(signKey);/* www  .j a  v  a 2s .c om*/
    return mac;
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a string.// w  w w.  j a v  a  2s. c  o m
 *
 * @param c The string to encrypt.
 * @return The encrypted string in HEX.
 */
public static String encrypt(String c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c.getBytes());
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt string", e);
        return null;
    }
}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Loads a raw secret key.//from   w w w . ja  va 2s  .c om
 * 
 * @param key
 * @param keyAlgorithm
 * @return
 * @throws GeneralSecurityException
 */
public static Key loadRawSecretKey(byte[] key, String keyAlgorithm) throws GeneralSecurityException {
    return new SecretKeySpec(key, keyAlgorithm);
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea cifrar/* w  w w .j  a v a 2  s .c o  m*/
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param cleartext el texto sin cifrar a encriptar
 * @return el texto cifrado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException
 */
public static String encrypt(String key, String iv, String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:com.fpt.crypto.CipherDemo.java

public static String decrypt(String in, String key) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, DecoderException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] inputBytes = Hex.decodeHex(in.toCharArray());
    SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secKey);
    byte[] outputBytes = cipher.doFinal(inputBytes);
    return new String(outputBytes);

}

From source file:D_common.E_ncript.java

public static byte[] encrypt(byte[] data, String keyStr) {
    try {/*w  w w  .ja  v a2 s . c o  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * /*w w  w  . jav  a  2s .co m*/
 * Get Private Key or return null.
 * 
 * @param account
 *            the account we're syncing
 * @return Private Key
 */
public static SecretKey getPrivateKey(Account account, AccountManager accountManager) {
    // TODO: Redesign more secure location to safe private key?
    String keyString = accountManager.getUserData(account, PRIVATE_KEY);
    SecretKey key = null;
    if (!TextUtils.isEmpty(keyString)) {
        key = new SecretKeySpec(Base64.decode(keyString, Base64.DEFAULT), "AES");
    }
    return key;
}