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:com.intera.roostrap.util.EncryptionUtil.java

public static void encrypt(String encryptionKey, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    encryptOrDecrypt(encryptionKey, Cipher.ENCRYPT_MODE, is, os);
}

From source file:Main.java

public static String encryptData(String password, String plaintextData) throws Exception {
    // Thank you Mr. Nelenkov
    String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html";
    Log.v(TAG, maybeThisHelps);//www .ja v a 2  s  .c o  m
    int iterationCount = 100; //because Polaroid
    int keyLength = 256;
    int saltLength = keyLength;

    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[saltLength];
    random.nextBytes(salt);
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = new byte[cipher.getBlockSize()];
    random.nextBytes(iv);

    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
    byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8"));

    String ivToString = new String(Base64.encode(iv, 0));
    String saltToString = new String(Base64.encode(salt, 0));
    String ciphertextToString = new String(Base64.encode(ciphertext, 0));

    Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString);
    return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", "");
}

From source file:com.netsteadfast.greenstep.util.EncryptorUtils.java

public static String encrypt(String key1, String iv1, String value) {
    try {/*ww  w. j a  v a  2  s . co m*/
        IvParameterSpec iv = new IvParameterSpec(iv1.getBytes(Constants.BASE_ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes(Constants.BASE_ENCODING), "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:de.uzk.hki.da.passwordEncryptor.passwordEncryptor.java

private static String encryptPasswordForContentBroker(String password) {

    byte key[] = "394z57f4".getBytes();
    byte encryptedPassword[];

    try {/*from  ww  w.ja va  2 s  . c  o  m*/
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));

        Cipher encrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        encrypt.init(Cipher.ENCRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        encryptedPassword = encrypt.doFinal(password.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't encrypt password " + password + e);
    }

    return new String(Base64.encodeBase64(encryptedPassword));
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * /*from w  w w  .  j a  va 2  s.  co  m*/
 * 
 * @param data  ?
 * @return  ?
 */
public static String encrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, getKey());
        byte e[] = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(e);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:D_common.E_ncript.java

public static byte[] encrypt(byte[] data, String keyStr) {
    try {//from  w ww.  j av a2 s  .c  o  m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.tydic.dbp.utils.ThreeDesUtils.java

public static String encryptMode(String Src) {
    try {/*  w  ww .ja  v a 2  s.  c  o m*/
        // ?
        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
        // 
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        return Hex.encodeHexString(c1.doFinal(Src.getBytes()));
    } catch (java.security.NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (javax.crypto.NoSuchPaddingException e2) {
        e2.printStackTrace();
    } catch (java.lang.Exception e3) {
        e3.printStackTrace();
    }
    return null;
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Encrypts string value with the specified AES algorithm
 * @param Data string to be encrypted/*  w w  w.j  av a 2s  .  c o m*/
 * @return encrypted string
 */
public static String encrypt(String Data) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        Base64 encoder = new Base64();
        String encryptedValue = new String(encoder.encode(encVal)).trim();
        return encryptedValue;
    } catch (Exception e) {
        return null;
    }
}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String encrypt(final String unsecureText, final String key) throws PunchOutCipherException {
    String encrypted = null;//  w  w w.ja  v a  2 s.  com
    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        final byte[] encryptedValue = cipher.doFinal(unsecureText.getBytes());
        encrypted = new Base64().encodeAsString(encryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during encryption." + e.getMessage();
        LOG.error(msg);
        throw new PunchOutCipherException(msg, e);
    }

    return encrypted;
}

From source file:net.dbjorge.jthumbor.ThumborUtils.java

/**
 * Encrypts the given plaintext with the given key according to AES-128 in ECB mode.
 *
 * This function performs NO padding on either of the key or plaintext. It REQUIRES that both
 * key and plaintext be non-null, non-empty, and have sizes which are multiples of 16.
 *//*from  w  w  w.ja  v  a 2 s . c  o  m*/
public static byte[] aesEncrypt(String key, String plaintext) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        mAesCipher.init(Cipher.ENCRYPT_MODE, keySpec);
        return mAesCipher.doFinal(plaintext.getBytes());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}