Example usage for javax.crypto Cipher init

List of usage examples for javax.crypto Cipher init

Introduction

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

Prototype

public final void init(int opmode, Certificate certificate) throws InvalidKeyException 

Source Link

Document

Initializes this cipher with the public key from the given certificate.

Usage

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  w  ww.  j  a  v  a2 s.  c o m
        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

/**
 * function decrypt the string and return the result
 * @param stringToDecrypt the string against which the decryption to be performed
 * @return the decrypted String/*from  ww w  . j  a  va2  s  .  c o m*/
 */
public static final String decrypt(String stringToDecrypt) {
    try {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        return new String(cipher.doFinal(stringToDecrypt.getBytes()));
    } catch (Exception e) {

    }
    return null;
}

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;
}

From source file:Main.java

public static byte[] cipher(int mode, byte[] data, byte[] secret) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    SecretKeySpec secretKeySpec = new SecretKeySpec(sha256(secret), "AES");
    Cipher c = Cipher.getInstance("AES");
    c.init(mode, secretKeySpec);
    return c.doFinal(data);
}

From source file:Main.java

public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
    try {//from   w w  w.  j  a va2s  .  c  om
        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:com.java.demo.DesDemo.java

public static byte[] Decrypt(byte[] strBytes) {
    try {//from  w w w.j  av a  2s .  co  m
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(strBytes);
        return result;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.java.demo.DesDemo.java

public static byte[] Encrypt(String str) {
    try {/* w ww .  java 2  s  .  co  m*/
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
        byte[] result = cipher.doFinal(str.getBytes());
        return result;

    } catch (Exception e) {
        return null;
    }
}

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);
    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);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

From source file:Main.java

/**
 * Decrypt data/*  w w  w  .j av  a  2s. c  om*/
 * @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)));
}