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:corner.services.impl.DESedeEncryptServiceImpl.java

public byte[] encrypt(byte[] src, byte[] key) {
    try {//w w w .j  a va  2s  .c  om
        SecretKey deskey = new SecretKeySpec(key, Algorithm);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, deskey);
        return c1.doFinal(src);
    } 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.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] encrypt(String clearText, String key, byte[] iv) {
    try {// w ww  . j a va 2s. c o  m
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
        return cipher.doFinal(clearText.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt.", e);
    }
}

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

private static Cipher getEncryptCipher(HttpServletRequest request) throws Exception {
    Cipher encryptCipher = (Cipher) request.getAttribute(REQUEST_TAG_ENCRYPT_CIPHER);
    if (encryptCipher == null) {
        encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
        request.setAttribute(REQUEST_TAG_ENCRYPT_CIPHER, encryptCipher);
    }/* w ww .  j av a2  s  .c  o m*/
    return encryptCipher;
}

From source file:com.jwt.security.auth.cryptographics.Crypto.java

public String encrypt(String plaintext) throws InvalidKeySpecException {
    try {/*from   w w w . jav  a 2s .  co  m*/
        SecretKey key = generateKey(cryptoProps.getSalt(), cryptoProps.getPassphrase());
        byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, cryptoProps.getIv(), plaintext.getBytes("UTF-8"));
        return base64(encrypted);
    } catch (UnsupportedEncodingException e) {
        throw fail(e);
    }
}

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Encryption /*from w  w w  .j  a va2 s  .com*/
 * @param content content to be encrypted
 * @param encryptKey encryption key
 * @return 
 * @throws Exception 
 */
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);
    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));
}

From source file:io.manasobi.utils.CryptoUtils.java

/**
 *  .//from w  w w  .  j ava  2 s  .  c  o  m
 *
 * @param keyHex generateHexKey ? ? ?? Hex ? ? 
 * @param data    ?? byte 
 * 
 * @return  ? ??
 */
public static byte[] encryptByDES(String keyHex, byte[] data) {

    SecretKey key = getSecretDESKeyFromHex(keyHex);

    byte[] encryptedData = null;

    try {

        Cipher cipher = Cipher.getInstance(ALGORITHM);

        cipher.init(Cipher.ENCRYPT_MODE, key);

        encryptedData = cipher.doFinal(data);

    } catch (Exception e) {

        throw new CryptoUtilsException(e.getMessage());
    }

    return encryptedData;
}

From source file:net.mobid.codetraq.utils.PasswordProcessor.java

/**
 * Encrypts a text using the <code>passPhrase</code> above and an algorithm supported
 * by your virtual machine implementation. You can change the default algorithm with
 * another algorithm, but please make sure your virtual machine supports it.
 * @param valueToEncrypt - text to encrypt
 * @return an encrypted, Base64 encoded text
 *//*  w  w w  . ja va 2 s.c o  m*/
public static String encryptString(String valueToEncrypt) {
    String output = null;
    try {
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations);
        SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
        // begin encrypting...
        byte[] byteToEncrypt = valueToEncrypt.getBytes("UTF8");
        byte[] encrypted = cipher.doFinal(byteToEncrypt);
        output = new Base64().encodeToString(encrypted);
    } catch (Exception ex) {
        Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.aqnote.shared.encrypt.symmetric.Blowfish.java

private static void generateCipher(String rawKey) {
    ProviderUtil.addBCProvider();//from w  w  w . ja  va  2s .c  o m
    encryptCipher = instanceCipher(CIPHER_NAME, PROVIDER_NAME);
    decryptCipher = instanceCipher(CIPHER_NAME, PROVIDER_NAME);
    key = generateKey(rawKey);
    iv = generateIV();
    initCipher(encryptCipher, Cipher.ENCRYPT_MODE, key, iv);
    initCipher(decryptCipher, Cipher.DECRYPT_MODE, key, iv);
}

From source file:com.vmware.bdd.security.EncryptionGuard.java

/**
 * Encrypt the clear text against given secret key.
 * /*www .  java 2 s .c o m*/
 * @param clearText
 *           the clear string
 * @return the encrypted string, or null if the clear string is null
 * @throws CommonException
 *            if input arguments is null
 */
public static String encode(String clearText) throws GeneralSecurityException, UnsupportedEncodingException {
    if (clearText == null) {
        return null;
    }

    Key key = GuardKeyStore.getEncryptionKey();
    String salt = SaltGenerator.genRandomString(SALT_SIZE);

    String inputText = salt + clearText; // add salt
    byte[] clearBytes = inputText.getBytes(UTF8_ENCODING);

    Cipher cipher = getCiperInternal(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedBytes = cipher.doFinal(clearBytes);

    Base64 base64 = new Base64(0); // 0 - no chunking
    return salt + base64.encodeToString(encryptedBytes);
}

From source file:testFileHandler.java

public void encrypt(String message, String secretKey, javax.swing.JTextArea ciphertextField) throws Exception {

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    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);
    // Encode the string into bytes using utf-8
    byte[] plainTextBytes = message.getBytes("utf-8");
    // Encrypt//from  w  ww . j av  a  2  s.  c  om
    byte[] buf = cipher.doFinal(plainTextBytes);
    // Encode bytes to base64 to get a string
    byte[] base64Bytes = Base64.encodeBase64(buf);
    String base64EncryptedString = new String(base64Bytes);

    ciphertextField.setText(base64EncryptedString);
}