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

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(clear);
}

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key, byte[] iv, String cipherAlgotirhm) {
    try {//from   w ww  .  j  a va 2s.co m
        Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, key, iv, cipherAlgotirhm);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:encryptdecrypt.util.Security.java

public static String encrypt(String strToEncrypt) {
    try {/*  w ww.j a  v a  2  s .  c  om*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes())));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Logic.security.java

public static String symmetricEncrypt(String text, String secretKey) {
    byte[] raw;//from   w w  w . j a v  a  2 s  . co  m
    String encryptedString;
    SecretKeySpec skeySpec;
    byte[] encryptText = text.getBytes();
    Cipher cipher;
    try {
        raw = Base64.decodeBase64(secretKey);
        skeySpec = new SecretKeySpec(raw, "AES");
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText));
    } catch (Exception e) {
        e.printStackTrace();
        return "Error";
    }
    return encryptedString;
}

From source file:licenceexecuter.Encryptor.java

public static String encrypt(String key, String initVector, String value) {
    try {/*from   ww  w.j  a v a  2  s .c o 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());
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] encryptMessage(String key, String input) throws Exception {
    if (checkNull(input)) {
        return null;
    }/*from   w  w w  . j a  va  2s .c  om*/
    try {
        return doFinal(key, Cipher.ENCRYPT_MODE, input.getBytes());
    } catch (Exception e) {
        throw new Exception("201");
    }
}

From source file:Main.java

public static byte[] encrypt(String clearText) {
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = null;/*from  w ww  .  j  a v a 2  s . co m*/
    byte[] cipherText = null;

    try {
        // init cipher
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        cipherText = cipher.doFinal(clearText.getBytes());
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    return Base64.encode(cipherText, 10);
}

From source file:Main.java

public static String encryption(String string) throws Exception {
    // TODO Auto-generated method stub

    //generate symmetric key
    KeyGenerator keygt = KeyGenerator.getInstance("AES");
    keygt.init(128);/*from   ww  w  . jav  a  2s.  c  om*/
    SecretKey symkey = keygt.generateKey();

    //get it encoded
    byte[] aes_ba = symkey.getEncoded();

    //create cipher
    SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    //encryption
    byte[] EncSymbyteArray = cipher.doFinal(string.getBytes());

    //encrypt symKey with PublicKey
    //        Key pubKey = getPublicKey();

    Key pubKey = publicKey;

    //RSA cipher
    Cipher cipherAsm = Cipher.getInstance("RSA", "BC");
    cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey);

    //RSA encryption
    byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba);

    //           File f3 = new File(BASE_PATH,"enc.txt");
    //           File f3key = new File(BASE_PATH,"enckey.txt");
    //           File f3file = new File(BASE_PATH,"encfile.txt");
    //           writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray);

    //byte != new String
    //return new String(byteMerger(asymEncsymKey, EncSymbyteArray));
    return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT);

}

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

/**
 * Encrypt data/*www  . j  ava  2s . co m*/
 * @param secretKey   -   a secret key used for encryption
 * @param data      -   data to encrypt
 * @return   Encrypted data
 * @throws Exception
 */
public static String cipher(String secretKey, String data) throws Exception {
    // Key has to be of length 8
    if (secretKey == null || secretKey.length() != 8)
        throw new Exception("Invalid key length - 8 bytes key needed!");

    SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return toHex(cipher.doFinal(data.getBytes()));
}