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:Main.java

private static byte[] getEncCode(String key, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;
    try {/*from   www. j  a  va 2  s  . c  om*/
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, newDesInstance(key));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:Main.java

public static byte[] TDesDec(byte[] key, byte[] in) throws Exception {

    Key deskey = null;/*from  w  w  w  . j  ava 2s  .  c  om*/
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:Main.java

public static byte[] TDesEnc(byte[] key, byte[] in) throws Exception {

    Key deskey = null;/*ww  w .ja v  a2s .  c o m*/
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:Main.java

/** 
 * decrypt /* w w w  .  ja v  a2  s.c om*/
 * @param data prepare to be decrypted 
 * @param key byteArray[] key
 * @return byte[] original data 
 * */
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {

    Key k = byteArrayKeyToSecurityKey(key);

    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //init, decrypt mode
    cipher.init(Cipher.DECRYPT_MODE, k);
    //exec 
    return cipher.doFinal(data);
}

From source file:Main.java

private static byte[] Rsa(byte[] byteData, Key pKey, int opmode) throws Exception {
    Cipher cipher = null;
    try {//from w  ww  .  j  av a  2s .  com
        cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(opmode, pKey);

        return cipher.doFinal(byteData);
    } finally {
        cipher = null;
    }
}

From source file:Main.java

private static byte[] getEncCode(Context mContext, byte[] byteS) {
    byte[] byteFina = null;
    Cipher cipher;
    try {//from  w  w w.  j a  va 2  s.  c  o m
        cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, getKey(mContext));
        byteFina = cipher.doFinal(byteS);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }
    return byteFina;
}

From source file:com.basp.trabajo_al_minuto.model.business.BusinessSecurity.java

/**
 * Se encarga de encriptar la contrasea ingresada por el usuario *
 *///from   w ww. j av  a  2 s.  c o  m
public static String encrypt(String value) throws BusinessException {
    String secretKey = "e-business";
    String base64EncryptedString = "";
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainTextBytes = value.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
        throw new BusinessException(ex);
    }
    return base64EncryptedString;
}

From source file:Main.java

public static byte[] getRSAEncryptedData(byte[] dataWithHash) {
    BigInteger modulus = new BigInteger(
            "C150023E2F70DB7985DED064759CFECF0AF328E69A41DAF4D6F01B538135A6F91F8F8B2A0EC9BA9720CE352EFCF6C5680FFC424BD634864902DE0B4BD6D49F4E580230E3AE97D95C8B19442B3C0A10D8F5633FECEDD6926A7F6DAB0DDB7D457F9EA81B8465FCD6FFFEED114011DF91C059CAEDAF97625F6C96ECC74725556934EF781D866B34F011FCE4D835A090196E9A5F0E4449AF7EB697DDB9076494CA5F81104A305B6DD27665722C46B60E5DF680FB16B210607EF217652E60236C255F6A28315F4083A96791D7214BF64C1DF4FD0DB1944FB26A2A57031B32EEE64AD15A8BA68885CDE74A5BFC920F6ABF59BA5C75506373E7130F9042DA922179251F",
            16);/*w w w .  j  a v  a 2  s.  com*/
    BigInteger pubExp = new BigInteger("010001", 16);

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
        RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherData = cipher.doFinal(dataWithHash);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeyException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (BadPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (InvalidKeySpecException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (NoSuchPaddingException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    return null;
}

From source file:com.wso2telco.proxy.util.DecryptAES.java

public static String decrypt(String encryptedText) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException, ConfigurationException {
    if (encryptionKey != null) {
        byte[] encryptionKeyByteValue = encryptionKey.getBytes();
        SecretKey secretKey = new SecretKeySpec(encryptionKeyByteValue, AuthProxyConstants.ASE_KEY);
        String decryptedText = null;
        if (encryptedText != null) {
            byte[] encryptedTextByte = Base64.decodeBase64(encryptedText);
            Cipher cipher = Cipher.getInstance("AES");

            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
            decryptedText = new String(decryptedByte);
        }//from   w  w w  .  j a  va2s .  co m
        return decryptedText;
    } else {
        throw new ConfigurationException("MSISDN EncryptionKey could not be found in mobile-connect.xml");
    }
}

From source file:love.sola.netsupport.util.RSAUtil.java

public static String encrypt(String value) {
    try {/*  w w w  . j  av a2 s.c  o  m*/
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}