Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

In this page you can find the example usage for javax.crypto Cipher getInstance.

Prototype

public static final Cipher getInstance(String transformation, Provider provider)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:authentication.AES.java

public static String encrypt(String textopuro) throws Exception {
    Cipher encripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES");
    encripta.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] a = encripta.doFinal(textopuro.getBytes("UTF-8"));
    return StringUtils.newStringUtf8(Base64.encodeBase64(a, false));
}

From source file:Main.java

/**
 * Constructs a new Cipher./*from  w ww.j av  a  2  s . co  m*/
 */
public static Cipher newCipher(String algorithm, String provider) {
    try {
        return Cipher.getInstance(algorithm, provider);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Not a valid encryption algorithm", e);
    } catch (NoSuchPaddingException e) {
        throw new IllegalStateException("Should not happen", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("Not a valid encryption provider", e);
    }
}

From source file:Main.java

static String encryptRsaB64(byte[] bytes, Key pubRsaKey) {
    try {//from w ww  .j a v  a 2  s .c o  m
        Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
        // cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, new SecureRandom());
        SecureRandom sc = null;
        cipher.init(Cipher.ENCRYPT_MODE, pubRsaKey, sc);
        byte[] cipherText = cipher.doFinal(bytes);
        return encodeB64(cipherText);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

static byte[] decryptRsaB64(String s64, Key privRsaKey) {
    try {/*from   w w  w.ja  va  2 s .  c  o m*/
        byte[] sBytes = decodeB64(s64);
        if (sBytes != null) {
            Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
            cipher.init(Cipher.DECRYPT_MODE, privRsaKey);
            return cipher.doFinal(sBytes);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.aspose.showcase.qrcodegen.web.api.util.StringEncryptor.java

public static String encrypt(String data, String password) throws Exception {

    Security.addProvider(new BouncyCastleProvider());
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");

    final Random r = new SecureRandom();
    byte[] salt = new byte[SALT_SIZE];
    r.nextBytes(salt);/*from ww w  .ja  v a 2  s .  c o m*/

    SecretKeyFactory fact = SecretKeyFactory.getInstance("PBEWITHMD5AND128BITAES-CBC-OPENSSL", "BC");

    cipher.init(Cipher.ENCRYPT_MODE,
            fact.generateSecret(new PBEKeySpec(password.toCharArray(), salt, PBE_KEY_SALE_SIZE)));

    byte[] encVal = cipher.doFinal(data.getBytes());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // writing encrypted data along with the salt in the format readable by
    // open ssl api
    bos.write("Salted__".getBytes());
    bos.write(salt);
    bos.write(encVal);
    String encryptedValue = new String(Base64.encode(bos.toByteArray()));
    bos.close();

    return encryptedValue;

}

From source file:authentication.AES.java

public static String decrypt(String textoencriptado) throws Exception {
    Cipher decripta = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(chaveencriptacao.getBytes("UTF-8"), "AES");
    decripta.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    return new String(decripta.doFinal(Base64.decodeBase64(textoencriptado)), "UTF-8");
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java

private static byte[] encryptData(byte[] content) {
    byte[] outBytes = null;

    try {//from  ww  w .  ja  v  a2  s  . c om
        Key key = new SecretKeySpec(secret_key, "DESede");

        Cipher cipher = Cipher.getInstance("DESede", "SunJCE");

        cipher.init(Cipher.ENCRYPT_MODE, key, cipher.getParameters());

        outBytes = cipher.doFinal(content);

    } catch (Exception ex) {
        //log.error("PacketUtil",ex.getMessage(), ex);
        return outBytes;
    }

    return outBytes;
}

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

public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);//from  w ww .j  a v a  2  s . c om
    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.sshutils.utils.CryptHelper.java

public static String encrypt(String strToEncrypt) {
    try {//from   w w w  .  ja v a2s  .c om

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        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:de.serverfrog.pw.crypt.SerpentUtil.java

public static CipherOutputStream getOutputStream(OutputStream os, Key key) {
    try {//from w  w w  .j a  v a 2  s  .  c  o m
        Cipher instance = Cipher.getInstance("Serpent", "BC");
        instance.init(Cipher.ENCRYPT_MODE, key);
        return new CipherOutputStream(os, instance);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException
            | NoSuchPaddingException ex) {
        Logger.getLogger(SerpentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}