今天找了下资料,终于弄成功了,记录一下。
1 package com.example.sysprint.util; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import java.security.Key; 9 import java.security.SecureRandom; 10 import java.util.Base64; 11 import javax.crypto.Cipher; 12 import javax.crypto.CipherInputStream; 13 import javax.crypto.CipherOutputStream; 14 import javax.crypto.KeyGenerator; 15 16 public class DES { 17 Key key ; 18 19 private static final String keys="DES"; 20 21 public DES() { 22 setKey(keys); // 生成密匙 23 } 24 25 public Key getKey() { 26 return key ; 27 } 28 29 30 public void setKey(Key key) { 31 this.key = key; 32 } 33 34 35 36 /** 37 * 根据参数生成 KEY 38 */ 39 40 public void setKey(String strKey) { 41 try { 42 KeyGenerator _generator = KeyGenerator.getInstance ("DES"); 43 44 _generator.init( new SecureRandom(strKey.getBytes())); 45 46 this . key = _generator.generateKey(); 47 48 _generator = null ; 49 50 } catch (Exception e) { 51 throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); 52 } 53 54 } 55 56 57 58 /** 59 60 * 加密 String 明文输入 ,String 密文输出 61 62 */ 63 64 public String encryptStr(String strMing) { 65 byte [] byteMi = null; 66 byte [] byteMing = null; 67 String strMi = ""; 68 69 //BASE64Encoder base64en = new BASE64Encoder(); 70 //Java SE9 中已删除,换成以下 71 Base64.Encoder base64en = Base64.getEncoder(); 72 73 try { 74 75 byteMing = strMing.getBytes( "UTF8" ); 76 77 byteMi = this .encryptByte(byteMing); 78 79 strMi = base64en.encodeToString(byteMi); 80 81 } catch (Exception e) { 82 83 throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); 84 85 } finally { 86 base64en = null ; 87 byteMing = null ; 88 byteMi = null ; 89 } 90 return strMi; 91 } 92 93 /** 94 * 解密 以 String 密文输入 ,String 明文输出 95 * @param strMi 96 * @return 97 */ 98 public String decryptStr(String strMi) { 99 //BASE64Decoder base64De = new BASE64Decoder(); 100 Base64.Encoder base64De = Base64.getEncoder(); 101 102 byte [] byteMing = null ; 103 104 byte [] byteMi = null ; 105 106 String strMing = "" ; 107 108 try { 109 Base64.Decoder decoder = Base64.getDecoder(); 110 111 byteMi = decoder.decode(strMi); 112 113 byteMing = this .decryptByte(byteMi); 114 115 strMing = new String(byteMing, "UTF8" ); 116 117 } catch (Exception e) { 118 throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); 119 } finally { 120 base64De = null ; 121 byteMing = null ; 122 byteMi = null ; 123 } 124 return strMing; 125 } 126 127 128 129 /** 130 * 加密以 byte[] 明文输入 ,byte[] 密文输出 131 * @param byteS 132 * @return 133 */ 134 135 private byte [] encryptByte( byte [] byteS) { 136 byte [] byteFina = null ; 137 Cipher cipher; 138 try { 139 cipher = Cipher.getInstance ( "DES" ); 140 141 cipher.init(Cipher. ENCRYPT_MODE , key ); 142 143 byteFina = cipher.doFinal(byteS); 144 145 } catch (Exception e) { 146 147 throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); 148 149 } finally { 150 cipher = null ; 151 } 152 153 return byteFina; 154 155 } 156 157 158 159 /** 160 161 * 解密以 byte[] 密文输入 , 以 byte[] 明文输出 162 163 * 164 165 * @param byteD 166 167 * @return 168 169 */ 170 171 private byte [] decryptByte( byte [] byteD) { 172 173 Cipher cipher; 174 175 byte [] byteFina = null ; 176 177 try { 178 179 cipher = Cipher.getInstance ( "DES" ); 180 181 cipher.init(Cipher. DECRYPT_MODE , key ); 182 183 byteFina = cipher.doFinal(byteD); 184 185 } catch (Exception e) { 186 187 throw new RuntimeException( 188 189 "Error initializing SqlMap class. Cause: " + e); 190 191 } finally { 192 193 cipher = null ; 194 195 } 196 197 return byteFina; 198 199 } 200 201 202 203 /** 204 205 * 文件 file 进行加密并保存目标文件 destFile 中 206 207 * 208 209 * @param file 210 211 * 要加密的文件 如 c:/test/srcFile.txt 212 213 * @param destFile 214 215 * 加密后存放的文件名 如 c:/ 加密后文件 .txt 216 217 */ 218 219 public void encryptFile(String file, String destFile) throws Exception { 220 221 Cipher cipher = Cipher.getInstance ( "DES" ); 222 223 // cipher.init(Cipher.ENCRYPT_MODE, getKey()); 224 225 cipher.init(Cipher. ENCRYPT_MODE , this . key ); 226 227 InputStream is = new FileInputStream(file); 228 229 OutputStream out = new FileOutputStream(destFile); 230 231 CipherInputStream cis = new CipherInputStream(is, cipher); 232 233 byte [] buffer = new byte [1024]; 234 235 int r; 236 237 while ((r = cis.read(buffer)) > 0) { 238 239 out.write(buffer, 0, r); 240 241 } 242 243 cis.close(); 244 245 is.close(); 246 247 out.close(); 248 249 } 250 251 252 253 /** 254 255 * 文件采用 DES 算法解密文件 256 257 * 258 259 * @param file 260 261 * 已加密的文件 如 c:/ 加密后文件 .txt * 262 * 解密后存放的文件名 如 c:/ test/ 解密后文件 .txt 263 264 */ 265 266 public void decryptFile(String file, String dest) throws Exception { 267 Cipher cipher = Cipher.getInstance ( "DES" ); 268 cipher.init(Cipher. DECRYPT_MODE , this.key ); 269 InputStream is = new FileInputStream(file); 270 OutputStream out = new FileOutputStream(dest); 271 CipherOutputStream cos = new CipherOutputStream(out, cipher); 272 byte [] buffer = new byte [1024]; 273 int r; 274 while ((r = is.read(buffer)) >= 0) { 275 cos.write(buffer, 0, r); 276 } 277 cos.close(); 278 out.close(); 279 is.close(); 280 } 281 282 public static void main(String[] args) throws Exception { 283 284 DES des = new DES(); 285 String str1 = "123456" ; 286 // DES 加密字符串 287 String str2 = des.encryptStr(str1); 288 // DES 解密字符串 289 String deStr = des.decryptStr(str2); 290 System. out .println( " 加密前: " + str1); 291 System. out .println( " 加密后: " + str2); 292 System. out .println( " 解密后: " + deStr); 293 } 294 }