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.vico.license.util.rsa.RSAdoDecrypt.java

public static String decrypt(String cryptograph) throws Exception {
    Key privateKey;/*from   www.  j  a va 2 s . c  o  m*/
    String path = Thread.currentThread().getContextClassLoader().getResource("/").toURI().getPath();
    ObjectInputStream ois = null;
    try {
        /** ? */
        ois = new ObjectInputStream(new FileInputStream(path + FileNames.PRIVATEKEY_NAME));
        privateKey = (Key) ois.readObject();
    } catch (Exception e) {
        throw e;
    } finally {
        ois.close();
    }

    /** Cipher?RSA */
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, privateKey);

    /** ? */
    byte[] b1 = Base64.decodeBase64(cryptograph);
    /** ? */
    byte[] b = cipher.doFinal(b1);
    return new String(b);
}

From source file:controlpac.EncryptHelper.java

public static String Encriptar(String texto) {
    String base64EncryptedString = "";
    try {/*from ww  w.  j  av  a  2 s.c  o  m*/
        MessageDigest md = MessageDigest.getInstance("MD5"); //Crea un hash con la clave elegida
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede"); //Crea la clave
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);//Inicializa el cifrado con la clave
        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);//Cifra el texto
        byte[] base64Bytes = Base64.encodeBase64(buf);//Encodea el texto en base64
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:cn.lynx.emi.license.GenerateLicense.java

private static final String encrypt(String key, String data) {
    byte[] corekey = Base64.decodeBase64(key);

    PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey);

    try {//from   www . j a va 2  s . c  o  m
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        Key privateKey = keyFactory.generatePrivate(pkspec);

        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        byte[] encData = cipher.doFinal(data.getBytes("UTF-8"));
        System.out.println("after encrypt, len=" + encData.length);
        return Base64.encodeBase64String(encData);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:controlpac.EncryptHelper.java

public static String Desencriptar(String textoEncriptado) {
    String base64EncryptedString = "";
    try {/* w  w w .j ava2 s . c o  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));//Desencodea el texto en base64
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8")); //Crea un hash con la clave elegida
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");//Crea la clave 
        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key); //Inicia el descifrado
        byte[] plainText = decipher.doFinal(message);//Descifra el texto
        base64EncryptedString = new String(plainText, "UTF-8");
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:Main.java

public static String decryptData(String encryptBase64Data, String keyBase64) {
    SecretKeySpec key = new SecretKeySpec(Base64.decode(keyBase64, Base64.DEFAULT), "DES");
    Cipher decipher = null;
    try {/* ww w .jav  a2s .  co  m*/
        decipher = Cipher.getInstance("DES");
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptData = Base64.decode(encryptBase64Data, Base64.DEFAULT);
        return new String(decipher.doFinal(encryptData), "UTF8");
    } catch (Exception e) {
    }
    return null;
}

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

public static String encrypt(String Data) throws Exception {
    Key key = generateKey();/*from www.  j  a v a 2 s  .c  o m*/
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java

public static String generateKey(String toEncode) {

    String encoded = "";
    try {//  w  w w. j a v a  2s . c o m
        byte[] saltEncrypt = SALT_ENCRYPT.getBytes();
        SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp = factoryKeyEncrypt.generateSecret(
                new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH));
        SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
        Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER);
        aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey);
        byte[] bytes = StringUtils.getBytesUtf8(toEncode);
        byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes);
        encoded = Base64.encodeBase64URLSafeString(encryptBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * /*from w w w .j a v a  2 s. c o m*/
 * 
 * @param data  ?
 * @return  ?
 */
public static String decrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes()));
        return new String(d);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * /*from w  w  w  .j  a va2 s . c  om*/
 * 
 * @param data  ?
 * @return  ?
 */
public static String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        byte e[] = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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

public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();//from ww w  .j a va  2s.c  om
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}