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.marvelution.jira.plugins.hudson.encryption.StringEncrypter.java

/**
 * Encrypt a given {@link String}/*from  ww w.  j ava 2 s .  c om*/
 * 
 * @param stringToEncrypt the {@link String} to encrypt
 * @return the encrypted {@link String}
 */
public String encrypt(String stringToEncrypt) {
    if (stringToEncrypt == null || stringToEncrypt.length() == 0) {
        return "";
    }
    try {
        initiliseCipher(Cipher.ENCRYPT_MODE);
        return new String(Base64.encodeBase64(cipher.doFinal(stringToEncrypt.getBytes(UNICODE_FORMAT))));
    } catch (Exception e) {
        LOGGER.error("Failed to encrypt provided String. Reason: " + e.getMessage(), e);
        throw new StringEncryptionException("Failed to encrypt provided String. Reason: " + e.getMessage(), e);
    }
}

From source file:com.sapienter.jbilling.common.JBCryptoImpl.java

public String encrypt(String text) {
    Cipher cipher = getCipher();//from  ww w.  j  ava2  s .co m
    byte[] crypted;
    try {
        cipher.init(Cipher.ENCRYPT_MODE, mySecretKey, ourPBEParameters);
        byte[] bytes = text.getBytes(UTF8);
        crypted = cipher.doFinal(bytes);
    } catch (GeneralSecurityException e) {
        throw new IllegalArgumentException("Can not encrypt :" + text, e);
    }
    String cryptedText = useHexForBinary ? Util.binaryToString(crypted)
            : new String(Base64.encodeBase64(crypted));
    return cryptedText;
}

From source file:com.eucalyptus.auth.euare.EuareServerCertificateUtil.java

public static String getEncryptedKey(final String certArn, final String certPem) throws AuthException {
    final ServerCertificate targetCert = lookupServerCertificate(certArn);
    // generate symmetric key
    final MessageDigest digest = Digest.SHA256.get();
    final byte[] salt = new byte[32];
    Crypto.getSecureRandomSupplier().get().nextBytes(salt);
    digest.update(salt);//from   www  . java2 s.c  o m
    final SecretKey symmKey = new SecretKeySpec(digest.digest(), "AES");

    try {
        // encrypt the server pk using symm key
        Cipher cipher = Ciphers.AES_CBC.get();
        final byte[] iv = new byte[16];
        Crypto.getSecureRandomSupplier().get().nextBytes(iv);
        cipher.init(Cipher.ENCRYPT_MODE, symmKey, new IvParameterSpec(iv),
                Crypto.getSecureRandomSupplier().get());
        final byte[] cipherText = cipher.doFinal(Base64.encode(targetCert.getPrivateKey().getBytes()));
        final String encPrivKey = new String(Base64.encode(Arrays.concatenate(iv, cipherText)));

        // encrypt the symmetric key using the certPem
        X509Certificate x509Cert = PEMFiles.getCert(B64.standard.dec(certPem));
        cipher = Ciphers.RSA_PKCS1.get();
        cipher.init(Cipher.ENCRYPT_MODE, x509Cert.getPublicKey(), Crypto.getSecureRandomSupplier().get());
        byte[] symmkey = cipher.doFinal(symmKey.getEncoded());
        final String b64SymKey = new String(Base64.encode(symmkey));

        return String.format("%s\n%s", b64SymKey, encPrivKey);
    } catch (final Exception ex) {
        throw Exceptions.toUndeclared(ex);
    }
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Encrypt String associated with a key/* w ww .  j  av a 2 s .  co  m*/
 *
 * @param value The value to encrypt
 * @return encrypted string
 */
public static String encrypt(String value) {
    if (StringUtils.isBlank(value)) {
        throw new IllegalArgumentException();
    }

    String encryptedText = "";

    try {
        Cipher cipher = Cipher.getInstance(CIPHER);
        cipher.init(Cipher.ENCRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
        encryptedText = Base64.encodeToString(cipher.doFinal(value.getBytes()), Base64.NO_CLOSE);
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        Log.e(TAG, "Exception during encrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    }

    return encryptedText;
}

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

private static void generateCipher(String rawKey) {
    try {/*from w  w w  .jav  a 2s  .  co  m*/
        SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), ALGORITHM);
        encodeCipher = Cipher.getInstance(ALGORITHM);
        encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec);
        decodeCipher = Cipher.getInstance(ALGORITHM);
        decodeCipher.init(Cipher.DECRYPT_MODE, keySpec);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

}

From source file:com.sldeditor.common.property.EncryptedPropertiesApache.java

@Override
public void initialise(String password) {
    PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(salt, 20);
    SecretKeyFactory kf;//  w  ww .  j a v a2  s .  c o m
    try {
        kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(password.toCharArray()));
        encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
        encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
        decrypter.init(Cipher.DECRYPT_MODE, k, ps);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:edu.kit.dama.util.CryptUtil.java

/**
 * Hidden constuctor./*  w w  w . j ava  2s. c  om*/
 *
 * @param pSecret The secret used for the SecretKeySpec. The secret must
 * have a length of 128, 192 or 256 bits.
 */
private CryptUtil(byte[] pSecret) {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(pSecret, "AES");
        deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
        deCipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
        enCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider());
        enCipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new IllegalStateException("Failed to create cipher instances.", e);
    }
}

From source file:com.launchkey.sdk.crypto.JCECrypto.java

/**
 * @see Crypto#encryptRSA(byte[], PublicKey)
 *//*from ww  w .  j  a v a2s .com*/
public byte[] encryptRSA(byte[] message, PublicKey publicKey) {
    return processRSA(message, publicKey, Cipher.ENCRYPT_MODE);
}

From source file:cn.vlabs.duckling.aone.client.impl.EmailNameEncoding.java

public String getEncryptEmail(String email) {
    String context = getContext(email);

    SecretKeySpec spec;/*from   w  w w  .ja v a 2s. c o  m*/
    try {
        spec = new SecretKeySpec(getKey(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, spec);
        byte[] result = cipher.doFinal(context.getBytes("utf-8"));
        return encodeByte(result);
    } catch (Exception e) {
        throw new RuntimeException("", e);
    }
}

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

private void initBlowfish() {
    encryptCipher = getCipher(CIPHER_NAME, PROVIDER_NAME);
    decryptCipher = getCipher(CIPHER_NAME, PROVIDER_NAME);
    initCipher(encryptCipher, Cipher.ENCRYPT_MODE, keySpec, paramSpec);
    initCipher(decryptCipher, Cipher.DECRYPT_MODE, keySpec, paramSpec);
}