---
title: "DES加密解密"
author: "thirtysix"
date: "2007-09-25T13:01:00Z"
tags: []
slug: "des"
draft: false
lastmod: "2007-09-25T13:01:00Z"
id: 202081
url: "https://thirtysix.iteye.com/blog/202081"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package com.sinosoft.olyvem.common;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.security.SecureRandom;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.crypto.Cipher;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.crypto.SecretKey;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.crypto.SecretKeyFactory;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import javax.crypto.spec.DESKeySpec;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import sun.misc.BASE64Encoder;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class DES ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    private byte\[] desKey;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public DES(byte\[] desKey) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        this.desKey = desKey;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public byte\[] doEncrypt(byte\[] plainText) throws Exception ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // DES算法要求有一个可信任的随机数源  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecureRandom sr = new SecureRandom();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        byte rawKeyData\[] = desKey;/\*\*//* 用某种方法获得密匙数据 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 从原始密匙数据创建DESKeySpec对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        DESKeySpec dks = new DESKeySpec(rawKeyData);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 创建一个密匙工厂，然后用它把DESKeySpec转换成  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 一个SecretKey对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecretKey key = keyFactory.generateSecret(dks);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // Cipher对象实际完成加密操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Cipher cipher = Cipher.getInstance("DES");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 用密匙初始化Cipher对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        cipher.init(Cipher.ENCRYPT\_MODE, key, sr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 现在，获取数据并加密  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        byte data\[] = plainText;/\*\*//* 用某种方法获取数据 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 正式执行加密操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte encryptedData\[] = cipher.doFinal(data);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return encryptedData;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public byte\[] doDecrypt(byte\[] encryptText) throws Exception ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // DES算法要求有一个可信任的随机数源  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecureRandom sr = new SecureRandom();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        byte rawKeyData\[] = desKey; /\*\*//* 用某种方法获取原始密匙数据 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 从原始密匙数据创建一个DESKeySpec对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        DESKeySpec dks = new DESKeySpec(rawKeyData);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 创建一个密匙工厂，然后用它把DESKeySpec对象转换成  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 一个SecretKey对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        SecretKey key = keyFactory.generateSecret(dks);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // Cipher对象实际完成解密操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        Cipher cipher = Cipher.getInstance("DES");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 用密匙初始化Cipher对象  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        cipher.init(Cipher.DECRYPT\_MODE, key, sr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 现在，获取数据并解密  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        byte encryptedData\[] = encryptText;/\*\*//* 获得经过加密的数据 \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        // 正式执行解密操作  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte decryptedData\[] = cipher.doFinal(encryptedData);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return decryptedData;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String\[] args) throws Exception ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String key = "FtpXPass";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String value = "olympic";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        BASE64Encoder base64Encoder = new BASE64Encoder();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        DES desEncrypt = new DES(key.getBytes());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte\[] encryptText = desEncrypt.doEncrypt(value.getBytes());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //System.out.println("doEncrypt   -   " + toHexString(encryptText));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("doEncrypt   -   "  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                + base64Encoder.encode(encryptText));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        byte\[] decryptText = desEncrypt.doDecrypt("r9NGYcKAtdo=".getBytes());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        System.out.println("doDecrypt   -   " + new String(decryptText));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        //System.out.println("doDecrypt   -   " + toHexString(decryptText));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static String toHexString(byte\[] value) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String newString = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        for (int i = 0; i &lt; value.length; i++) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            byte b = value\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            String str = Integer.toHexString(b);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if (str.length() &gt; 2) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                str = str.substring(str.length() - 2);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if (str.length() &lt; 2) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                str = "0" + str;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)            }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            newString += str;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return newString.toUpperCase();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)