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.example.license.RSAUtil.java

public static String encrypt(String data, PrivateKey pri_key) throws Exception {
    // Cipher??//from  w ww . j  ava  2  s  .  co m
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // SecureRandom random = new SecureRandom();
    // ?Cipher?
    cipher.init(Cipher.ENCRYPT_MODE, pri_key);
    byte[] results = cipher.doFinal(data.getBytes());
    // http://tripledes.online-domain-tools.com/??
    for (int i = 0; i < results.length; i++) {
        System.out.print(results[i] + " ");
    }
    System.out.println();
    // ??Base64?
    return Base64.encodeBase64String(results);
}

From source file:com.elle.analyster.admissions.AESCrypt.java

public static String encrypt(String key, String initVector, String value) {
    try {/*from w  w  w .jav  a2 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());
        System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:com.feedzai.commons.sql.abstraction.util.AESHelper.java

/**
 * Encrypts a byte[]./* w w w  . ja va2  s .co  m*/
 *
 * @param c   The byte[] to encrypt.
 * @param key The key.
 * @return The encrypted array as a HEX string.
 */
public static String encrypt(byte[] c, String key) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(key.toCharArray()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encoded = cipher.doFinal(c);
        return new String(Hex.encodeHex(encoded));

    } catch (Exception e) {
        logger.warn("Could not encrypt byte[]", e);
        return null;
    }
}

From source file:com.os.util.PasswordDecoderEncoder.java

public static String encrypt(String plainPassword) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    key = convertHexToBytes(keyst);/*from  www  . j  a  v  a  2s.  co m*/
    final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    final String encryptedString = Base64.encodeBase64String(cipher.doFinal(plainPassword.getBytes("UTF8")));
    System.out.println(encryptedString);
    String passwordEncrypted = encryptedString.trim();

    return passwordEncrypted;

}

From source file:com.ikon.util.SecureStore.java

/**
 * DES encoder/* ww  w .  ja va2 s.  c o m*/
 */
public static byte[] desEncode(String key, byte[] src)
        throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey sKey = keyFactory.generateSecret(keySpec);

    Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
    cipher.init(Cipher.ENCRYPT_MODE, sKey);
    byte[] dst = cipher.doFinal(src);

    return dst;
}

From source file:net.mindengine.oculus.frontend.web.Auth.java

public static String encodeUser(User user) throws Exception {
    if (user == null) {
        throw new IllegalArgumentException("User should not be null");
    }//w  w w  . j  a va2s  .c o m
    if (secrectAuthKey == null) {
        throw new IllegalArgumentException("Couldn't generate secret key");
    }

    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, secrectAuthKey);

    SealedObject sealedUser = new SealedObject(user, cipher);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(sealedUser);
    oos.close();
    return new String(Base64.encodeBase64(baos.toByteArray()));
}

From source file:jp.co.golorp.emarf.util.CryptUtil.java

/**
 * 16???AES???Base64????//from w w w.j a v  a 2  s .c o  m
 *
 * @param string
 *            
 * @return ?
 */
public static String encrypt(final String string) {

    if (string == null) {
        return null;
    }

    byte[] input = string.getBytes();

    byte[] encryped = cipher(Cipher.ENCRYPT_MODE, input);

    byte[] encoded = Base64.encodeBase64(encryped, false);

    String ret = new String(encoded);

    LOG.debug("Encrypt [" + string + "] to [" + ret + "].");

    return ret;
}

From source file:com.tesora.dve.common.PECryptoUtils.java

public static String encrypt(String str) throws PEException {
    if (StringUtils.isBlank(str))
        return str;

    try {//www  .jav a2  s .  c o m
        Cipher cipher = createCrypter(Cipher.ENCRYPT_MODE);

        byte[] enc = cipher.doFinal(str.getBytes("UTF8"));

        return new String(Base64.encodeBase64(enc));
    } catch (Exception e) {
        throw new PEException("Failed to encrypt '" + str + "'", e);
    }
}

From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java

public static String rsaEncrypt(String string) throws Exception {
    StringWriter resultWriter = new StringWriter();
    byte[] bytes = string.getBytes("UTF-8");
    PublicKey pubKey = (PublicKey) readKeyFromFile(Config.getProperty("escidoc.aa.public.key.file"), true);
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    int blockSize = 245;
    for (int i = 0; i < bytes.length; i += blockSize) {
        byte[] result = cipher.doFinal(bytes, i, (i + blockSize < bytes.length ? blockSize : bytes.length - i));
        if (i > 0) {
            resultWriter.write("&");
        }/*from  ww w  .  ja va  2  s  .com*/
        resultWriter.write("auth=");
        resultWriter.write(URLEncoder.encode(new String(Base64.encodeBase64(result)), "ISO-8859-1"));
    }
    return resultWriter.toString();

}

From source file:adminpassword.Encryption.java

public String encrypt(String word, String idKey) throws Exception {

    byte[] ivBytes;
    String password = idKey; //you can give whatever you want. This is for testing purpose
    SecureRandom random = new SecureRandom();
    byte bytes[] = new byte[20];
    random.nextBytes(bytes);/*  w ww.  j ava2 s. c o m*/

    byte[] saltBytes = bytes;

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);
    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypting the word
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    AlgorithmParameters params = cipher.getParameters();
    ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
    byte[] encryptedTextBytes = cipher.doFinal(word.getBytes("UTF-8"));

    //prepend salt and vi
    byte[] buffer = new byte[saltBytes.length + ivBytes.length + encryptedTextBytes.length];
    System.arraycopy(saltBytes, 0, buffer, 0, saltBytes.length);
    System.arraycopy(ivBytes, 0, buffer, saltBytes.length, ivBytes.length);
    System.arraycopy(encryptedTextBytes, 0, buffer, saltBytes.length + ivBytes.length,
            encryptedTextBytes.length);
    return new Base64().encodeToString(buffer);
}