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

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

From source file:Main.java

public static byte[] encryptMsg(String message) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException,
        UnsupportedEncodingException {
    if (message == null) {
        return new byte[0];
    }//  w  ww  . j a  v  a 2 s.  c om
    /* Encrypt the message. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
    return cipherText;
}

From source file:io.cloudslang.content.database.utils.TripleDES.java

static byte[] encryptString(final byte[] text) throws Exception {
    final SecretKey key = new SecretKeySpec(
            TripleDES.md5Hash("NpWsCaJQj1LaXt)YYnzr\\%zP~RydB*3YGutr*@|A\\ckG3\\Yf%k"), ENCRYPTION_KEYSPECTYPE);
    final Cipher cipher = Cipher.getInstance(ENCRYPTION_MODE);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(text);
}

From source file:Main.java

public static byte[] TriDesEncryption(byte[] byteKey, byte[] dec) {
    try {/*from   w w w  . ja va 2  s.co 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);
        }
        SecretKeySpec key = new SecretKeySpec(en_key, "DESede");
        Cipher ecipher = Cipher.getInstance("DESede/ECB/NoPadding");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] en_b = ecipher.doFinal(dec);
        return en_b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.bytecode.util.Crypto.java

private static Key generateKey(String keystring, int bits) throws Exception {
    byte[] keyBytes = new byte[bits];
    byte[] key = new byte[bits];
    for (int i = 0; i < bits; i++) {
        keyBytes[i] = (byte) keystring.codePointAt(i);
    }//  w w  w  .  j a v  a 2s .co m
    SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    key = cipher.doFinal(keyBytes);
    for (int i = 0; i < bits - 16; i++) {
        key[16 + i] = key[i];
    }

    return new SecretKeySpec(key, "AES");
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);//  ww w .  j  a  v  a  2  s.c om
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String decrypt1(String encryptedData) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);/*from  ww w .  j ava2s  .  co m*/
    SecretKey key = keyGen.generateKey();
    Cipher c = Cipher.getInstance(ALGO);

    c.init(Cipher.DECRYPT_MODE, key);
    //        byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    //        byte[] decValue = c.doFinal(decordedValue);
    byte[] decValue = c.doFinal(encryptedData.getBytes("UTF8"));
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.zf.decipher.DataEn.java

private static byte[] encrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }/*from ww  w .ja va  2s. c  om*/
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:com.zf.decipher.DataEn.java

private static byte[] decrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }//w w w  .  j a va2 s  .com
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

static byte[] decryptRsaB64(String s64, Key privRsaKey) {
    try {/* w  w w  . ja v  a2s.  co  m*/
        byte[] sBytes = decodeB64(s64);
        if (sBytes != null) {
            Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
            cipher.init(Cipher.DECRYPT_MODE, privRsaKey);
            return cipher.doFinal(sBytes);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}