码迷,mamicode.com
首页 > 编程语言 > 详细

Java DES 测试

时间:2015-04-22 20:08:38      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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));
    }
}

 

Java DES 测试

标签:

原文地址:http://www.cnblogs.com/yshyee/p/4448178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!