Chinaunix首页 | 论坛 | 博客
  • 博客访问: 516933
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2010-06-22 17:17:34

DESedeUtil.java

package test.crypto;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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 DESedeUtil {

    public static final String ALGORITHM = "DESede";

    private String transformation = null;

    private static DESedeUtil instance = null;

    private byte[] ivParam = null;

    private DESedeUtil(String transformation) {
        this(transformation, new byte[8]);
    }

    private DESedeUtil(String transformation, byte[] ivParam) {
        super();
        this.transformation = transformation;
        this.ivParam = ivParam;
    }

    public synchronized static DESedeUtil getInstance() {
        if (instance == null) {
            instance = new DESedeUtil("DESede/CBC/PKCS5Padding");
        }
        return instance;
    }

    public byte[] encode(byte[] key, byte[] data) throws RuntimeException {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam);

            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);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] decode(byte[] key, byte[] data) throws RuntimeException {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParam);
            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);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (InvalidAlgorithmParameterException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }

    public byte[] generateRandomKey() throws RuntimeException {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            kgen.init(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(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 {
        DESedeUtil desedeUtil = DESedeUtil.getInstance();

        byte[] key = desedeUtil.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 = desedeUtil.encode(key, data);
        System.out.println("encrypted data : " + Arrays.toString(encData));

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


Output

key length : 24 bytes (192 bits)
key data : [118, 121, -43, -51, 25, -57, -12, 88, -118, -9, 118, -17, -17, -32, -43, -36, -80, 93, 97, 93, 7, 21, -70, 11]
plain text : Hello
           : [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
encrypted data : [-37, -127, 98, 45, -66, -64, 36, 107, 9, 44, 67, 86, 90, -32, 73, -36]
decrypted data : [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
decrypted data (text) : Hello


阅读(771) | 评论(0) | 转发(0) |
0

上一篇:JCE - AES

下一篇:JCE - RSA

给主人留下些什么吧!~~