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.jwm123.loggly.reporter.TripleDesCipher.java

public TripleDesCipher(String keyPath, AppDirectory appDir) throws NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException {
    this.appDir = appDir;
    if (key == null) {
        this.keyPath = keyPath;
        getKey();//from   w  w  w .  j  a v a 2  s .com
    }
    SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
    encrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
    decrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    decrypter.init(Cipher.DECRYPT_MODE, keySpec);
}

From source file:com.github.sshw.crypt.EncryptionBean.java

public String encrypt(String message, String password) throws Exception {
    Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
    Key key = keyFromPassword(password);
    IvParameterSpec ivb = new IvParameterSpec(key.getEncoded());
    encrypt.init(Cipher.ENCRYPT_MODE, key, ivb);
    byte[] cb = encrypt.doFinal(message.getBytes());
    String c = Base64.encodeBase64URLSafeString(cb);
    return c;/*from  ww w  .j  av  a  2  s.c om*/
}

From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

public static String encrypt(String key, String toEncrypt) throws Exception {
    Key skeySpec = generateKeySpec(key);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", new BouncyCastleProvider());
    String abc = RandomStringUtils.randomAlphanumeric(16);
    System.out.println(abc);/*from  w w w .  jav  a 2  s. c om*/
    byte[] ivBytes = abc.getBytes();
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
    byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
    byte[] encryptedValue;
    encryptedValue = Base64.encodeBase64(encrypted);
    String result = new String();
    result += new String(encryptedValue);
    result = abc + result;
    System.out.println(result);
    return result;
}

From source file:com.clustercontrol.commons.util.CryptUtil.java

private static String encrypt(String key, String word) {
    if (word == null) {
        return null;
    }//from   w  ww  .  ja  v a2  s  . c o  m
    // ?
    SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), algorithm);
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance(algorithm);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, sksSpec);
    } catch (InvalidKeyException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    byte[] encrypted = null;
    try {
        encrypted = cipher.doFinal(word.getBytes());
    } catch (IllegalBlockSizeException | BadPaddingException e) {
        m_log.warn("encrypt : " + (e.getClass().getName()) + "," + e.getMessage(), e);
        return null;
    }

    return Base64.encodeBase64String(encrypted);
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;//ww  w  . j  ava 2s .  c om
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java

public static String generateKey(String toEncode) {

    String encoded = "";
    try {/*from ww  w . j  a v a  2 s . c om*/
        byte[] saltEncrypt = SALT_ENCRYPT.getBytes();
        SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp = factoryKeyEncrypt.generateSecret(
                new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH));
        SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
        Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER);
        aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey);
        byte[] bytes = StringUtils.getBytesUtf8(toEncode);
        byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes);
        encoded = Base64.encodeBase64URLSafeString(encryptBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}

From source file:jatoo.properties.FileProperties.java

public FileProperties(File file, Key key) throws GeneralSecurityException {

    this.file = file;

    cipherEncrypt = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);

    cipherDecrypt = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipherDecrypt.init(Cipher.DECRYPT_MODE, key);
}

From source file:com.liusoft.dlog4j.upgrade.StringUtils.java

/**
 * // ww  w. j a va 2  s .co  m
 * @param src ??
 * @param key 8?
 * @return     ??
 * @throws Exception
 */
public static byte[] encrypt(byte[] src, byte[] key) throws Exception {
    //      DES????
    SecureRandom sr = new SecureRandom();
    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);
    // ?DESKeySpec??
    // SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);
    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
    // ??
    // ??
    return cipher.doFinal(src);
}

From source file:com.seer.datacruncher.utils.CryptoUtil.java

/**
 * Method To Encrypt The Given String//from   w  ww .j  a v a2s .  c o  m
 */
public String encrypt(String unencryptedString) {
    String encryptedString = null;
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key);
        if (!checkKeyVerify())
            unencryptedString = unencryptedString.substring(0, unencryptedString.length() / 2);
        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.miyue.util.Cryptos.java

/**
 * AES./*from   w  w w  .  jav  a2  s.  c om*/
 * 
 * @param input 
 * @param key ?AES?
 */
public static byte[] aesEncrypt(byte[] input, byte[] key) {
    return aes(input, key, Cipher.ENCRYPT_MODE);
}