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.juicioenlinea.application.secutiry.Security.java

public static String desencriptar(String textoEncriptado) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {/* w w  w.j  av a  2 s  .  c om*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("UTF-8"));
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        encryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }

    return encryptedString;
}

From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java

public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {//from   ww w  .  ja  v a2  s  . c o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        byte[] encr64 = Base64.encodeBase64(encryptedData);

        //System.out.println(new String(encr64));

        return new String(encr64);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java

public static String decrypt(String key, String data) {
    byte[] decryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {//from  w w w  . j a  v a  2  s .  c o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        //decryptedData = cipher.doFinal(Base64.decode(data));

        decryptedData = cipher.doFinal(Base64.decodeBase64(data));

        //return new String(decryptedData);

        return new String(decryptedData);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:net.duckling.ddl.web.agent.util.AuthUtil.java

private static String decodeAuth(String auth) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    SecretKeySpec spec = new SecretKeySpec(getKey(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] result = cipher.doFinal(Base64.decodeBase64(auth));
    return new String(result, "UTF-8");
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String decifrarMD5(String textoEncriptado) throws Exception {
    String base64EncryptedString = "";

    try {// www.  j a v  a 2  s .  c om
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);

        Cipher decipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
        throw ex;
    }
    return base64EncryptedString;
}

From source file:gsn.http.ac.Protector.java

public static String encrypt(String value) throws Exception {
    logger.debug("Encrypt key");
    Key key = generateKey();/* ww  w .  ja  v a  2 s  .  c om*/
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);

    String valueToEnc = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        eValue = new sun.misc.BASE64Encoder().encode(encValue);
        //eValue = Base64.encodeBase64String(encValue);
    }
    return eValue;
}

From source file:gsn.http.ac.Protector.java

public static String decrypt(String value) throws Exception {
    Key key = generateKey();//from   w  ww . jav  a 2  s .  c om
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);

    String dValue = null;
    String valueToDecrypt = value;
    for (int i = 0; i < ITERATIONS; i++) {
        byte[] decordedValue = new sun.misc.BASE64Decoder().decodeBuffer(valueToDecrypt);
        //byte[] decordedValue = Base64.decodeBase64(valueToDecrypt);
        byte[] decValue = c.doFinal(decordedValue);
        dValue = new String(decValue).substring(salt.length());
        valueToDecrypt = dValue;
    }
    return dValue;
}

From source file:com.dragoniade.encrypt.EncryptionHelper.java

private static Cipher getDecrypter(String seed, String algorithm) {
    try {/*  w w  w  .j  a va  2  s.  co m*/
        byte[] byteKey = getSeed(seed);
        SecretKeySpec keySpec = new SecretKeySpec(byteKey, algorithm);
        Cipher decrypt = Cipher.getInstance(algorithm);
        decrypt.init(Cipher.DECRYPT_MODE, keySpec);
        return decrypt;
    } catch (Exception e) {
        return null;
    }

}

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

/**
 * ?/*  w  ww  . j  a  v  a  2  s.  c om*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decryptBase64(String data, String key) throws Exception {
    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, securekey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes())));
}

From source file:com.lingxiang2014.util.RSAUtils.java

public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
    Assert.notNull(privateKey);//from ww  w. ja v a  2 s .c o m
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        return null;
    }
}