Package io.conducive.client.crypt

Source Code of io.conducive.client.crypt.AES

package io.conducive.client.crypt;

import com.googlecode.gwt.crypto.bouncycastle.InvalidCipherTextException;
import com.googlecode.gwt.crypto.client.AESCipher;
import com.googlecode.gwt.crypto.util.SecureRandom;

import java.math.BigInteger;

/**
* @author Reuben Firmin
*/
public class AES {

    // TODO gwt-crypto doesn't let you set the IV. rewrite?
    public static String encrypt(String key, String iv, String value) {
        AESCipher cipher = new AESCipher();
        cipher.setKey(Hash.hash256(key.getBytes()));
        try {
            return cipher.encrypt(value);
        } catch (InvalidCipherTextException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decrypt(String key, String iv, String value) {
        AESCipher cipher = new AESCipher();
        cipher.setKey(Hash.hash256(key.getBytes()));
        try {
            return cipher.decrypt(value);
        } catch (InvalidCipherTextException e) {
            throw new RuntimeException(e);
        }
    }

    // TODO move out of AES
    public static String randomKey() {
        BigInteger exponent = new BigInteger(256, new SecureRandom());
        return exponent.toString();
    }
}
TOP

Related Classes of io.conducive.client.crypt.AES

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.