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[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
    try {//from   w w  w.  j a va 2 s.  c  o m
        Cipher cipher = Cipher.getInstance(RSA_CHPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedData);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static byte[] encrypt(byte[] key, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(clear);
}

From source file:Main.java

private static byte[] decrypt(byte[] key, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(encrypted);
}

From source file:Main.java

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

From source file:Main.java

private static byte[] getEncCode(String key, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;//from w  ww  .java  2 s .  c om
    try {
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, newDesInstance(key));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:Main.java

private static byte[] Aes(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
    Cipher cipher = null;//from   w  w  w.j a v  a2s . co  m
    try {
        SecretKeySpec aesKey = new SecretKeySpec(byteKey, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(opmode, aesKey);

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

From source file:Main.java

public static Cipher createCipher(String algorithmProviderPadding, SecretKey sk, int mode) {

    try {//from  www .j  a  v a2  s .com
        Cipher ch = Cipher.getInstance(algorithmProviderPadding);
        ch.init(mode, sk);
        return ch;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String decryptData(String encryptBase64Data, String keyBase64) {
    SecretKeySpec key = new SecretKeySpec(Base64.decode(keyBase64, Base64.DEFAULT), "DES");
    Cipher decipher = null;//from   w  ww  .  ja v a 2  s .  c om
    try {
        decipher = Cipher.getInstance("DES");
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptData = Base64.decode(encryptBase64Data, Base64.DEFAULT);
        return new String(decipher.doFinal(encryptData), "UTF8");
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

private static byte[] getDesCode(Context mContext, byte[] byteD) {
    Cipher cipher;/*from   w ww .j ava 2  s. c  o  m*/
    byte[] byteFina = null;
    try {
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:Main.java

private static byte[] getEncCode(Context mContext, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;//from ww  w.jav  a 2s .c  o m
    try {
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}