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 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);/*  w ww  . j a va  2  s  . com*/
    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:cn.lynx.emi.license.ViewLicense.java

private static final String _decrypt(String data) {
    byte[] corekey = Base64.decodeBase64(LICENSE_CORE_KEY);
    byte[] rawData = Base64.decodeBase64(data);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(corekey);
    try {/*from   w  w  w . ja  v  a2 s.c  o  m*/
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        return new String(cipher.doFinal(rawData), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java

public static String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();//  ww w  .  ja va  2  s.  co m
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = Base64.encodeBase64String(encValue);
    return encryptedValue;
}

From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();// www  . j  a va 2s.  com
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = Base64.decodeBase64(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.searchbox.utils.DecryptLicense.java

public static byte[] decrypt(byte[] inpBytes) throws Exception {

    byte[] pkbytes = Base64.decodeBase64(privkey);
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pkbytes);
    PrivateKey pk = keyFactory.generatePrivate(privateKeySpec);

    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, pk);
    return cipher.doFinal(inpBytes);
}

From source file:Main.java

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

    Key deskey = null;//from  w  w  w  . j  ava 2 s . com
    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:com.esri.geoevent.datastore.Crypto.java

static public String doEncrypt(String stringToEncrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, encryptionKey);
    byte[] encVal = c.doFinal(stringToEncrypt.getBytes());
    String encodedEncryptedString = new String(Base64.encodeBase64(encVal));
    return encodedEncryptedString;
}

From source file:Main.java

public static byte[] TriDesDecryption(byte[] byteKey, byte[] dec) {
    try {//ww w.j  a v  a2  s.c  o m
        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;
}

From source file:com.esri.geoevent.datastore.Crypto.java

static public String doDecrypt(String stringToDecrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, encryptionKey);
    byte[] decodedValue = Base64.decodeBase64(stringToDecrypt.getBytes());
    byte[] decryptedValue = c.doFinal(decodedValue);
    String decryptedString = new String(decryptedValue);
    return decryptedString;
}

From source file:Main.java

public static void encrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream(fileIn);
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    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);// w  w w. j  av a 2  s. com
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}