Example usage for javax.crypto Cipher ENCRYPT_MODE

List of usage examples for javax.crypto Cipher ENCRYPT_MODE

Introduction

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

Prototype

int ENCRYPT_MODE

To view the source code for javax.crypto Cipher ENCRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to encryption mode.

Usage

From source file:Main.java

/**
 * RSA encryption/*w  w w  .j  a  va2  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:Main.java

public static byte[] encryptMsg(String message) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException,
        UnsupportedEncodingException {
    if (message == null) {
        return new byte[0];
    }// w w  w.  j  a v  a2s.  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);/*from  www.j ava 2 s. c  o 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:EncDec.AES.java

public static String encrypt(String key, String initVector, String value) {
    try {//from w w  w  .java 2s.  co  m
        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;
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

public static String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();/*  ww  w.j a  v  a 2s  .c  om*/
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = new Base64().encodeToString(encValue);
    return encryptedValue;
}

From source file:Main.java

public static String enCrypto(String txt, String key) {

    if (txt != null && !"".equals(txt) && !"null".equals(txt)) {

        try {/*from www . j av  a2s.com*/

            StringBuffer sb = new StringBuffer();
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
            SecretKeyFactory skeyFactory = null;
            Cipher cipher = null;
            try {
                skeyFactory = SecretKeyFactory.getInstance("DES");
                cipher = Cipher.getInstance("DES");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byte[] cipherText = cipher.doFinal(txt.getBytes());
            for (int n = 0; n < cipherText.length; n++) {
                String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF));
                if (stmp.length() == 1) {
                    sb.append("0" + stmp);
                } else {
                    sb.append(stmp);
                }
            }
            return sb.toString().toUpperCase(Locale.US);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    return null;
}

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

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

    Key deskey = null;//from  w w  w.j  a v  a2s .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.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

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:br.com.aws.odonto.utils.CipherUtil.java

public static synchronized String crypt(String str) throws Exception {
    String strCrypt = null;// w w w. j a  va 2s.c  o m
    try {
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedData = cipher.doFinal(str.getBytes());
        //System.out.println("cipher encrypt: " + new String(encryptedData));
        strCrypt = base64.encodeToString(encryptedData);
        //System.out.println("   base64 encode: " + strCrypt);
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return strCrypt;
}