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:io.personium.core.model.file.DataCryptor.java

/**
 * Generate InputStream for encryption from Input and return it.
 * If encryptEnable is false, it returns input as is.
 * @param input input data// w  w w. j  a v a  2s .co  m
 * @param encryptEnable encryption flag
 * @return InputStream for encryption
 */
public InputStream encode(InputStream input, boolean encryptEnable) {
    if (!encryptEnable) {
        return input;
    }
    try {
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(iv));
        CipherInputStream encodedInputStream = new CipherInputStream(input, cipher);

        return encodedInputStream;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tremolosecurity.provisioning.customTasks.CreateOTPKey.java

public static String generateEncryptedToken(String userID, GoogleAuthenticatorKey key, String hostName,
        ConfigManager cfg, String encryptionKey) throws ProvisioningException {
    TOTPKey totpkey = new TOTPKey();
    totpkey.setHost(hostName);//from  w ww.  ja v  a2 s .  com
    totpkey.setScratchCodes(key.getScratchCodes());
    totpkey.setSecretKey(key.getKey());
    totpkey.setUserName(userID);
    totpkey.setValidationCode(key.getVerificationCode());

    Gson gson = new Gson();
    String json = gson.toJson(totpkey);
    SecretKey sc = cfg.getSecretKey(encryptionKey);
    String attrVal = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(json.getBytes("UTF-8"));

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, sc);

        byte[] encJson = cipher.doFinal(baos.toByteArray());
        String base64d = new String(org.bouncycastle.util.encoders.Base64.encode(encJson));

        Token token = new Token();
        token.setEncryptedRequest(base64d);
        token.setIv(new String(org.bouncycastle.util.encoders.Base64.encode(cipher.getIV())));

        json = gson.toJson(token);
        attrVal = new String(org.bouncycastle.util.encoders.Base64.encode(json.getBytes("UTF-8")));

    } catch (Exception e) {
        throw new ProvisioningException("Could not encrypt key", e);
    }
    return attrVal;
}

From source file:cherry.goods.crypto.RSACryptoTest.java

private RSACrypto create2(char[] password) throws Exception {

    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);//from ww  w.java2s  .  co  m
    KeyPair key = keygen.generateKeyPair();

    String pbeAlgName = "PBEWithMD5AndDES";
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20);
    SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec);
    AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName);
    pbeParam.init(pbeParamSpec);
    Cipher cipher = Cipher.getInstance(pbeAlgName);
    cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam);
    EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam,
            cipher.doFinal(key.getPrivate().getEncoded()));

    RSACrypto impl = new RSACrypto();
    impl.setAlgorithm("RSA/ECB/PKCS1Padding");
    impl.setPublicKeyBytes(key.getPublic().getEncoded());
    impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password);
    return impl;
}

From source file:com.example.license.DESUtil.java

public static String encryptBase64(String data, String key) throws Exception {

    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE, securekey);

    byte[] results = cipher.doFinal(data.getBytes("UTF-8"));
    // Encrypt/*from w ww  . j av  a2s. c om*/
    // byte[] unencryptedByteArray = data.getBytes("UTF8");
    // byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray);

    // Encode bytes to base64 to get a string
    byte[] encodedBytes = Base64.encodeBase64(results);
    return new String(encodedBytes);
}

From source file:com.norbl.util.StringUtil.java

public static String encrypt(SecretKey key, String s) throws Exception {
    Cipher c = Cipher.getInstance("AES");
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] enb = c.doFinal(s.getBytes());
    Base64 b64enc = new Base64();
    return (new String(b64enc.encode(enb)));
}

From source file:gobblin.crypto.EncodingBenchmark.java

@Benchmark
public byte[] write1KRecordsDirectCipherStream(EncodingBenchmarkState state) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, state.credStore.getKey());
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    OutputStream os = new CipherOutputStream(sink, cipher);
    os.write(state.OneKBytes);//from   w  ww .  j  av a 2 s  .c o  m
    os.close();

    return sink.toByteArray();
}

From source file:br.com.ufjf.labredes.crypto.Cryptography.java

public static String encrypt(Serializable object, byte[] aesKey) {
    SecretKeySpec secretKey = new SecretKeySpec(aesKey, ALGORITHM_SYM);
    String object_encrypted = null;
    try {//from   w ww . j a v  a 2 s.c om
        final Cipher cipher = Cipher.getInstance(ALGORITHM_SYM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] data = SerializationUtils.serialize(object);
        byte[] cipherText = cipher.doFinal(data);
        object_encrypted = new BASE64Encoder().encode(cipherText);
    } catch (Exception e) {
        e.printStackTrace();
        return "bbb";
    }
    return object_encrypted;
}

From source file:com.xwiki.authentication.ntlm.NTLMAuthServiceImpl.java

protected String encryptText(String text, XWikiContext context) {
    try {/*from w w  w .  ja  v a  2 s .  co 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 = Base64.encode(encrypted);
            return encoded.replaceAll("=", "_");
        } else {
            LOG.error("Encryption key not defined");
        }
    } catch (Exception e) {
        LOG.error("Failed to encrypt text", e);
    }

    return null;
}

From source file:CipherSocket.java

public OutputStream getOutputStream() throws IOException {
    OutputStream os = delegate == null ? super.getOutputStream() : delegate.getOutputStream();
    Cipher cipher = null;/*w w w  .java 2  s .  c  om*/
    try {
        cipher = Cipher.getInstance(algorithm);
        int size = cipher.getBlockSize();
        byte[] tmp = new byte[size];
        Arrays.fill(tmp, (byte) 15);
        IvParameterSpec iv = new IvParameterSpec(tmp);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
    } catch (Exception e) {
        throw new IOException("Failed to init cipher: " + e.getMessage());
    }
    CipherOutputStream cos = new CipherOutputStream(os, cipher);
    return cos;
}

From source file:cl.niclabs.tscrypto.common.messages.EncryptedData.java

private byte[] encryptAES(SecretKeySpec skeySpec, byte[] data) {

    byte[] encrypted = null;

    try {//from   w w  w . jav a 2  s .  c o m
        // Instantiate the cipher
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        encrypted = cipher.doFinal(data);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException e) {
        e.printStackTrace();
    }

    return encrypted;
}