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.aast.encrypt.EncryptManager.java

public static String encryptAES(String key, String value) {
    try {/*  w  w w .j a va 2  s. c  om*/
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "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:MainClass.java

private static byte[] passwordEncrypt(char[] password, byte[] plaintext) throws Exception {
    int MD5_ITERATIONS = 1000;
    byte[] salt = new byte[8];
    SecureRandom random = new SecureRandom();
    random.nextBytes(salt);//w w  w  . j av  a  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, MD5_ITERATIONS);
    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(salt);
    baos.write(ciphertext);
    return baos.toByteArray();
}

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);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

From source file:com.jsmartframework.web.manager.CsrfEncrypter.java

private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_CSRF_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_CSRF_ENCRYPT_CIPHER, encryptCipher);
    }/*from  ww  w . ja  v a 2  s.c o  m*/
    return encryptCipher;
}

From source file:com.jsmartframework.web.manager.AuthEncrypter.java

private static Cipher getEncryptCipher(HttpServletRequest request, String key) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_AUTH_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF8"), "AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_AUTH_ENCRYPT_CIPHER, encryptCipher);
    }/*  w w  w.  j a v a 2  s .  c o m*/
    return encryptCipher;
}

From source file:aajavafx.Kripto.java

public String encrypt(String plainText) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] byteCipher = cipher.doFinal(plainText.getBytes("UTF-8"));
    Base64 base64 = new Base64();
    String stringToStore = new String(base64.encode(byteCipher));
    //byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
    return stringToStore;
}

From source file:com.myapp.common.AES4MEncrypt.java

/**
 * /*w w w.  jav  a  2  s  .c o  m*/
 * 
 * @param sSrc 
 * @param sKey KEY
 * @return
 * @throws Exception
 * @author cdduqiang
 * @date 201443
 */
public static String encrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        log.error("Encrypt Key ??");
        throw new Exception("Encrypt Key ??");
    }

    byte[] raw = hex2byte(sKey);
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] anslBytes = sSrc.getBytes(ENCODING);// string2AnslBytes(sSrc);
    byte[] encrypted = cipher.doFinal(anslBytes);
    return byte2hex(encrypted).toUpperCase();
}

From source file:com.iterzp.momo.utils.RSAUtils.java

/**
 * /* w  w  w  .j av  a2s.  co  m*/
 * 
 * @param publicKey
 *            
 * @param data
 *            ?
 * @return ??
 */
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA", PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.liferay.sync.engine.util.Encryptor.java

public static String encrypt(String value) throws Exception {
    if (value == null) {
        return "";
    }/*from   w  w  w.  j  a v  a2  s .c  o m*/

    SecretKey secretKey = new SecretKeySpec(_PASSWORD, _ALGORITHM);

    Cipher cipher = Cipher.getInstance(_ALGORITHM);

    cipher.init(Cipher.ENCRYPT_MODE, secretKey);

    String salt = getSalt();

    String encryptedValue = value;

    for (int i = 0; i < _ITERATIONS; i++) {
        encryptedValue = salt.concat(encryptedValue);

        byte[] encryptedBytes = cipher.doFinal(encryptedValue.getBytes(_UTF8_CHARSET));

        encryptedValue = Base64.encodeBase64String(encryptedBytes);
    }

    return encryptedValue;
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

/**
 * Verschlsselt den bergebenen String./* w  w  w  . j  av a2 s  .c  o  m*/
 * 
 * @param aString zu verschlsselnder String.
 */
public static String encrypt(final String aString) throws GeneralSecurityException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return base64Encode(pbeCipher.doFinal(aString.getBytes()));
}