对加密使用的整理:
1.需要导入jar包
1'org.bouncycastle:bcprov-jdk16:1.46', 2'commons-codec:commons-codec:1.9',
2.没有找到对应的加密算法名称,定义了算法名称Enum:EncryptAlgorithmEnum.java
1/** 2 * 3 */ 4package org.rico.commons.utils.encrypt; 5 6/** 7 * @author rico 8 * 加密算法名称enum 9 */ 10public enum EncryptAlgorithmEnum { 11 MD5("MD5"), 12 SHA("SHA"), 13 SHA1("SHA1"), 14 SHA256("SHA256"), 15 16 DSA("DSA"), 17 RSA("RSA"), 18 19 AES("AES"), 20 DES("DES"), 21 DESede("DESede"); 22 23 24 private String name; 25 26 private EncryptAlgorithmEnum(String name) { 27 this.name = name; 28 } 29 30 public String value() { 31 return this.getName(); 32 } 33 34 public String getName() { 35 return name; 36 } 37 38 public void setName(String name) { 39 this.name = name; 40 } 41}
3.加密工具类EncryptUtil.java
1/** 2 * 3 */ 4package org.rc.common.encrypt; 5 6import java.io.UnsupportedEncodingException; 7import java.security.InvalidKeyException; 8import java.security.Key; 9import java.security.KeyFactory; 10import java.security.KeyPair; 11import java.security.KeyPairGenerator; 12import java.security.MessageDigest; 13import java.security.NoSuchAlgorithmException; 14import java.security.PrivateKey; 15import java.security.PublicKey; 16import java.security.SecureRandom; 17import java.security.Security; 18import java.security.spec.InvalidKeySpecException; 19import java.security.spec.PKCS8EncodedKeySpec; 20import java.security.spec.X509EncodedKeySpec; 21 22import javax.crypto.BadPaddingException; 23import javax.crypto.Cipher; 24import javax.crypto.IllegalBlockSizeException; 25import javax.crypto.KeyGenerator; 26import javax.crypto.NoSuchPaddingException; 27import javax.crypto.SecretKey; 28import javax.crypto.spec.SecretKeySpec; 29 30import org.apache.commons.codec.binary.Base64; 31import org.apache.commons.lang3.StringUtils; 32import org.bouncycastle.jce.provider.BouncyCastleProvider; 33 34/** 35 * @author rico 36 * md5加密工具 37 * message-digest algorithm 5 (信息-摘要算法),常用于文件校验 38 * 39 * SHA(Secure Hash Algorithm,安全散列算法) 40 * 数字签名等密码学应用中重要的工具,被广泛地应用于电子商务等信息安全领域。 41 * SHA与MD5通过碰撞法都被破解了,但是SHA仍然是公认的安全加密算法,较之MD5更为安全 42 * https://my.oschina.net/u/733161/blog/756814 43 */ 44public class EncryptUtil { 45 private static final String CHARSET_UTF8 = "UTF-8"; 46 47 static { 48 //TODO add Provider 49 // Adds a provider to the next position available. 50 Security.addProvider(new BouncyCastleProvider()); 51 } 52 53 54 //============================================================================ 55 // 单向加密 MD5,SHA 56 //============================================================================ 57 /** 58 * 单向加密 59 * @param value 60 * @return 61 * @throws NoSuchAlgorithmException 62 */ 63 public static byte[] digest(String algorithm, String value) throws NoSuchAlgorithmException { 64 if(StringUtils.isBlank(algorithm)) { 65 throw new NullPointerException("algorithm is null."); 66 } 67 if(StringUtils.isBlank(value)) { 68 throw new NullPointerException("digest value is null."); 69 } 70 71 MessageDigest digest = MessageDigest.getInstance(algorithm); 72 digest.update(value.getBytes()); 73 74 return digest.digest(); 75 } 76 77 /** 78 * 单向加密并转换为字符串格式 79 * @param value 80 * @return 81 * @throws NoSuchAlgorithmException 82 */ 83 public static String digestToString(String algorithm, String value) throws NoSuchAlgorithmException { 84 byte[] bytes = digest(algorithm, value); 85 return Base64.encodeBase64String(bytes); 86 } 87 88 /** 89 * md5 加密 90 * @param value 91 * @return 92 * @throws NoSuchAlgorithmException 93 */ 94 public static byte[] md5(String value) throws NoSuchAlgorithmException { 95 return digest(EncryptAlgorithmEnum.MD5.value(), value); 96 } 97 98 /** 99 * md5加密并转换为字符串格式 100 * @param value 101 * @return 102 * @throws NoSuchAlgorithmException 103 */ 104 public static String md5ToString(String value) throws NoSuchAlgorithmException { 105 return digestToString(EncryptAlgorithmEnum.MD5.value(), value); 106 } 107 108 /** 109 * sha加密 110 * @param value 111 * @return 112 * @throws NoSuchAlgorithmException 113 */ 114 public static byte[] sha(String value) throws NoSuchAlgorithmException { 115 return digest(EncryptAlgorithmEnum.SHA.value(), value); 116 } 117 118 /** 119 * sha加密并转换为字符串格式 120 * @param value 121 * @return 122 * @throws NoSuchAlgorithmException 123 */ 124 public static String shaToString(String value) throws NoSuchAlgorithmException { 125 return digestToString(EncryptAlgorithmEnum.SHA.value(), value); 126 } 127 128 129 //=============================================================================== 130 // 对称双向加密AES,DES 131 //=============================================================================== 132 /** 133 * 生成密钥 134 * @param algorithm 135 * @return 136 * @throws NoSuchAlgorithmException 137 */ 138 public static SecretKey generateSecretKey(String algorithm) throws NoSuchAlgorithmException { 139 if(StringUtils.isBlank(algorithm)) { 140 throw new NullPointerException("algorithm name is null"); 141 } 142 KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); 143 return keyGenerator.generateKey(); 144 } 145 146 /** 147 * 生成密钥字符串 148 * @param algorithm 149 * @return 150 * @throws NoSuchAlgorithmException 151 */ 152 public static String generateSecretKeyString(String algorithm) throws NoSuchAlgorithmException { 153 SecretKey secretKey = generateSecretKey(algorithm); 154 return Base64.encodeBase64String(secretKey.getEncoded()); 155 } 156 157 /** 158 * 生成AES密钥 159 * @return 160 * @throws NoSuchAlgorithmException 161 */ 162 public static SecretKey aesKey() throws NoSuchAlgorithmException { 163 return generateSecretKey(EncryptAlgorithmEnum.AES.value()); 164 } 165 166 /** 167 * 生成AES密钥字符串 168 * @return 169 * @throws NoSuchAlgorithmException 170 */ 171 public static String aesKeyString() throws NoSuchAlgorithmException { 172 return generateSecretKeyString(EncryptAlgorithmEnum.AES.value()); 173 } 174 175 /** 176 * 生成DES密钥 177 * @return 178 * @throws NoSuchAlgorithmException 179 */ 180 public static SecretKey desKey() throws NoSuchAlgorithmException { 181 return generateSecretKey(EncryptAlgorithmEnum.DES.value()); 182 } 183 184 /** 185 * 生成DES密钥字符串 186 * @return 187 * @throws NoSuchAlgorithmException 188 */ 189 public static String desKeyString() throws NoSuchAlgorithmException { 190 return generateSecretKeyString(EncryptAlgorithmEnum.DES.value()); 191 } 192 193 /** 194 * 解析SecretKey 195 * @param algorithm 196 * @param value 197 * @return 198 * @throws NoSuchAlgorithmException 199 */ 200 public static SecretKey parseSecretKey(String algorithm, String value) throws NoSuchAlgorithmException { 201 byte[] bytes = Base64.decodeBase64(value); 202 return parseSecretKey(algorithm, bytes); 203 } 204 205 /** 206 * 解析SecretKey 207 * @param algorithm 208 * @param bytes 209 * @return 210 */ 211 public static SecretKey parseSecretKey(String algorithm, byte[] bytes) { 212 SecretKey secretKey = new SecretKeySpec(bytes, 0, bytes.length, algorithm); 213 return secretKey; 214 } 215 216 /** 217 * 解析 AES SecretKey 218 * @param value 219 * @return 220 * @throws NoSuchAlgorithmException 221 */ 222 public static SecretKey parseAESSecretKey(String value) throws NoSuchAlgorithmException { 223 return parseSecretKey(EncryptAlgorithmEnum.AES.value(), value); 224 } 225 public static SecretKey parseAESSecretKey(byte[] byets) throws NoSuchAlgorithmException { 226 return parseSecretKey(EncryptAlgorithmEnum.AES.value(), byets); 227 } 228 229 /** 230 * 解析 DES SecretKey 231 * @param value 232 * @return 233 * @throws NoSuchAlgorithmException 234 */ 235 public static SecretKey parseDESSecretKey(String value) throws NoSuchAlgorithmException { 236 return parseSecretKey(EncryptAlgorithmEnum.DES.value(), value); 237 } 238 public static SecretKey parseDESSecretKey(byte[] bytes) throws NoSuchAlgorithmException { 239 return parseSecretKey(EncryptAlgorithmEnum.DES.value(), bytes); 240 } 241 242 /** 243 * 对称加密 244 * @param algorithm 245 * @param key 246 * @param value 247 * @return 248 * @throws NoSuchAlgorithmException 249 * @throws NoSuchPaddingException 250 * @throws InvalidKeyException 251 * @throws IllegalBlockSizeException 252 * @throws BadPaddingException 253 */ 254 public static byte[] encryptSymmetric(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException, 255 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 256 if(StringUtils.isBlank(algorithm)) { 257 throw new NullPointerException("algorithm name is null"); 258 } 259 if(StringUtils.isBlank(value)) { 260 throw new NullPointerException("encrypt value is null"); 261 } 262 263 Cipher cipher = Cipher.getInstance(algorithm); 264 cipher.init(Cipher.ENCRYPT_MODE, key); 265 return cipher.doFinal(value.getBytes()); 266 } 267 public static String encryptSymmetricString(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException, 268 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 269 byte[] bytes = encryptSymmetric(algorithm, key, value); 270 return Base64.encodeBase64String(bytes); 271 } 272 273 /** 274 * 对称解密 275 * @param algorithm 276 * @param key 277 * @param value 278 * @return 279 * @throws NoSuchAlgorithmException 280 * @throws NoSuchPaddingException 281 * @throws InvalidKeyException 282 * @throws IllegalBlockSizeException 283 * @throws BadPaddingException 284 * @throws UnsupportedEncodingException 285 */ 286 public static byte[] decryptSymmetric(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException, 287 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 288 289 if(StringUtils.isBlank(algorithm)) { 290 throw new NullPointerException("algorithm name is null"); 291 } 292 if(StringUtils.isBlank(value)) { 293 throw new NullPointerException("decrypt value is null"); 294 } 295 296 Cipher cipher = Cipher.getInstance(algorithm); 297 cipher.init(Cipher.DECRYPT_MODE, key); 298 299 byte[] decodeBytes = Base64.decodeBase64(value); 300 return cipher.doFinal(decodeBytes); 301 } 302 public static String decryptSymmetricString(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException, 303 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 304 byte[] bytes = decryptSymmetric(algorithm, key, value); 305 return new String(bytes, CHARSET_UTF8); 306 } 307 308 /** 309 * DES对称加密 310 * @param key 311 * @param value 312 * @return 313 * @throws InvalidKeyException 314 * @throws NoSuchAlgorithmException 315 * @throws NoSuchPaddingException 316 * @throws IllegalBlockSizeException 317 * @throws BadPaddingException 318 */ 319 public static byte[] desEncrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 320 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { 321 return encryptSymmetric(EncryptAlgorithmEnum.DES.value(), key, value); 322 } 323 public static String desEncryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 324 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { 325 return encryptSymmetricString(EncryptAlgorithmEnum.DES.value(), key, value); 326 } 327 328 /** 329 * DES对称解密 330 * @param key 331 * @param value 332 * @return 333 * @throws InvalidKeyException 334 * @throws NoSuchAlgorithmException 335 * @throws NoSuchPaddingException 336 * @throws IllegalBlockSizeException 337 * @throws BadPaddingException 338 * @throws UnsupportedEncodingException 339 */ 340 public static String desDecryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 341 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 342 return decryptSymmetricString(EncryptAlgorithmEnum.DES.value(), key, value); 343 } 344 public static byte[] desDecrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 345 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 346 return decryptSymmetric(EncryptAlgorithmEnum.DES.value(), key, value); 347 } 348 349 /** 350 * AES对称加密 351 * @param key 352 * @param value 353 * @return 354 * @throws InvalidKeyException 355 * @throws NoSuchAlgorithmException 356 * @throws NoSuchPaddingException 357 * @throws IllegalBlockSizeException 358 * @throws BadPaddingException 359 */ 360 public static byte[] aesEncrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 361 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { 362 return encryptSymmetric(EncryptAlgorithmEnum.AES.value(), key, value); 363 } 364 public static String aesEncryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 365 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { 366 return encryptSymmetricString(EncryptAlgorithmEnum.AES.value(), key, value); 367 } 368 369 /** 370 * AES对称解密 371 * @param key 372 * @param value 373 * @return 374 * @throws InvalidKeyException 375 * @throws NoSuchAlgorithmException 376 * @throws NoSuchPaddingException 377 * @throws IllegalBlockSizeException 378 * @throws BadPaddingException 379 * @throws UnsupportedEncodingException 380 */ 381 public static byte[] aesDecrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 382 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 383 return decryptSymmetric(EncryptAlgorithmEnum.AES.value(), key, value); 384 } 385 public static String aesDecryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 386 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 387 return decryptSymmetricString(EncryptAlgorithmEnum.AES.value(), key, value); 388 } 389 390 391 //================================================================================= 392 // 非对称双向加密RSA/DSA 393 //===========================非对称双向加密========================================= 394 /** 395 * 生成密钥对 396 * @param algorithm 397 * @param keysize 398 * @return 399 * @throws NoSuchAlgorithmException 400 */ 401 public static KeyPair generateKeyPair(String algorithm, int keysize) throws NoSuchAlgorithmException { 402 if(StringUtils.isBlank(algorithm)) { 403 throw new NullPointerException("algorithm name is null"); 404 } 405 if(keysize <= 0) { 406 throw new IllegalArgumentException("keysize should be great than zero."); 407 } 408 409 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); 410 keyPairGenerator.initialize(keysize); 411 412 return keyPairGenerator.generateKeyPair(); 413 } 414 415 /** 416 * 生成密钥对 417 * @param algorithm 418 * @param keysize 419 * @param random 420 * @return 421 * @throws NoSuchAlgorithmException 422 */ 423 public static KeyPair generateKeyPair(String algorithm, int keysize, SecureRandom random) throws NoSuchAlgorithmException { 424 if(StringUtils.isBlank(algorithm)) { 425 throw new NullPointerException("algorithm name is null"); 426 } 427 if(keysize <= 0) { 428 throw new IllegalArgumentException("keysize should be great than zero."); 429 } 430 431 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm); 432 keyPairGenerator.initialize(keysize, random); 433 434 return keyPairGenerator.generateKeyPair(); 435 } 436 437 /** 438 * 获取RSA密钥对 439 * @param keysize 440 * @return 441 * @throws NoSuchAlgorithmException 442 */ 443 public static KeyPair rsaKeyPair(int keysize) throws NoSuchAlgorithmException { 444 return generateKeyPair(EncryptAlgorithmEnum.RSA.value(), keysize); 445 } 446 public static KeyPair rsaKeyPair(int keysize, SecureRandom random) throws NoSuchAlgorithmException { 447 return generateKeyPair(EncryptAlgorithmEnum.RSA.value(), keysize, random); 448 } 449 450 /** 451 * 获取DSA密钥对 452 * @param keysize 453 * @return 454 * @throws NoSuchAlgorithmException 455 */ 456 public static KeyPair dsaKeyPair(int keysize) throws NoSuchAlgorithmException { 457 return generateKeyPair(EncryptAlgorithmEnum.DSA.value(), keysize); 458 } 459 public static KeyPair dsaKeyPair(int keysize, SecureRandom random) throws NoSuchAlgorithmException { 460 return generateKeyPair(EncryptAlgorithmEnum.DSA.value(), keysize, random); 461 } 462 463 public static String keyToString(Key key) { 464 return Base64.encodeBase64String(key.getEncoded()); 465 } 466 467 /** 468 * 非对称加密 469 * @param algorithm 470 * @param key 471 * @param value 472 * @return 473 * @throws NoSuchAlgorithmException 474 * @throws NoSuchPaddingException 475 * @throws InvalidKeyException 476 * @throws IllegalBlockSizeException 477 * @throws BadPaddingException 478 * @throws UnsupportedEncodingException 479 */ 480 public static byte[] encryptNonSymmetric(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 481 if(StringUtils.isBlank(algorithm)) { 482 throw new NullPointerException("algorithm name is null"); 483 } 484 if(StringUtils.isBlank(value)) { 485 throw new NullPointerException("encrypt value is null"); 486 } 487 488 Cipher cipher = Cipher.getInstance(algorithm); 489 cipher.init(Cipher.ENCRYPT_MODE, key); 490 return cipher.doFinal(value.getBytes(CHARSET_UTF8)); 491 } 492 public static String encryptNonSymmetricString(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 493 byte[] bytes = encryptNonSymmetric(algorithm, key, value); 494 return Base64.encodeBase64String(bytes); 495 } 496 497 /** 498 * 非对称解密 499 * @param algorithm 500 * @param key 501 * @param value 502 * @return 503 * @throws NoSuchAlgorithmException 504 * @throws NoSuchPaddingException 505 * @throws InvalidKeyException 506 * @throws IllegalBlockSizeException 507 * @throws BadPaddingException 508 * @throws UnsupportedEncodingException 509 */ 510 public static byte[] decryptNonSymmetric(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, 511 InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 512 if(StringUtils.isBlank(algorithm)) { 513 throw new NullPointerException("algorithm name is null"); 514 } 515 if(StringUtils.isBlank(value)) { 516 throw new NullPointerException("decrypt value is null"); 517 } 518 519 byte[] bytes = Base64.decodeBase64(value); 520 Cipher cipher = Cipher.getInstance(algorithm); 521 cipher.init(Cipher.DECRYPT_MODE, key); 522 return cipher.doFinal(bytes); 523 } 524 public static String decryptNonSymmetricString(String algorithm, Key key, String value) throws NoSuchAlgorithmException, 525 NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 526 byte[] decryptBytes = decryptNonSymmetric(algorithm, key, value); 527 return new String(decryptBytes, CHARSET_UTF8); 528 } 529 530 /** 531 * RSA加密 532 * @param key 533 * @param value 534 * @return 535 * @throws InvalidKeyException 536 * @throws NoSuchAlgorithmException 537 * @throws NoSuchPaddingException 538 * @throws IllegalBlockSizeException 539 * @throws BadPaddingException 540 * @throws UnsupportedEncodingException 541 */ 542 public static byte[] rsaEncrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 543 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 544 return encryptNonSymmetric(EncryptAlgorithmEnum.RSA.value(), key, value); 545 } 546 public static String rsaEncryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 547 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 548 return encryptNonSymmetricString(EncryptAlgorithmEnum.RSA.value(), key, value); 549 } 550 551 /** 552 * RSA解密 553 * @param key 554 * @param value 555 * @return 556 * @throws InvalidKeyException 557 * @throws NoSuchAlgorithmException 558 * @throws NoSuchPaddingException 559 * @throws IllegalBlockSizeException 560 * @throws BadPaddingException 561 * @throws UnsupportedEncodingException 562 */ 563 public static byte[] rsaDecrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 564 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 565 return decryptNonSymmetric(EncryptAlgorithmEnum.RSA.value(), key, value); 566 } 567 public static String rsaDecryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 568 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 569 return decryptNonSymmetricString(EncryptAlgorithmEnum.RSA.value(), key, value); 570 } 571 572 /** 573 * DSA加密 574 * @param key 575 * @param value 576 * @return 577 * @throws InvalidKeyException 578 * @throws NoSuchAlgorithmException 579 * @throws NoSuchPaddingException 580 * @throws IllegalBlockSizeException 581 * @throws BadPaddingException 582 * @throws UnsupportedEncodingException 583 */ 584 public static byte[] dsaEncrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 585 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 586 return encryptNonSymmetric(EncryptAlgorithmEnum.DSA.value(), key, value); 587 } 588 public static String dsaEncryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, 589 IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 590 return encryptNonSymmetricString(EncryptAlgorithmEnum.DSA.value(), key, value); 591 } 592 593 /** 594 * DSA解密 595 * @param algorithm 596 * @param key 597 * @param value 598 * @return 599 * @throws InvalidKeyException 600 * @throws NoSuchAlgorithmException 601 * @throws NoSuchPaddingException 602 * @throws IllegalBlockSizeException 603 * @throws BadPaddingException 604 * @throws UnsupportedEncodingException 605 */ 606 public static byte[] dsaDecrypt(String algorithm, Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 607 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 608 return decryptNonSymmetric(EncryptAlgorithmEnum.DSA.value(), key, value); 609 } 610 public static String dsaDecryptString(String algorithm, Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, 611 NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 612 return decryptNonSymmetricString(EncryptAlgorithmEnum.DSA.value(), key, value); 613 } 614 615 /** 616 * 解析私钥 617 * @param keyFactory 618 * @param keyValue 619 * @return 620 * @throws InvalidKeySpecException 621 */ 622 public static PrivateKey parsePrivateKey(KeyFactory keyFactory, byte[] bytes) throws InvalidKeySpecException { 623 // Creates a new PKCS8EncodedKeySpec with the given encoded key. 624 // PKCS8EncodedKeySpec represents the ASN.1 encoding of a private key, encoded according to the ASN.1 type PrivateKeyInfo. 625 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes); 626 PrivateKey key = (PrivateKey) keyFactory.generatePrivate(keySpec); 627 628 return key; 629 } 630 public static PrivateKey parsePrivateKey(KeyFactory keyFactory, String keyValue) throws InvalidKeySpecException { 631 return parsePrivateKey(keyFactory, Base64.decodeBase64(keyValue.getBytes())); 632 } 633 634 /** 635 * 解析私钥(RSA) 636 * @param keyValue 637 * @return 638 * @throws InvalidKeySpecException 639 * @throws NoSuchAlgorithmException 640 */ 641 public static PrivateKey parseRSAPrivateKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException { 642 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value()); 643 return parsePrivateKey(keyFactory, bytes); 644 } 645 public static PrivateKey parseRSAPrivateKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException { 646 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value()); 647 return parsePrivateKey(keyFactory, keyValue); 648 } 649 650 /** 651 * 解析私钥(DSA) 652 * @param keyValue 653 * @return 654 * @throws InvalidKeySpecException 655 * @throws NoSuchAlgorithmException 656 */ 657 public static PrivateKey parseDSAPrivateKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException { 658 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value()); 659 return parsePrivateKey(keyFactory, bytes); 660 } 661 public static PrivateKey parseDSAPrivateKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException { 662 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value()); 663 return parsePrivateKey(keyFactory, keyValue); 664 } 665 666 /** 667 * 解析公钥 668 * @param keyFactory 669 * @param keyValue 670 * @return 671 * @throws InvalidKeySpecException 672 */ 673 public static PublicKey parsePublicKey(KeyFactory keyFactory, byte[] bytes) throws InvalidKeySpecException { 674 // Creates a new X509EncodedKeySpec with the given encoded key. 675 // X509EncodedKeySpec represents the ASN.1 encoding of a public key, encoded according to the ASN.1 type SubjectPublicKeyInfo. 676 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); 677 PublicKey key = keyFactory.generatePublic(keySpec); 678 679 return key; 680 } 681 public static PublicKey parsePublicKey(KeyFactory keyFactory, String keyValue) throws InvalidKeySpecException { 682 return parsePublicKey(keyFactory, Base64.decodeBase64(keyValue.getBytes())); 683 } 684 685 /** 686 * 解析公钥(RSA) 687 * @param keyValue 688 * @return 689 * @throws InvalidKeySpecException 690 * @throws NoSuchAlgorithmException 691 */ 692 public static PublicKey parseRSAPublicKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException { 693 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value()); 694 return parsePublicKey(keyFactory, bytes); 695 } 696 public static PublicKey parseRSAPublicKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException { 697 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value()); 698 return parsePublicKey(keyFactory, keyValue); 699 } 700 701 /** 702 * 解析公钥(DSA) 703 * @param keyValue 704 * @return 705 * @throws InvalidKeySpecException 706 * @throws NoSuchAlgorithmException 707 */ 708 public static PublicKey parseDSAPublicKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException { 709 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value()); 710 return parsePublicKey(keyFactory, bytes); 711 } 712 public static PublicKey parseDSAPublicKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException { 713 KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value()); 714 return parsePublicKey(keyFactory, keyValue); 715 } 716 717 718 /** 719 * 获取秘钥工厂类 720 * @param algorithm 721 * @return 722 * @throws NoSuchAlgorithmException 723 */ 724 public static KeyFactory getKeyFactory(String algorithm) throws NoSuchAlgorithmException { 725 return KeyFactory.getInstance(algorithm); 726 } 727 728 /** 729 * 获取秘钥工厂类(RSA) 730 * @param algorithm 731 * @return 732 * @throws NoSuchAlgorithmException 733 */ 734 public static KeyFactory getRSAKeyFactory() throws NoSuchAlgorithmException { 735 return getKeyFactory(EncryptAlgorithmEnum.RSA.value()); 736 } 737 738 /** 739 * 获取秘钥工厂类(DSA) 740 * @param algorithm 741 * @return 742 * @throws NoSuchAlgorithmException 743 */ 744 public static KeyFactory getDSAKeyFactory() throws NoSuchAlgorithmException { 745 return getKeyFactory(EncryptAlgorithmEnum.DSA.value()); 746 } 747 748}
附: