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.imaginary.home.cloud.Configuration.java

static public @Nonnull String encrypt(@Nonnull String keySalt, @Nonnull String value) {
    try {/*  w  w  w. ja v a  2  s. c  om*/
        SecretKeySpec spec = new SecretKeySpec(getConfiguration().getCustomSalt(keySalt), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, spec);

        byte[] raw = value.getBytes("utf-8");
        byte[] encrypted = cipher.doFinal(raw);
        byte[] b64 = Base64.encodeBase64(encrypted);
        return new String(b64, "utf-8");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.blackcrowsys.sinscrypto.AesEncryptor.java

@Override
public String encrypt(String secretkey, String iv, String toEncrypt)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, DecoderException {
    Cipher cipher = Cipher.getInstance(AESMODE);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretkey.getBytes(), AES);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(Hex.decodeHex(iv.toCharArray())));
    return Base64.encodeBase64String(cipher.doFinal(toEncrypt.getBytes()));
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static String encryptStringImpl(Context context, final String plainText) {
    String encryptedText = plainText;
    try {/*  w ww .  j  a  v a2s. c  o  m*/
        final KeyStore keyStore = getKeyStore(context);

        PublicKey publicKey = keyStore.getCertificate(KEY_ALIAS).getPublicKey();

        String algorithm = ALGORITHM_OLD;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            algorithm = ALGORITHM;
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        CipherOutputStream cipherOutputStream = new CipherOutputStream(outputStream, cipher);
        cipherOutputStream.write(plainText.getBytes("UTF-8"));
        cipherOutputStream.close();

        byte[] bytes = outputStream.toByteArray();
        encryptedText = Base64.encodeToString(bytes, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedText;
}

From source file:Main.java

/**
 * This method should be used to encrypt a block of data.
 * // ww  w  .  jav a 2s .co  m
 * @param fileData
 *            the data to encrypt.
 * @param encryptionKey
 *            the key to initialize the cipher-algorithm.
 * @param encryptCompleted
 *            a flag, that indicates, that a multi-part encryption is to be
 *            completed. e.g. false, if the fileData consist of multiple
 *            parts. true, if the fileData consists only of one single part
 *            and so they could be encrypted in one operation.
 * 
 * @return the encrypted data.
 */
public static byte[] encryptData(byte[] fileData, byte[] encryptionKey, boolean encryptCompleted) {

    // Initializing may only be done at the start of a multi-part crypto-operation.
    // if it's a single part crypto-operation initialization must always be done.
    if (!encryptInitialized) {
        // Initializing the cipher
        try {
            encryptCipher = Cipher.getInstance("AES");
        } catch (Exception e) {
            Log.e(TAG, "Error while initializing encryption.");
            return null;
        }

        try {
            SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, keySpec);
        } catch (Exception e) {
            Log.e(TAG, "Error while initializing encryption.");
            return null;
        }
        encryptInitialized = true;
    }

    // encrypting
    try {
        if (!encryptCompleted)
            // done in case of multi-part operation
            fileData = encryptCipher.update(fileData);
        else
            // done in case of single part operation or to finish a multi-part operation
            fileData = encryptCipher.doFinal(fileData);
    } catch (Exception e) {
        Log.e(TAG, "Error during encryption.");
        return null;
    }

    // at the and of an multi-part operation flags must be reset
    if (encryptCompleted)
        encryptInitialized = false;
    return fileData;
}

From source file:com.creditcloud.common.security.impl.DESTextCipher.java

@Override
public void init(String salt) {
    try {//from ww w  . jav a2s .co  m
        SecretKey sk = keyFactory.generateSecret(new DESKeySpec(salt.getBytes()));
        encryptCipher.init(Cipher.ENCRYPT_MODE, sk);
        decryptCipher.init(Cipher.DECRYPT_MODE, sk);
    } catch (InvalidKeyException | InvalidKeySpecException ex) {
        logger.error("Can't init Cipher.[salt={}]", salt, ex);
    }
}

From source file:com.haulmont.cuba.web.test.PasswordEncryptionTest.java

protected String encryptPassword(String password) {
    SecretKeySpec key = new SecretKeySpec(PASSWORD_KEY.getBytes(), "DES");
    IvParameterSpec ivSpec = new IvParameterSpec(PASSWORD_KEY.getBytes());
    String result;//from w w w . jav  a2s .  c om
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
        result = new String(Hex.encodeHex(cipher.doFinal(password.getBytes())));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:com.alta189.deskbin.util.DesEncrypter.java

public DesEncrypter(SecretKey key) {
    try {/*from  w w w.  j a  v a 2s .co m*/
        ecipher = Cipher.getInstance("DES");
        dcipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        dcipher.init(Cipher.DECRYPT_MODE, key);

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

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

public static String encrypt(String unencrypted, String key, String initVector) throws ServerException {
    try {//  w  ww .j a v a 2s. c o  m
        Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, key, initVector);

        byte[] encrypted = cipher.doFinal(unencrypted.getBytes());
        return Base64.encodeBase64String(encrypted);
    } catch (GeneralSecurityException e) {
        throw new ServerException(ServerError.ENCRYPTION, e);
    }
}

From source file:adminpassword.AESDemo.java

public String encrypt(String plainText) throws Exception {

    //get salt/*from  ww w  .j a  va  2s . c o  m*/
    salt = generateSalt();
    byte[] saltBytes = salt.getBytes("UTF-8");

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    //encrypt the message
    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(plainText.getBytes("UTF-8"));
    return new Base64().encodeAsString(encryptedTextBytes);
}

From source file:com.arwantech.docjavaui.utils.TripleDES.java

public String encrypt(String unencryptedString) {
    String encryptedString = null;
    try {/* w w w.  ja v  a2 s. c om*/
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
        byte[] encryptedText = cipher.doFinal(plainText);
        encryptedString = new String(Base64.encodeBase64(encryptedText));
    } catch (InvalidKeyException | UnsupportedEncodingException | IllegalBlockSizeException
            | BadPaddingException e) {
        e.printStackTrace();
    }
    return encryptedString;
}