Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

In this page you can find the example usage for javax.crypto Cipher getInstance.

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:Main.java

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:Main.java

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128, new SecureRandom(decryptKey.getBytes()));

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);

    return new String(decryptBytes);
}

From source file:Main.java

public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;/* ww w  .j av  a2 s. c om*/
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:Main.java

public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;/*w ww.  jav  a2  s  . c o  m*/
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);

    byte[] bOut = cipher.doFinal(data);

    return bOut;

}

From source file:Main.java

public static String encode(String key, String cleartext) throws Exception {
    SecretKeySpec secretKeySpec = createKey(key);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
    String result = parseByte2HexStr(c.doFinal(cleartext.getBytes("UTF-8")));

    return result;
}

From source file:Main.java

public static String deCrypto(String txt, String key) {
    SecretKeyFactory skeyFactory = null;
    Cipher cipher = null;//from w w w .ja  v  a2  s .  c o  m
    byte[] btxts = null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        skeyFactory = SecretKeyFactory.getInstance("DES");
        cipher = Cipher.getInstance("DES");
        SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        btxts = new byte[txt.length() / 2];
        for (int i = 0, count = txt.length(); i < count; i += 2) {
            btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16);
        }
        return (new String(cipher.doFinal(btxts)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:Main.java

public static byte[] decodeFile(byte[] key, byte[] fileData, boolean isPlainText) throws Exception {
    byte[] decrypted;
    if (!isPlainText) {
        decrypted = new byte[fileData.length - key.length * 2];
        System.arraycopy(fileData, key.length, decrypted, 0, decrypted.length);
    } else {//from ww w  .  ja v a 2s  .c om
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        decrypted = cipher.doFinal(fileData);
    }
    return decrypted;
}

From source file:Main.java

public static byte[] encryptAES(SecretKey sKey, byte[] message) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE, sKey);
    return c.doFinal(message);
}

From source file:Main.java

public static byte[] decryptAES(SecretKey sKey, byte[] message) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.DECRYPT_MODE, sKey);
    return c.doFinal(message);
}

From source file:Main.java

public static byte[] TriDesDecryption(byte[] byteKey, byte[] dec) {
    try {//from  w w w  .ja v a 2 s . c  om
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKey key = new SecretKeySpec(en_key, "DESede");
        Cipher dcipher = Cipher.getInstance("DESede/ECB/NoPadding");
        dcipher.init(Cipher.DECRYPT_MODE, key);

        byte[] de_b = dcipher.doFinal(dec);

        return de_b;
    } catch (Exception e) {
        e.printStackTrace();

    }
    return null;
}