java中常用的数据加密算法


1. [图片] 33.jpg    
java中常用的数据加密算法

2. [代码]加密常用的工具类     跳至 [2] [全屏预览]

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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
 
import com.sun.mail.util.BASE64DecoderStream;
import com.sun.mail.util.BASE64EncoderStream;
 
 
public class util {
    /**
     * 传入名文和公钥钥对数据进行RSA解密
     * <br>生成时间:2014年5月2日  下午2:38:13
     * <br>返回值:String
     * <br>@param src
     * <br>@param pubkey
     * <br>@return
     */
    public static String rsaEncoding(String src,PublicKey pubkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.ENCRYPT_MODE, pubkey);
            byte[] by = cip.doFinal(src.getBytes());
            return new String(BASE64EncoderStream.encode(by));
             
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
         
    }
    /**
     * 传入RSA密文和私钥对数据进行解密
     * <br>生成时间:2014年5月2日  下午2:37:08
     * <br>返回值:String
     * <br>@param sec
     * <br>@param privkey
     * <br>@return
     */
    public static String rsaDeEncoding(String sec,PrivateKey privkey){
        try {
            Cipher cip = Cipher.getInstance("RSA");
            cip.init(cip.DECRYPT_MODE, privkey);
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            return new String(cip.doFinal(by));
             
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        }
         
    }
     
    /**
     * 传入字符串、**,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES)
     * <br>生成时间:2014年5月2日  下午12:05:44
     * <br>返回值:String 密文
     * <br>@param src
     * <br>@param key
     * <br>@param method(DES、AES、DESede)
     * <br>@return
     */
    //对称加密加密
    public static String doubKeyEncoding(String src,String keysrc,String method) {
        SecretKey key;
        try {
            //生成**
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //初始化此**生成器。
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();
             
            //加密
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(Cipher.ENCRYPT_MODE, key);
            ciph.update(src.getBytes("utf-8"));
            //使用64进行编码,一避免出现丢数据情景
            byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
            return new String(by);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 传入字符串、**、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)
     * <br>生成时间:2014年5月2日  下午1:12:13
     * <br>返回值:String **原文
     * <br>@param sec
     * <br>@param key
     * <br>@param method(DES、AES、DESede)
     * <br>@return
     */
    public static String doubKeyDencoding(String sec,String keysrc,String method) {
        SecretKey key;
        try {
            //生成**
            KeyGenerator kg =  KeyGenerator.getInstance(method);
            //初始化此**生成器。
            kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
            key = kg.generateKey();
            //加密
            Cipher ciph =  Cipher.getInstance(method);
            ciph.init(ciph.DECRYPT_MODE, key);
            //使用64进行解码,一避免出现丢数据情景
            byte[] by = BASE64DecoderStream.decode(sec.getBytes());
            ciph.update(by);
            return new String(ciph.doFinal());
             
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException(e);
        } catch (BadPaddingException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
         
    /**
     * 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
     * <br>生成时间:2014年5月2日  上午11:13:44
     * <br>返回值:String         加密后的密文
     * <br>@param src     传入加密字符串(明文)
     * <br>@param method  指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
     * <br>@return
     */
    public static String ecodingPasswd(String src,String method){
         
        try {
            //信息摘要算法
            MessageDigest md5 = MessageDigest.getInstance(method);
            md5.update(src.getBytes());
            byte[] encoding = md5.digest();
            //使用64进行编码,一避免出现丢数据情景
            return new String(BASE64EncoderStream.encode(encoding));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e+"加密失败!!");
        }
         
    }
}