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:gov.nih.nci.cadsr.cadsrpasswordchange.core.OracleObfuscation.java

public byte[] encrypt(byte[] bytes) throws GeneralSecurityException {
    cipher.init(Cipher.ENCRYPT_MODE, key, iv); // normally you could leave
    // out the IvParameterSpec
    // argument, but not with
    // Oracle//from  w  w w.j  a  v  a  2s .  c o m

    return cipher.doFinal(bytes);
}

From source file:com.vmware.o11n.plugin.crypto.service.CryptoEncryptionService.java

/**
 * AES Encryption CBC Mode with PKCS5 Padding
 *
 * @param dataB64 Data to encrypt Base64 encoded
 * @param secretB64 Encryption secret Base64 encoded. For AES128 this should be 128 bits (16 bytes) long. For AES256 this should be 256 bits (32 bytes) long.
 * @param ivB64 Initialization Vector Base64 encoded. 16 bytes long
 * @return Encrypted data Base64 Encoded
 * @throws NoSuchAlgorithmException//from w  w w.  ja v  a  2 s.c  om
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidAlgorithmParameterException
 * @throws IOException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public String aesEncrypt(String dataB64, String secretB64, String ivB64)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String encryptedB64 = null;

    final byte[] dataBytes = Base64.decodeBase64(dataB64);
    final byte[] secretBytes = Base64.decodeBase64(secretB64);
    final byte[] ivBytes = Base64.decodeBase64(ivB64);
    final Cipher cipher = Cipher.getInstance(AES_CIPHER);

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretBytes, "AES"),
            new IvParameterSpec(ivBytes, 0, cipher.getBlockSize()));

    encryptedB64 = Base64.encodeBase64String(cipher.doFinal(dataBytes));
    return encryptedB64;
}

From source file:com.jaspersoft.jasperserver.jaxrs.client.core.EncryptionUtils.java

private static String getEncryptedPassword(PublicKey publicKey, String pwd) throws Exception {
    byte[] encryptedUtfPass;
    Cipher enc = Cipher.getInstance("RSA/NONE/NoPadding", new BouncyCastleProvider());
    enc.init(Cipher.ENCRYPT_MODE, publicKey);
    String utfPass = URLEncoder.encode(pwd, CharEncoding.UTF_8);
    encryptedUtfPass = enc.doFinal(utfPass.getBytes());

    return byteArrayToHexString(encryptedUtfPass);
}

From source file:Main.java

public static byte[] aesIGEencrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {//from  w  w  w . j av  a  2 s  .  c  om

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] ivp = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {

            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.lling.qiqu.utils.AesUtils.java

/**
 * Encrypts a message using a 128bit Bse64 key using AES.
 *
 * @param key     128bit Base64 AES key/*from   www. j  a  v a 2s.com*/
 * @param message Message to encrypt
 * @return hex encoded encrypted message
 */
public static String encrypt(String key, String message) {
    String encrypted = "";
    try {
        // Generate the secret key specs.
        byte[] keyArray = Base64.decodeBase64(key.getBytes());
        SecretKeySpec skeySpec = new SecretKeySpec(keyArray, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypt = cipher.doFinal(message.getBytes());
        encrypted = new String(hexCodec.encode(encrypt));
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
    return encrypted;
}

From source file:edu.harvard.i2b2.analysis.security.RijndaelAlgorithm.java

public RijndaelAlgorithm(String password, int ksize, String encryptionType, String emethod) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), encryptionType);

    cipherEnc = Cipher.getInstance(emethod);
    cipherDec = Cipher.getInstance(emethod);

    keySize = ksize;//from w w w.j a v a  2  s  . c o m

    //setKey( pword );
    cipherEnc.init(Cipher.ENCRYPT_MODE, skeySpec);
    cipherDec.init(Cipher.DECRYPT_MODE, skeySpec);

}

From source file:com.google.u2f.gaedemo.impl.DataStoreImpl.java

@Override
public String storeSessionData(EnrollSessionData sessionData) {

    SecretKey key = new SecretKeySpec(SecretKeys.get().sessionEncryptionKey(), "AES");
    byte[] ivBytes = new byte[16];
    random.nextBytes(ivBytes);/*from   ww  w  .  j  a v  a 2  s  . co m*/
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    Cipher cipher;
    try {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, IV);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    }

    SealedObject sealed;
    try {
        sealed = new SealedObject(sessionData, cipher);
    } catch (IllegalBlockSizeException | IOException e) {
        throw new RuntimeException(e);
    }

    ByteArrayOutputStream out;
    try {
        out = new ByteArrayOutputStream();
        ObjectOutputStream outer = new ObjectOutputStream(out);

        outer.writeObject(sealed);
        outer.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return Base64.encodeBase64URLSafeString(out.toByteArray());
}

From source file:com.miyue.util.Cryptos.java

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

From source file:com.teasoft.teavote.util.AppProperties.java

public void setSecret(String secret) {
    try {//from w w  w . j a  v  a  2  s.  co m
        String first = PBKDF2_ALGORITHM.substring(0, PBKDF2_ALGORITHM.length() - 2);
        String second = "";
        for (int i = 0, j = 12; i < 4; i++) {
            second += first.substring(j - (i * 4), first.length() - i * 4);
        }
        Key aesKey = new SecretKeySpec(second.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(secret.getBytes());
        this.secret = Base64.encodeBase64String(encrypted);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(AppProperties.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.sshtools.j2ssh.transport.cipher.BlowfishCbc.java

/**
 *
 *
 * @param mode//  w  w w .ja va  2s .  c  o m
 * @param iv
 * @param keydata
 *
 * @throws AlgorithmOperationException
 */
public void init(int mode, byte[] iv, byte[] keydata) throws AlgorithmOperationException {
    try {
        cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");

        // Create a 16 byte key
        byte[] actualKey = new byte[16];
        System.arraycopy(keydata, 0, actualKey, 0, actualKey.length);

        SecretKeySpec keyspec = new SecretKeySpec(actualKey, "Blowfish");

        // Create the cipher according to its algorithm
        cipher.init(((mode == ENCRYPT_MODE) ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE), keyspec,
                new IvParameterSpec(iv, 0, cipher.getBlockSize()));
    } catch (NoSuchPaddingException nspe) {
        log.error("Blowfish initialization failed", nspe);
        throw new AlgorithmOperationException("No Padding not supported");
    } catch (NoSuchAlgorithmException nsae) {
        log.error("Blowfish initialization failed", nsae);
        throw new AlgorithmOperationException("Algorithm not supported");
    } catch (InvalidKeyException ike) {
        log.error("Blowfish initialization failed", ike);
        throw new AlgorithmOperationException("Invalid encryption key");

        /*} catch (InvalidKeySpecException ispe) {
             throw new AlgorithmOperationException("Invalid encryption key specification");*/
    } catch (InvalidAlgorithmParameterException ape) {
        log.error("Blowfish initialization failed", ape);
        throw new AlgorithmOperationException("Invalid algorithm parameter");
    }
}