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 byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    try {/*from w  ww.j  ava 2  s. c  om*/
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void genMessageCiphers(char[] pass) {
    try {//from w  w w. ja v  a  2s. com
        messageEncryptionCipher = genCipher(pass, messageSalt, KEY_FACTORY, ITERATION_COUNT,
                Cipher.ENCRYPT_MODE);
        messageDecryptionCipher = genCipher(pass, messageSalt, KEY_FACTORY, ITERATION_COUNT,
                Cipher.DECRYPT_MODE);
    } catch (Exception e) {
        Log.d(LOG_TAG, "genStorageCiphers", e);
    }
}

From source file:Main.java

public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {/*  w  w w .  ja  v a  2 s  .  co  m*/
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.scrubstudios.srvmon.notificator.classes.Crypt.java

public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {// ww  w  .j  a va  2  s  .c  om
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        encryptedData = cipher.doFinal(data.getBytes());

        byte[] encr64 = Base64.encodeBase64(encryptedData);

        //System.out.println(new String(encr64));

        return new String(encr64);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Crypt.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);// w w w .jav  a  2  s.  com
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:Main.java

private static byte[] encryptWithPassphrase(byte[] encryptionSalt, int iterations, byte[] data,
        String passphrase) throws GeneralSecurityException {
    Cipher cipher = getCipherFromPassphrase(passphrase, encryptionSalt, iterations, Cipher.ENCRYPT_MODE);
    return cipher.doFinal(data);
}

From source file:DesEncrypter.java

DesEncrypter(SecretKey key) throws Exception {
    ecipher = Cipher.getInstance("DES");
    dcipher = Cipher.getInstance("DES");
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);
}

From source file:Main.java

/**
 * Computes an Ephemeral ID.//from  w  w  w . j  a  v  a  2  s . c o  m
 * @param key                 AES key (Advertiser Identity Key). The first 16 bytes are used.
 * @param timeCounter         Advertiser time counter
 * @param rotationExponent    Advertiser rotation exponent (0 to 15)
 * @return Final ephemeral key of 16 bytes, of which only the first 8 bytes should be used.
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
@NonNull
public static byte[] computeEID(byte[] key, int timeCounter, byte rotationExponent)
        throws GeneralSecurityException {
    //        String transformation = "AES/CBC/PKCS5Padding";
    String transformation = "AES/ECB/NoPadding";
    @SuppressLint("GetInstance")
    // spec says it has to be ECB, ignore lint warning
    Cipher aes = Cipher.getInstance(transformation);
    aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, 16, "AES"));

    byte[] tempKey = aes
            .doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
                    0x00, 0x00, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff) });

    // clear K lowest bits
    timeCounter = timeCounter >>> rotationExponent << rotationExponent;

    // reset cipher with a new encryption key
    aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(tempKey, "AES"));
    return aes.doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            rotationExponent, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff),
            (byte) ((timeCounter >>> 8) & 0xff), (byte) (timeCounter & 0xff) });
}

From source file:Main.java

public 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 byte[] TriDesEncryption(byte[] byteKey, byte[] dec) {
    try {/*from ww  w .j  a va 2 s  .co m*/
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKeySpec key = new SecretKeySpec(en_key, "DESede");
        Cipher ecipher = Cipher.getInstance("DESede/ECB/NoPadding");
        ecipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] en_b = ecipher.doFinal(dec);
        return en_b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}