Java DES 测试

package com.des.test;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author y
 */
public class DESUtil {
    private static final String CHARSET_TYPE    = "UTF-8";
    private static final String ENCRYPT_KEY     = "Z7CKBCcc5KwlZqjbunr1yhZG5nM";
    private static final String ENCRYPT_SCHEME  = "DESede";
    
    byte[] keyAsBytes;
    
    private final KeySpec myKeySpec;
    private final SecretKeyFactory mySecretKeyFactory;
    private final Cipher cipher;
    private final SecretKey key;
    
    public DESUtil() throws InvalidKeyException, UnsupportedEncodingException, 
            NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException{
        
        keyAsBytes = ENCRYPT_KEY.getBytes(CHARSET_TYPE);
        myKeySpec = new DESedeKeySpec(keyAsBytes);
        mySecretKeyFactory = SecretKeyFactory.getInstance(ENCRYPT_SCHEME);
        cipher = Cipher.getInstance(ENCRYPT_SCHEME);
        key = mySecretKeyFactory.generateSecret(myKeySpec);
    }
    
    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            
            byte[] plainText = unencryptedString.getBytes(CHARSET_TYPE);
            byte[] encryptedText = cipher.doFinal(plainText);
            
            Base64 base64 = new Base64();
            
            encryptedString = base64.encodeAsString(encryptedText);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return encryptedString;
    }
    
    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, key);
            
            Base64 base64 = new Base64();
            
            byte[] encryptedText = base64.decode(encryptedString);
            byte[] plainText = cipher.doFinal(encryptedText);
            
            decryptedText = new String(plainText,CHARSET_TYPE);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(DESUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return decryptedText;
    }
    
    public static void main(String[] args) throws Exception {
        String str = "hello,这是Java DES加密测试数据";
        
        DESUtil desUtil = new DESUtil();
        
        System.out.println("==desc before:"+str);
        String encryptStr = desUtil.encrypt(str);
        System.out.println("==desc after:"+encryptStr);
        System.out.println("==descrypt:"+desUtil.decrypt(encryptStr));
    }
}

 

posted @ 2015-04-22 17:43  yshy  阅读(444)  评论(0编辑  收藏  举报