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

public static String encode(String key, String cleartext) throws Exception {
    SecretKeySpec secretKeySpec = createKey(key);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
    String result = parseByte2HexStr(c.doFinal(cleartext.getBytes("UTF-8")));

    return result;
}

From source file:Main.java

public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception {

    Key deskey = null;//from   w w w  . j  av a 2 s. com
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:Main.java

public static byte[] encryptAES(SecretKey sKey, byte[] message) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE, sKey);
    return c.doFinal(message);
}

From source file:Main.java

public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {

    Key deskey = null;//from   w ww.  j ava 2  s.com
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(keyiv);
    cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:Main.java

public static byte[] encrypt(byte[] input, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    return cipher.doFinal(input);
}

From source file:Main.java

public static byte[] getEncCode(byte[] byteE, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;// w w w. java2  s .c om

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");

        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteE);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:Main.java

public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {/*from  w  ww .j a va2 s.  co  m*/
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
    } catch (Exception e) {
        Log.e(TAG, "Error during encryption: " + e.toString());
        return errorbyte;
    }
}

From source file:Main.java

private static byte[] encrypt(String input)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] inputBytes = input.getBytes();
    return cipher.doFinal(inputBytes);
}

From source file:Main.java

public static boolean encryptFile(byte[] key, byte[] iv, String sourceFilePath, String destFilePath) {
    return handleFile(Cipher.ENCRYPT_MODE, key, iv, sourceFilePath, destFilePath);
}

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

public static String encrypt(String strToEncrypt) {
    try {/* w  w  w  . j av  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;

}