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:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a string./*from   w w w.j a  v  a  2 s.  c  o  m*/
 *
 * @param c The string to encrypt.
 * @return The encrypted string in HEX.
 */
public static String encrypt(String c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c.getBytes());
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt string", e);
        return null;
    }
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a byte[]./*from w  w w.  j  a  v  a2s. c  o  m*/
 *
 * @param c   The byte[] to encrypt.
 * @param key The key.
 * @return The encrypted array as a HEX string.
 */
public static String encrypt(byte[] c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c);
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt byte[]", e);
        return null;
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String encryptStringImpl(Context context, final String plainText) {
    String encryptedText = plainText;
    try {//  w w w  . j  av  a2 s .c om
        final KeyStore keyStore = getKeyStore(context);

        PublicKey publicKey = keyStore.getCertificate(KEY_ALIAS).getPublicKey();

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        cipherOutputStream.write(plainText.getBytes("UTF-8"));
        cipherOutputStream.close();

        byte[] bytes = outputStream.toByteArray();
        encryptedText = Base64.encodeToString(bytes, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedText;
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * /*from  www  .  j a  va  2s.c  o m*/
 * 
 * @param target
 * @throws Exception
 */
static void decryptionByPublicKey(String target) throws Exception {
    PublicKey publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    cipher.update(decodeBase64(target));
    String source = new String(cipher.doFinal(), "UTF-8");
    System.out.println("??\r\n" + source);
}

From source file:Main.java

public static byte[] desEncrypt(byte[] data, String key) throws Exception {
    try {/*w  w  w .  j  a  v 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.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[] encrypt2(byte[] data, String key) throws Exception {
    try {/*from w  w w  .  jav a2s  .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 String encrypt(String data, String key) throws Exception {
    try {/*from  w  w  w . ja va 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.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:com.wabacus.util.DesEncryptTools.java

public static String decrypt(String encryptedString) {
    try {//from  w  w  w  .j  a va2  s .c  o  m
        if (KEY_OBJ == null) {
            log.warn("" + encryptedString);
            return encryptedString;
        }
        byte[] b = base64Decode(encryptedString);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.DECRYPT_MODE, KEY_OBJ);
        return new String(c1.doFinal(b));
    } catch (Exception e) {
        throw new WabacusConfigLoadingException("" + encryptedString + "", e);
    }
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * //from w  w w .  j a  v a  2 s.co  m
 * 
 * @param data
 * @return
 * @throws Exception
 */
static String encryptionByPublicKey(String source) throws Exception {
    PublicKey publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    cipher.update(source.getBytes("UTF-8"));
    String target = encodeBase64(cipher.doFinal());
    System.out.println("??\r\n" + target);
    return target;
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * ?//from w w w. j a v  a  2 s  .  co  m
 * 
 * @param target
 * @throws Exception
 */
static void decryptionByPrivateKey(String target) throws Exception {
    PrivateKey privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    cipher.update(decodeBase64(target));
    String source = new String(cipher.doFinal(), "UTF-8");
    System.out.println("???\r\n" + source);
}