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.sysfore.pos.licensemanagement.LicenceManagementUtil.java

public static String encrypt(String strToEncrypt) {
    try {/*w  ww  . j  a  v  a  2 s.  c  o m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:com.sysfore.pos.licensemanagement.LicenceManagementUtil.java

public static String decrypt(String strToDecrypt) {
    try {/*  w ww  .  j  ava2 s.co m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        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:Componentes.EncryptionMD5.java

public static String Desencriptar(String encriptado) {
    System.out.println(encriptado);
    String secretKey = "qualityinfosolutions"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*w w w .  j av  a  2 s. c o m*/
        System.out.println("h");
        byte[] message = Base64.decodeBase64(encriptado.getBytes("utf-8"));
        System.out.println("b");
        MessageDigest md = MessageDigest.getInstance("MD5");
        System.out.println("a");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        System.out.println("c");
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        System.out.println("g");
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        System.out.println("w");
        Cipher decipher = Cipher.getInstance("DESede");
        System.out.println("t");
        decipher.init(Cipher.DECRYPT_MODE, key);
        System.out.println("r");
        System.out.println(Arrays.toString(message));
        byte[] plainText = decipher.doFinal(message);
        System.out.println(Arrays.toString(plainText));
        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return base64EncryptedString;
}

From source file:com.credomatic.gprod.db2query2csv.Security.java

/**
 * Descifra una cadena de caracteres apartir de la llave de cifrado y retorna le valor original.
 * @param key llave generada durante el cifrado de la cadena original
 * @param value cadena cifrda// w  ww.  j a  va 2s. c  o m
 * @return cadena descifrada
 */
public static String decrypt(String key, String value) {
    try {
        Key k = new SecretKeySpec(new Base64().decode(key), "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, k);
        byte[] decodedValue = new Base64().decode(value);
        byte[] decValue = c.doFinal(decodedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.iterzp.momo.utils.RSAUtils.java

/**
 * /*from w  ww .  j a va2 s.c  o m*/
 * 
 * @param publicKey
 *            
 * @param data
 *            ?
 * @return ??
 */
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA", PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * //from   ww w . j  a  v a  2 s .c o m
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String encode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte e[] = cipher.doFinal(data.getBytes("UTF-8"));
        return replaceChar(Base64.encodeBase64String(e));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (UnsupportedEncodingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * //from w w w.j  a va2s  . c  o m
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String decode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes("UTF-8")));
        return replaceChar(new String(d));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IOException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}

From source file:Main.java

private static byte[] Aes(byte[] byteData, byte[] byteKey, int opmode) throws Exception {
    Cipher cipher = null;
    try {/*from w w  w .j  a  v a  2  s. c  om*/
        SecretKeySpec aesKey = new SecretKeySpec(byteKey, "AES");
        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(opmode, aesKey);

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

From source file:club.jmint.crossing.specs.Security.java

public static String desDecrypt(String data, String key) throws CrossException {
    String ret = null;// w  ww.j a  v a 2 s.c  o  m
    try {
        DESKeySpec desKey = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);

        Cipher cipher = Cipher.getInstance(CIPHER_DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, securekey);

        ret = new String(cipher.doFinal(Base64.decodeBase64(data)));
    } catch (Exception e) {
        CrossLog.printStackTrace(e);
        throw new CrossException(ErrorCode.COMMON_ERR_DECRYPTION.getCode(),
                ErrorCode.COMMON_ERR_DECRYPTION.getInfo());
    }
    return ret;
}

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Encryption /*from w  w  w  .  j a v  a 2s . c o m*/
 * @param content content to be encrypted
 * @param encryptKey encryption key
 * @return 
 * @throws Exception 
 */
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}