文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>JCE - AES

JCE - AES

时间:2010-06-22  来源:btpka3

AESUtil.java

package test.crypto;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {

    public static final String ALGORITHM = "AES";

    /** 128, 192, 256 */
    // when keysize != 128

    // Download Java(TM) Cryptography Extension (JCE) Unlimited Strength

    // Jurisdiction Policy Files

    // file name : jce_policy-6.zip

    private int keySize = 128;

    private String transformation = null;

    private static AESUtil instance = null;

    private AESUtil(int keySize, String transformation) {
        super();
        this.keySize = keySize;
        this.transformation = transformation;
    }

    public synchronized static AESUtil getInstance() {
        if (instance == null) {
            instance = new AESUtil(128, "AES/CBC/PKCS5Padding");
        }
        return instance;
    }

    public byte[] encode(byte[] key, byte[] data) throws InvalidKeyException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException, NoSuchProviderException {

        SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(new byte[16]);

        try {
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            return cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] decode(byte[] key, byte[] data) throws InvalidKeyException,
            InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(new byte[16]);
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            return cipher.doFinal(data);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] generateRandomKey() throws RuntimeException {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            kgen.init(keySize, new SecureRandom());
            SecretKey skey = kgen.generateKey();
            return skey.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] generateKey(byte[] seed) throws RuntimeException {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            kgen.init(keySize, new SecureRandom(seed));
            SecretKey skey = kgen.generateKey();
            return skey.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        AESUtil aesUtil = AESUtil.getInstance();

        byte[] key = aesUtil.generateRandomKey();
        System.out.println("key length : " + key.length + " bytes ("
                + (key.length * 8) + " bits)");
        System.out.println("key data : " + Arrays.toString(key));

        String dataStr = "Hello world!";
        byte[] data = dataStr.getBytes();
        System.out.println("plain text : " + dataStr);
        System.out.println(" : " + Arrays.toString(data));

        byte[] encData = aesUtil.encode(key, data);
        System.out.println("encrypted data : " + Arrays.toString(encData));

        byte[] decData = aesUtil.decode(key, encData);
        System.out.println("decrypted data : " + Arrays.toString(decData));
        System.out.println("decrypted data (text) : " + new String(decData));
    }
}


Output

key length : 16 bytes (128 bits)
key data : [100, 55, 123, -122, -74, 0, 110, 81, 1, 37, -26, 104, 56, -84, -82, -80]
plain text : Hello
           : [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
encrypted data : [-7, -34, 71, -45, -96, 106, -8, 44, -40, 25, -46, 14, -65, -109, -12, -46]
decrypted data : [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
decrypted data (text) : Hello


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载