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:de.scrubstudios.srvmon.agent.classes.Crypt.java

/**
 * This function is used to encrypt the outgoing data.
 * @param key Encryption key//from ww  w.  j  av  a  2  s  .  c om
 * @param data Data which will be encrypted.
 * @return The encrypted data string.
 */
public static String encrypt(String key, String data) {
    byte[] encryptedData = null;
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

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

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

    return null;
}

From source file:io.hawkcd.agent.services.SecurityService.java

public String encrypt(String raw) {
    try {/*from   ww  w.  ja  v  a 2  s .c o  m*/
        Cipher cipher = getCipher(Cipher.ENCRYPT_MODE);
        byte[] encryptedVal = cipher.doFinal(getBytes(raw));
        byte[] encodedValue = Base64.encodeBase64(encryptedVal);
        String result = getString(encodedValue);
        return result;
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void encryptOrDecrypt(String encryptionKey, int mode, InputStream is, OutputStream os)
        throws InvalidKeyException, IOException {
    DESKeySpec keySpec = new DESKeySpec(toBytes(encryptionKey));

    SecretKey key = null;//from w ww  .  j a v  a  2 s.c om
    Cipher cipher = null;
    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        key = secretKeyFactory.generateSecret(keySpec);
        cipher = Cipher.getInstance("DES");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided/*w  w  w.j  a  v a 2s.co  m*/
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.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());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:com.yukthi.utils.CryptoUtils.java

/**
 * Encrypts the provided input and convert encrypted bytes into hex string
 * @param secretKey Secret key to use for encryption
 * @param input Input to be encrypted/*  w w  w.java  2  s . com*/
 * @return encrypted and hex converted string
 */
public static String encrypt(String secretKey, String input) {
    if (secretKey.length() != 16) {
        throw new InvalidArgumentException("Secret key should be of length 16. Specified secrey key length - ",
                secretKey.length());
    }

    //convert input into bytes
    byte inputBytes[] = input.getBytes();

    //encrypt bytes
    byte encryptedBytes[] = doCrypto(Cipher.ENCRYPT_MODE, secretKey, inputBytes);

    //convert encrypted bytes into hex string
    return Hex.encodeHexString(encryptedBytes);
}

From source file:com.boulmier.machinelearning.jobexecutor.encrypted.AES.java

public String encrypt(String strToEncrypt) {
    try {//from w w  w. ja  v  a2s .c  o m
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        return (Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))));

    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

From source file:com.anteam.demo.codec.cipher.symmetric.DESTest.java

License:asdf

@Test
public void testDES() throws Exception {
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x0F, 0x0E, 0x0D, 0x0C };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");

    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] clearText = "?????;(*)*$(R%*PDSJF>XJIPUFIWE(*#*&$)@#*"
            .getBytes();/*from w  ww  .j  a  v a2s  .  c  o m*/

    System.out.println(clearText.length);

    byte[] encryptedText = m_encrypter.doFinal(clearText);
    System.out.println(encryptedText.length);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText);
    System.out.println(decryptedText.length);

    System.out.println(new String(clearText));
    System.out.println(new String(encryptedText));
    System.out.println(new String(decryptedText));

}

From source file:com.kixeye.chassis.transport.crypto.SymmetricKeyCryptoUtils.java

/**
 * Encrypts a data blob using the given key and cipher.
 * //from  w w w  . ja  v a2 s  .co  m
 * @param data
 * @param offset
 * @param length
 * @param key
 * @param cipher
 * @return
 * @throws GeneralSecurityException
 */
public static byte[] encrypt(byte[] data, int offset, int length, Key key, String cipherTransformation,
        String cipherProvider) throws GeneralSecurityException {
    Cipher cipher = loadCipher(cipherTransformation, cipherProvider);
    cipher.init(Cipher.ENCRYPT_MODE, key);

    return cipher.doFinal(data, offset, length);
}

From source file:Authentication.HashPassword.java

public String encrypt(String unencryptedString) {
    String encryptedString = null;
    try {/*from   ww  w .  j a v a 2 s.  c  o  m*/
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
        byte[] encryptedText = cipher.doFinal(plainText);
        encryptedString = new String(Base64.encodeBase64(encryptedText));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedString;
}

From source file:com.soctec.soctec.utils.Encryptor.java

/**
 * Constructs the encryptor.// w w  w .ja  v  a2  s .  com
 * Creates the needed keys and cipher for encryption.
 *
 */
public Encryptor() {

    try {
        keySpec = new DESKeySpec(keyString.getBytes("UTF8"));
        keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);

        ecipher = Cipher.getInstance("DES");
        decipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        decipher.init(Cipher.DECRYPT_MODE, key);

    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | InvalidKeySpecException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
}