DES加密算法Java应用实例
1import java.io.FileInputStream;
2import java.io.FileOutputStream;
3import java.io.ObjectInputStream;
4import java.io.ObjectOutputStream;
5import java.security.SecureRandom;
6 
7import javax.crypto.Cipher;
8import javax.crypto.KeyGenerator;
9import javax.crypto.SecretKey;
10 
11import sun.misc.BASE64Decoder;
12import sun.misc.BASE64Encoder;
13 
14/**
15*
16DES 全称为Data Encryption Standard即数据加密算法,它是IBM公司研究成功并公开发表的。
17DES算法的入口参数有三个:Key、Data、Mode。其中Key为8个字节共64位,是DES算法的工作密钥;
18Data也为8个字节64位,是要被加密或被解密的数据; Mode为DES的工作方式,有两种:加密或解密。
19DES算法是这样工作的:如Mode为加密,则用Key 去把数据Data进行加密,生成Data的密码形式(64位)作为DES的输出结果;
20如Mode为解密,则用Key去把密码形式的数据Data解密,还原为Data的明码形式(64位)作为DES的输出结果。
21在通信网络的两端,双方约定一致的Key,在通信的源点用Key对核心数据进行 DES加密,
22然后以密码形式在公共通信网(如电话网)中传输到通信网络的终点,数据到达目的地后,
23用同样的Key对密码数据进行解密,便再现了明码形式的核心数据。这样,便保证了核心数据(如PIN、MAC等)在公共通信网中传输的安全性和可靠性。
24通过定期在通信网络的源端和目的端同时改用新的Key,便能更进一步提高数据的保密性。
25初始Key值为64位,但DES算法规定,其中第8、 16、......64位是奇偶校验位,不参与DES运算。故Key 实际可用位数便只有56位。
263-DES(TripleDES):该算法被用来解决使用 DES 技术的 56 位时密钥日益减弱的强度,
27其方法是:使用两个密钥对明文运行 DES 算法三次,从而得到 112 位有效密钥强度。
28TripleDES 又称为 DESede(表示加密、解密和加密这三个阶段)。
29扩展阅读:http://zh.wikipedia.org/zh/3DES
30*
31*/
32public class DES_Encrypt {
33    /** 指定加密算法为DESede */
34    private static String ALGORITHM = "DESede";
35    /** 指定密钥存放文件 */
36    private static String KEYFile   = "KeyFile";
37 
38    /**
39    * 生成密钥
40    */
41    private static void generateKey() throws Exception {
42        /** DES算法要求有一个可信任的随机数源 */
43        SecureRandom sr = new SecureRandom();
44        /** 为DES算法创建一个KeyGenerator对象 */
45        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM);
46        /** 利用上面的随机数据源初始化这个KeyGenerator对象 */
47        kg.init(sr);
48        /** 生成密匙 */
49        SecretKey key = kg.generateKey();
50        /** 用对象流将生成的密钥写入文件 */
51        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(KEYFile));
52        oos.writeObject(key);
53        /** 清空缓存,关闭文件输出流 */
54        oos.close();
55    }
56 
57    /**
58    * 加密方法
59    *
60    * source 源数据
61    */
62    public static String encrypt(String source) throws Exception {
63        generateKey();
64        /** 将文件中的SecretKey对象读出 */
65        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(KEYFile));
66        SecretKey key = (SecretKey) ois.readObject();
67        /** 得到Cipher对象来实现对源数据的DES加密 */
68        Cipher cipher = Cipher.getInstance(ALGORITHM);
69        cipher.init(Cipher.ENCRYPT_MODE, key);
70        byte[] b = source.getBytes();
71        /** 执行加密操作 */
72        byte[] b1 = cipher.doFinal(b);
73        BASE64Encoder encoder = new BASE64Encoder();
74        return encoder.encode(b1);
75    }
76 
77    /**
78    * 解密密钥 cryptograph:密文
79    */
80    public static String decrypt(String cryptograph) throws Exception {
81        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(KEYFile));
82        SecretKey key = (SecretKey) ois.readObject();
83        Cipher cipher = Cipher.getInstance(ALGORITHM);
84        cipher.init(Cipher.DECRYPT_MODE, key);
85        BASE64Decoder decoder = new BASE64Decoder();
86        byte[] b1 = decoder.decodeBuffer(cryptograph);
87        byte[] b = cipher.doFinal(b1);
88        return new String(b);
89    }
90 
91    public static void main(String[] args) throws Exception {
92        String source = "Hello World!";// 要加密的字符串
93        String cryptograph = encrypt(source);// 生成的密文
94        System.out.println(cryptograph);
95 
96        String target = decrypt(cryptograph);// 解密密文
97        System.out.println(target);
98    }
99}
http://xzh.i3geek.com