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.aurel.track.report.query.ReportQueryBL.java

private static String encrypt(String clearText, char[] password) {
    // Create PBE parameter set
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    byte[] ciphertext = { 0 };

    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    try {/*from   w w  w  .  j  a  va  2  s  . c o m*/
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        // Create PBE Cipher
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initialize PBE Cipher with key and parameters
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

        // Encrypt the cleartext
        ciphertext = pbeCipher.doFinal(clearText.getBytes());
    } catch (Exception e) {
        LOGGER.error(ExceptionUtils.getStackTrace(e));
    }
    return new String(Base64.encodeBase64(ciphertext));
}

From source file:by.bsuir.chujko.model.entities.security.StringCrypter.java

/**
 * To update secret key./*  w w  w.  jav  a  2s. co m*/
 * 
 * @param key
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException 
 */
private void updateSecretKey(SecretKey key)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    ecipher = Cipher.getInstance(key.getAlgorithm());
    dcipher = Cipher.getInstance(key.getAlgorithm());
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);
}

From source file:hh.learnj.test.license.test.rsa.RSATest.java

/**
 * //  w  ww  .j  a  va  2s .com
 * 
 * @param data
 * @return
 * @throws Exception
 */
static String encryptionByPublicKey(String source) throws Exception {
    PublicKey publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    cipher.update(source.getBytes("UTF-8"));
    String target = encodeBase64(cipher.doFinal());
    System.out.println("??\r\n" + target);
    return target;
}

From source file:br.com.manish.ahy.kernel.util.HashUtil.java

private static Cipher getCipher(boolean enc)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);//from  w  w w  .j a  v a2s.c o  m
    SecretKeySpec skeySpec = new SecretKeySpec(getBytes(HEXES2), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    if (enc) {
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    } else {
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    }
    return cipher;
}

From source file:com.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java

private static String encrypt(String clearText, char[] password) {
    // Create PBE parameter set
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    byte[] ciphertext = { 0 };

    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    try {/*from  w  ww.  j av a  2  s. c om*/
        SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

        // Create PBE Cipher
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

        // Initialize PBE Cipher with key and parameters
        pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

        // Encrypt the cleartext
        ciphertext = pbeCipher.doFinal(clearText.getBytes());
    } catch (Exception e) {
        LOGGER.debug(ExceptionUtils.getStackTrace(e), e);
    }
    return new String(Base64.encodeBase64(ciphertext));
}

From source file:net.ftb.util.CryptoUtils.java

/**
 * Method to AES encrypt string if fails, will attempt to use {@link #encryptLegacy(String str, byte[] key)}
 * @param str string to encrypt//from ww w. j a v  a 2 s.c  om
 * @param key encryption key
 * @return encrypted string or "" if legacy fails
 */
public static String encrypt(String str, byte[] key) {
    try {
        Cipher aes = Cipher.getInstance("AES");
        aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(pad(key), "AES"));
        return Base64.encodeBase64String(aes.doFinal(("FDT:" + str).getBytes("utf8")));
    } catch (Exception e) {
        Logger.logError("Error Encrypting information, reverting to legacy format", e);
        return encryptLegacy(str, key);
    }
}

From source file:com.confighub.core.security.Encryption.java

private static String encryptShared(CipherTransformation ct, String decrypted, String secret)
        throws ConfigException {
    if (null == decrypted)
        return null;

    try {//  w ww .  j  a va2s . com
        Cipher cipher = Cipher.getInstance(ct.getName());
        final int blockSize = cipher.getBlockSize();

        SecretKeySpec sharedKey = new SecretKeySpec(getKeyAsBytes(secret, ct.getKeyLen()), ct.getAlgo());

        if ("CBC".equals(ct.getMode()))
            cipher.init(Cipher.ENCRYPT_MODE, sharedKey, new IvParameterSpec(getIV(blockSize)));
        else
            cipher.init(Cipher.ENCRYPT_MODE, sharedKey);

        byte[] encrypted = cipher.doFinal(Utils.isBlank(decrypted) ? new byte[0] : decrypted.getBytes("UTF8"));
        return Base64.encodeBase64String(encrypted);
    } catch (Exception e) {
        throw new ConfigException(Error.Code.ENCRYPTION_ERROR);
    }
}

From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java

/**
 * /*from w  w w .jav  a2 s . co m*/
 * 
 * @param data?
 * @param key
 *            
 * @return byte[] ?
 */
public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {

    // 
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    // ?
    // ???
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
    // 
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    return cipher.doFinal(data);
}

From source file:com.gvmax.common.util.Enc.java

public String encrypt(String valueToEnc) {
    if (!enabled) {
        return valueToEnc;
    }// w  ww  .  j  av a  2 s .c o  m
    if (valueToEnc == null) {
        return null;
    }
    try {
        c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        byte[] encValue = c.doFinal(valueToEnc.getBytes("UTF-8"));
        return Base64.encodeBase64String(encValue);
    } catch (Exception e) {
        logger.warn("Unable to encrypt: " + valueToEnc, e);
        return null;
    }
}

From source file:com.xwiki.authentication.AbstractAuthServiceImpl.java

protected String encryptText(String text, XWikiContext context) {
    try {//  w  w  w. jav a  2s .c o  m
        String secretKey = null;
        secretKey = context.getWiki().Param("xwiki.authentication.encryptionKey");
        secretKey = secretKey.substring(0, 24);

        if (secretKey != null) {
            SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "TripleDES");
            Cipher cipher = Cipher.getInstance("TripleDES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encrypted = cipher.doFinal(text.getBytes());
            String encoded = new String(Base64.encodeBase64(encrypted));
            return encoded.replaceAll("=", "_");
        } else {
            LOG.error("Encryption key not defined");
        }
    } catch (Exception e) {
        LOG.error("Failed to encrypt text", e);
    }

    return null;
}