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:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java

/** Decrypts the specified encrypted string, using the specified secret key. */
private static String decrypt(String sName, String secretKey) {
    if (secretKey == null) {
        secretKey = SYM_KEY_STR;//ww w  . j  av  a2s. c o m
    }

    if (sName == null || sName.equals("")) {
        return "";
    }

    String sText = "";
    try {
        SecretKeySpec skeySpec = decodeKey(secretKey);
        Cipher decryptCipher = Cipher.getInstance(TRANSFORMATION);
        decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec);

        byte[] encpArr = new Base64().decode(sName.trim());

        byte[] plainText = decryptCipher.doFinal(encpArr);

        sText = new String(plainText, CHARSET);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    return sText;
}

From source file:com.imaginary.home.cloud.Configuration.java

static public @Nonnull String decrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {//  w w w. j  a v  a2  s  .  c om
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, spec);

        byte[] b64 = value.getBytes("utf-8");
        byte[] raw = Base64.decodeBase64(b64);
        byte[] decrypted = cipher.doFinal(raw);
        return new String(decrypted, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.imaginary.home.cloud.Configuration.java

static public @Nonnull String encrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {/*ww w .  ja v a  2s.co  m*/
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, spec);

        byte[] raw = value.getBytes("utf-8");
        byte[] encrypted = cipher.doFinal(raw);
        byte[] b64 = Base64.encodeBase64(encrypted);
        return new String(b64, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.bconomy.autobit.Encryption.java

public static byte[] encrypt(byte[] cleartext, byte[] key) {
    if (keySpec == null)
        keySpec = new SecretKeySpec(key, "AES");
    Cipher aes;
    try {//from w  ww. j a  v a 2s  .c  o m
        aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes.init(Cipher.ENCRYPT_MODE, keySpec);
        return aes.doFinal(cleartext);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        ex.printStackTrace();
    }
    return new byte[0];
}

From source file:com.bconomy.autobit.Encryption.java

public static byte[] decrypt(byte[] cyphertext, byte[] key) {
    if (keySpec == null)
        keySpec = new SecretKeySpec(key, "AES");
    Cipher aes;
    try {//from  ww  w.j  a v a2 s  . com
        aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes.init(Cipher.DECRYPT_MODE, keySpec);
        return aes.doFinal(cyphertext);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        ex.printStackTrace();
    }
    return new byte[0];
}

From source file:com.example.license.RSAUtil.java

public static String encrypt(String data, PrivateKey pri_key) throws Exception {
    // Cipher??//  www. j  a  v  a2  s. c  om
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, pri_key);
    byte[] results = cipher.doFinal(data.getBytes());
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

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

/**
 * Decrypts a byte[] encrypted by {@link #encrypt} method.
 *
 * @param c   The encrypted HEX byte[]./* ww  w .j  a v  a 2  s  . c o m*/
 * @param key The key.
 * @return The decrypted string in a byte[].
 */
public static byte[] decrypt(byte[] c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        return cipher.doFinal(Hex.decodeHex((new String(c).toCharArray())));
    } catch (Exception e) {
        logger.warn("Could not decrypt byte[]", e);
        return null;
    }
}

From source file:com.sshutils.utils.CryptHelper.java

public static String decrypt(String strToDecrypt) {
    try {/*w w w  .jav a  2  s .  c o  m*/

        //                for (Provider provider: Security.getProviders()) {
        //  log.info(provider.getName());
        //  for (String key: provider.stringPropertyNames())
        //   log.info("\t" + key + "\t" + provider.getProperty(key));
        //}

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)));
        return decryptedString;
    } catch (Exception e) {
        log.error("Error while decrypting", e);

    }
    return null;
}

From source file:com.example.license.RSAUtil.java

/**
 * ?/*from  w w w . j  a  v a  2 s.c o m*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String encrypt(String data, String seed) throws Exception {
    KeyPair keyPair = generatorKeyPair(seed);
    // Cipher??
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate());
    byte[] results = cipher.doFinal(data.getBytes());
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

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

/**
 * Decrypts a string encrypted by {@link #encrypt} method.
 *
 * @param c   The encrypted HEX string.//from w  ww  .j a v a2s  .c  om
 * @param key The  key.
 * @return The decrypted string.
 */
public static String decrypt(String c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decoded = cipher.doFinal(Hex.decodeHex(c.toCharArray()));
        return new String(decoded);
    } catch (Exception e) {
        logger.warn("Could not decrypt string", e);
        return null;
    }
}