hjg
2024-03-18 8d48c59d5d3bcc4148ef97dc6f98e3a8cf9ca436
提交 | 用户 | 时间
58d006 1 package com.mandi.common;
A 2
3 import java.io.UnsupportedEncodingException;
4 import java.security.InvalidAlgorithmParameterException;
5 import java.security.InvalidKeyException;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.SecureRandom;
8
9 import javax.crypto.BadPaddingException;
10 import javax.crypto.Cipher;
11 import javax.crypto.IllegalBlockSizeException;
12 import javax.crypto.KeyGenerator;
13 import javax.crypto.NoSuchPaddingException;
14 import javax.crypto.SecretKey;
15 import javax.crypto.spec.IvParameterSpec;
16 import javax.crypto.spec.SecretKeySpec;
17
18 import org.apache.commons.codec.binary.Base64;
19
20 public class AESMethod {
21     public static String encryptPKCS7ForBase64(String pwd,String source,byte[] IV){
22         if(source==null||pwd==null)
23             return null;
24         try {
25             KeyGenerator keygen=KeyGenerator.getInstance("AES");
26             SecureRandom sr=SecureRandom.getInstance("SHA1PRNG");
27             sr.setSeed(pwd.getBytes());
28             keygen.init(128,sr);
29             SecretKey key=keygen.generateKey();
30             byte[] keyendoce=key.getEncoded();
31             SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES");
32             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
33             try {
34                 cipher.init(Cipher.ENCRYPT_MODE,keyspec,new IvParameterSpec(IV));
35                 byte[] sbytes=null;
36                 sbytes = cipher.doFinal(source.getBytes("utf8"));
37                 return Base64.encodeBase64String(sbytes) ;//byte2hexStr(sbytes);  //这里也可以用base64来表示,这样解密的时候反解析base64
38             } catch (UnsupportedEncodingException e) {
39                 System.out.println("加密过程中,getbyte有错误!");
40                 e.printStackTrace();
41             } catch (InvalidAlgorithmParameterException e) {
42                 e.printStackTrace();
43             }
44         } catch (NoSuchAlgorithmException e) {
45             e.printStackTrace();
46         } catch (NoSuchPaddingException e) {
47             e.printStackTrace();
48         } catch (InvalidKeyException e) {
49             e.printStackTrace();
50         } catch (IllegalBlockSizeException e) {
51             e.printStackTrace();
52         } catch (BadPaddingException e) {
53             e.printStackTrace();
54         }
55         return "";
56     }
57     
58     
59     public static String decryptPKCS7ForBase64(String pwd,String source,byte[] IV){
60         if(source==null||pwd==null)
61             return "";
62         byte[] sbytes=Base64.decodeBase64(source);//hexStr2bytes(source);
63         try {
64             KeyGenerator keygen=KeyGenerator.getInstance("AES");
65             SecureRandom sr=SecureRandom.getInstance("SHA1PRNG");
66             sr.setSeed(pwd.getBytes());
67             keygen.init(128,sr);
68             SecretKey key=keygen.generateKey();
69             byte[] keyendoce=key.getEncoded();
70             SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES");
71             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
72             try {
73                 cipher.init(Cipher.DECRYPT_MODE, keyspec,new IvParameterSpec(IV));
74                 byte[] dbytes=cipher.doFinal(sbytes);
75                 return new String(dbytes,"utf8");
76             } catch (UnsupportedEncodingException e) {
77                 System.out.println("解密过程中,编码错误!");
78                 e.printStackTrace();
79             } catch (InvalidAlgorithmParameterException e) {
80                 e.printStackTrace();
81             }
82         } catch (NoSuchAlgorithmException e) {
83             e.printStackTrace();
84         } catch (NoSuchPaddingException e) {
85             e.printStackTrace();
86         } catch (InvalidKeyException e) {
87             e.printStackTrace();
88         } catch (IllegalBlockSizeException e) {
89             e.printStackTrace();
90         } catch (BadPaddingException e) {
91             e.printStackTrace();
92         }
93         return "";
94     }
95     
96     
97     public static String encryptPKCS7ForBase64(byte[] pwd,String source,byte[] IV){
98         if(source==null||pwd==null)
99             return null;
100         try {
101             KeyGenerator keygen=KeyGenerator.getInstance("AES");
102             SecureRandom sr=SecureRandom.getInstance("SHA1PRNG");
103             sr.setSeed(pwd);
104             keygen.init(128,sr);
105             SecretKey key=keygen.generateKey();
106             byte[] keyendoce=key.getEncoded();
107             SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES");
108             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
109             try {
110                 cipher.init(Cipher.ENCRYPT_MODE,keyspec,new IvParameterSpec(IV));
111                 byte[] sbytes=null;
112                 sbytes = cipher.doFinal(source.getBytes("utf8"));
113                 return Base64.encodeBase64String(sbytes);//byte2hexStr(sbytes);  //这里也可以用base64来表示,这样解密的时候反解析base64
114             } catch (UnsupportedEncodingException e) {
115                 System.out.println("加密过程中,getbyte有错误!");
116                 e.printStackTrace();
117             } catch (InvalidAlgorithmParameterException e) {
118                 e.printStackTrace();
119             }
120         } catch (NoSuchAlgorithmException e) {
121             e.printStackTrace();
122         } catch (NoSuchPaddingException e) {
123             e.printStackTrace();
124         } catch (InvalidKeyException e) {
125             e.printStackTrace();
126         } catch (IllegalBlockSizeException e) {
127             e.printStackTrace();
128         } catch (BadPaddingException e) {
129             e.printStackTrace();
130         }
131         return "";
132     }
133     
134     
135     public static String decryptPKCS7ForBase64(byte[] pwd,String source,byte[] IV){
136         if(source==null||pwd==null)
137             return "";
138         byte[] sbytes=Base64.decodeBase64(source);//hexStr2bytes(source);
139         try {
140             KeyGenerator keygen=KeyGenerator.getInstance("AES");
141             SecureRandom sr=SecureRandom.getInstance("SHA1PRNG");
142             sr.setSeed(pwd);
143             keygen.init(128,sr);
144             SecretKey key=keygen.generateKey();
145             byte[] keyendoce=key.getEncoded();
146             SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES");
147             Cipher cipher=Cipher.getInstance("AES/CBC/PKCS5Padding");
148             try {
149                 cipher.init(Cipher.DECRYPT_MODE, keyspec,new IvParameterSpec(IV));
150                 byte[] dbytes=cipher.doFinal(sbytes);
151                 return new String(dbytes,"utf8");
152             } catch (UnsupportedEncodingException e) {
153                 System.out.println("解密过程中,编码错误!");
154                 e.printStackTrace();
155             } catch (InvalidAlgorithmParameterException e) {
156                 e.printStackTrace();
157             }
158         } catch (NoSuchAlgorithmException e) {
159             e.printStackTrace();
160         } catch (NoSuchPaddingException e) {
161             e.printStackTrace();
162         } catch (InvalidKeyException e) {
163             e.printStackTrace();
164         } catch (IllegalBlockSizeException e) {
165             e.printStackTrace();
166         } catch (BadPaddingException e) {
167             e.printStackTrace();
168         }
169         return "";
170     }
171 }