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 String encrypt(String data, String key) throws Exception {
    try {/*  w w w . j av  a2  s  .c  o m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] encrypt2(byte[] data, String key) throws Exception {
    try {//from www  . ja  va2 s.  c o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keyspec);
        byte[] encrypted = cipher.doFinal(data);
        return encrypted;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] desEncrypt(byte[] data, String key) throws Exception {
    try {//from  www .  jav  a  2s .c  o m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        byte[] base64Key = Base64.decode(key, Base64.DEFAULT);
        SecretKeySpec keyspec = new SecretKeySpec(base64Key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, keyspec);
        byte[] original = cipher.doFinal(data);
        return original;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
    try {/*from  ww  w . j a  v a 2s .c  om*/
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedData);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static byte[] Rsa(byte[] byteData, Key pKey, int opmode) throws Exception {
    Cipher cipher = null;//w w w . j  av a2s.com
    try {
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(opmode, pKey);

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

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");

    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}