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)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:Main.java

/**
 * RSA encryption//  w  ww .  j a v a  2  s  .  c o  m
 * @param pubKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] encryptRSA(RSAPublicKey pubKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    return cipher.doFinal(message);
}

From source file:clases.Seguridad.java

public static String encriptar(String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(CI);
    SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), ALG);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:Main.java

/**
 * RSA decryption//from  ww  w  . j av  a 2  s.  c  o  m
 * @param priKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] decryptRSA(RSAPrivateKey priKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, priKey);

    return cipher.doFinal(message);
}

From source file:com.fpt.crypto.CipherDemo.java

public static String encrypt(String in, String key) throws InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] inputBytes = in.getBytes();
    SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secKey);
    byte[] outputBytes = cipher.doFinal(inputBytes);
    return Hex.encodeHexString(outputBytes);
}

From source file:Controller.StringEncrypt.java

public final static String encrypt(String cleartext) {
    byte[] encrypted = null;
    try {/*  w ww  .  j a v a 2s  .c  o m*/
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec skeySpec = new SecretKeySpec("92AE31A79FEEB2A3".getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec("0123456789ABCDEF".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
        encrypted = cipher.doFinal(cleartext.getBytes());
    } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException
            | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
        JOptionPane.showMessageDialog(null, "Error encriptando contrasea");
    }
    return new String(encodeBase64(encrypted));
}

From source file:Main.java

public static byte[] encryptMsg(String message) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException,
        UnsupportedEncodingException {
    if (message == null) {
        return new byte[0];
    }//from w  ww  .j av  a2 s .c o  m
    /* Encrypt the message. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
    return cipherText;
}

From source file:MainClass.java

private static String encrypt(char[] password, String plaintext) throws Exception {

    byte[] salt = new byte[8];
    Random random = new Random();
    random.nextBytes(salt);/* w ww  . ja va2s.co m*/

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext.getBytes());

    BASE64Encoder encoder = new BASE64Encoder();

    String saltString = encoder.encode(salt);
    String ciphertextString = encoder.encode(ciphertext);

    return saltString + ciphertextString;
}

From source file:Main.java

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

From source file:com.avbravo.avbravoutils.crypto.Encriptador.java

public static String encrypt(String key, String cleartext) throws Exception {

    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:EncDec.AES.java

public static String encrypt(String key, String initVector, String value) {
    try {/*  ww w  .ja va  2s  . com*/
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        //System.out.println("encrypted string: "
        //        + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}