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:love.sola.netsupport.util.RSAUtil.java

public static String encrypt(String value) {
    try {/*from   w w w  .j a  v  a 2  s  .c  o m*/
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encrypted = cipher.doFinal(value.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.zf.decipher.DataEn.java

private static byte[] encrypt(byte[] data) throws Exception {
    if (null == secretKey) {
        throw new SecurityException("secretKey is null");
    }/*w ww.j a va 2s.  co  m*/
    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:DesEncrypter.java

DesEncrypter(String passPhrase) throws Exception {
    int iterationCount = 2;
    KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
    SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
    ecipher = Cipher.getInstance(key.getAlgorithm());
    dcipher = Cipher.getInstance(key.getAlgorithm());

    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}

From source file:aes_encryption.AES_Encryption.java

public static String encrypt(String strToEncrypt) {
    try {//from   www .ja  v a2s . c  om
        Cipher cipher = Cipher.getInstance("AES_Encryption/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));
    } catch (Exception e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

From source file:com.sirius.utils.encrypt.DESEncryptor.java

@Override
public byte[] encrypt(Object key, byte[] data) throws EncryptException {
    try {/*from   w w  w . ja  va2  s . c om*/
        Cipher cipher = Cipher.getInstance(ALGORITHM, security_provider);
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key));
        return cipher.doFinal(data);
    } catch (Exception e) {
        throw new EncryptException(e);
    }
}

From source file:model.Encryption.java

/**
 * @param plainText//from ww  w .  ja  v a 2s. c om
 * @return
 * @throws Exception
 */
public String encrypt(String plainText) throws Exception {
    if (secret == null)
        generateSecretKey();

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));

    return new Base64().encodeBase64String(encryptedTextBytes);
}

From source file:Conexion.newClass.java

public String encode(String texto) throws EncoderException {
    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {//ww w  .  java2  s .co m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);
    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:it.geosolutions.figis.persistence.dao.util.PwEncoder.java

/**
 * Encoding the password on base64/*  www . jav a  2 s . c om*/
 * @param msg
 * @return
 */
public static String encode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = msg.getBytes();
        byte[] encrypted = cipher.doFinal(input);
        byte[] output = Base64.encodeBase64(encrypted);

        return new String(output);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (IllegalBlockSizeException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (BadPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException("Error while encoding", ex);
    }
}

From source file:com.enviosya.client.tool.Tool.java

public String Encriptar(String texto) {

    //llave para encriptar datos
    String base64EncryptedString = "";

    try {//w  ww  .ja  va 2  s  .  co  m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (Exception e) {
        //Ac tengo que agregar el retorno de la exception
    }
    return base64EncryptedString;
}

From source file:com.jk.security.JKEncDec.java

/**
 * Encrypt./*  w  ww. j a  v  a 2 s .  c  o  m*/
 *
 * @param plainText
 *            the plain text
 * @return the string
 */
public static String encrypt(String plainText) {
    try {
        plainText = fixText(plainText);
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
        SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
        return toString(cipher.doFinal(plainText.getBytes("UTF-8")));
    } catch (Exception e) {
        throw new JKSecurityException(e);
    }
}