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.jopss.logico.negocio.util.CriptoUtils.java

public static String desDecode(String texto, String chave) {
    Cipher dcipher;
    SecretKey key;//  w w w.  j  a  va  2 s.c o m
    String decod = null;

    try {
        key = new SecretKeySpec(chave.getBytes(), 0, 8, "DES");
        dcipher = Cipher.getInstance("DES");
        dcipher.init(Cipher.DECRYPT_MODE, key);
        byte[] dec = Hex.decodeHex(texto.toCharArray());
        byte[] utf8 = dcipher.doFinal(dec);
        decod = new String(utf8, "UTF8");
    } catch (Exception e) {
        log.error(e);
    }

    return decod;
}

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * //from  w  w w.j a v a 2 s  .  com
 * This method is used for encrypt by using Algorithm - AES
 * 
 * @param String
 * @return String
 * @throws Exception
 */
public static String encryptAES(String data) throws Exception {

    Key key = generateKeyAES(); // step 1

    Cipher c = Cipher.getInstance(AES_ALGO); // step 2
    c.init(Cipher.ENCRYPT_MODE, key); // step 3

    byte[] encVal = c.doFinal(data.getBytes()); // step 4
    String encryptedValue = new BASE64Encoder().encode(encVal); // step 5

    return encryptedValue;
}

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * //w w  w  .  j a  va2  s.  c  om
 * This method is used for decrypt by using Algorithm - AES
 * 
 * @param String
 * @return String
 * @throws Exception
 */
public static String decryptAES(String encryptedData) throws Exception {

    Key key = generateKeyAES(); // step 1

    Cipher c = Cipher.getInstance(AES_ALGO); // step 2
    c.init(Cipher.DECRYPT_MODE, key); // step 3

    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); // step
    // 4
    byte[] decValue = c.doFinal(decordedValue); // step 5
    return new String(decValue);
}

From source file:de.marius_oe.cfs.cryption.Crypter.java

/**
 * Returns the {@link Cipher} for the encryption and decryption process.
 * //from ww w.j a  v  a 2 s.  c  o m
 * @param mode
 *            {@link Cipher.DECRYPT_MODE} or {@link Cipher.ENCRYPT_MODE}
 * @param iv
 *            the initial vector which should be used in the cipher. if the
 *            iv is <code>null</code>, a new iv will be generated.
 * @return {@link Cipher} object
 */
private static Cipher getCipher(int mode, byte[] iv) {
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        if (iv == null) {
            cipher.init(mode, KeyManager.instance().getKey());
        } else {
            IvParameterSpec parameterSpec = new IvParameterSpec(iv);
            cipher.init(mode, KeyManager.instance().getKey(), parameterSpec);
        }
        return cipher;
    } catch (InvalidKeyException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java

public static String cypher(byte[] sessionKey, String informationToCipher) {
    Cipher cipher;
    try {//ww  w.j av a  2 s  . c  om
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"));
        byte[] cipherData = cipher.doFinal(informationToCipher.getBytes("UTF-8"));
        return Base64.encodeBase64String(cipherData);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:kr.co.exsoft.eframework.util.LicenseUtil.java

/**
 * .//from  w  ww  .j  av a  2 s  . c o  m
 * 
 * @param word
 * @param key
 * @return String
 * @throws NoSuchPaddingException 
 * @throws NoSuchAlgorithmException 
 */
private static String spell(String word, Key key) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] cleartext = word.getBytes();
    byte[] ciphertext = cipher.doFinal(cleartext);

    return getString(ciphertext);
}

From source file:kr.co.exsoft.eframework.util.LicenseUtil.java

/**
 * ./*w w w  .  jav  a  2  s  .c o  m*/
 * 
 * @param word
 * @param key
 * @return String
 */
private static String unspell(String word, Key key) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);

    byte[] ciphertext = getBytes(word);
    byte[] cleartext = cipher.doFinal(ciphertext);

    return new String(cleartext);
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * DES bytKey8?// w  w w  .j  ava2s  .  co  m
 * 
 * @param bytP
 * @param bytKey
 * @return
 * @throws Exception
 */
public static byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception {
    DESKeySpec dks = new DESKeySpec(bytKey);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey sk = skf.generateSecret(dks);
    Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
    cip.init(Cipher.ENCRYPT_MODE, sk);
    return cip.doFinal(bytP);
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * DES bytKey8?/*ww w.  j  a  va2s.  c  o  m*/
 * 
 * @param bytE
 * @param bytKey
 * @return
 * @throws Exception
 */
public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception {
    DESKeySpec desKS = new DESKeySpec(bytKey);
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey sk = skf.generateSecret(desKS);
    Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
    cip.init(Cipher.DECRYPT_MODE, sk);
    return cip.doFinal(bytE);
}

From source file:com.jopss.logico.negocio.util.CriptoUtils.java

public static String desEncode(String texto, String chave) {
    Cipher ecipher;
    SecretKey key;//from w ww  . ja va 2 s  .  c  om
    String encod = null;
    try {
        key = new SecretKeySpec(chave.getBytes("UTF-8"), 0, 8, "DES");
        ecipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] utf8 = texto.getBytes("UTF8");
        byte[] crip = ecipher.doFinal(utf8);
        encod = new String(Hex.encodeHex(crip));

    } catch (Exception e) {
        log.error(e);
    }

    return encod;
}