Java DES 加密和解密源码

Java密码学结构设计遵循两个原则:

1) 算法的独立性和可靠性。

2) 实现的独立性和相互作用性。

算法的独立性是通过定义密码服务类来获得。用户只需了解密码算法的概念,而不用去关心如何实现这些概念。实现的独立性和相互作用性通过密码服务提供器来实现。密码服务提供器是实现一个或多个密码服务的一个或多个程序包。软件开发商根据一定接口,将各种算法实现后,打包成一个提供器,用户可以安装不同的提供器。安装和配置提供器,可将包含提供器的ZIPJAR文件放在CLASSPATH,再编辑Java安全属性文件来设置定义一个提供器。


DES算法及如何利用DES算法加密和解密类文件的步骤

DES算法简介

DESData Encryption Standard)是发明最早的最广泛使用的分组对称加密算法。DES算法的入口参数有三个:KeyDataMode。其中Key8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;ModeDES的工作方式,有两种:加密或解密。


[1].[代码] java 加密解密源码 跳至 [1]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.afreon.util;
 
import java.io.IOException;
import java.security.SecureRandom;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
public class DesUtil {
 
     private final static String DES = "DES" ;
 
     public static void main(String[] args) throws Exception {
         String data = "123 456" ;
         String key = "wang!@#$%" ;
         System.err.println(encrypt(data, key));
         System.err.println(decrypt(encrypt(data, key), key));
 
     }
     
     /**
      * Description 根据键值进行加密
      * @param data
      * @param key  加密键byte数组
      * @return
      * @throws Exception
      */
     public static String encrypt(String data, String key) throws Exception {
         byte [] bt = encrypt(data.getBytes(), key.getBytes());
         String strs = new BASE64Encoder().encode(bt);
         return strs;
     }
 
     /**
      * Description 根据键值进行解密
      * @param data
      * @param key  加密键byte数组
      * @return
      * @throws IOException
      * @throws Exception
      */
     public static String decrypt(String data, String key) throws IOException,
             Exception {
         if (data == null )
             return null ;
         BASE64Decoder decoder = new BASE64Decoder();
         byte [] buf = decoder.decodeBuffer(data);
         byte [] bt = decrypt(buf,key.getBytes());
         return new String(bt);
     }
 
     /**
      * Description 根据键值进行加密
      * @param data
      * @param key  加密键byte数组
      * @return
      * @throws Exception
      */
     private static byte [] encrypt( byte [] data, byte [] key) throws Exception {
         // 生成一个可信任的随机数源
         SecureRandom sr = new SecureRandom();
 
         // 从原始密钥数据创建DESKeySpec对象
         DESKeySpec dks = new DESKeySpec(key);
 
         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
         SecretKey securekey = keyFactory.generateSecret(dks);
 
         // Cipher对象实际完成加密操作
         Cipher cipher = Cipher.getInstance(DES);
 
         // 用密钥初始化Cipher对象
         cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
 
         return cipher.doFinal(data);
     }
     
     
     /**
      * Description 根据键值进行解密
      * @param data
      * @param key  加密键byte数组
      * @return
      * @throws Exception
      */
     private static byte [] decrypt( byte [] data, byte [] key) throws Exception {
         // 生成一个可信任的随机数源
         SecureRandom sr = new SecureRandom();
 
         // 从原始密钥数据创建DESKeySpec对象
         DESKeySpec dks = new DESKeySpec(key);
 
         // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
         SecretKey securekey = keyFactory.generateSecret(dks);
 
         // Cipher对象实际完成解密操作
         Cipher cipher = Cipher.getInstance(DES);
 
         // 用密钥初始化Cipher对象
         cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
 
         return cipher.doFinal(data);
     }
}
 
来源地址:http: //www.yoodb.com/article/display/266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值