|
4 | 4 | import com.javayh.secure.transmit.constant.EncryptConstant; |
5 | 5 | import com.javayh.secure.transmit.encrypt.SecureTransmitTemplate; |
6 | 6 | import com.javayh.secure.transmit.encrypt.base.Base64Util; |
7 | | -import lombok.SneakyThrows; |
| 7 | +import com.javayh.secure.transmit.exception.EncryptionException; |
8 | 8 | import lombok.extern.slf4j.Slf4j; |
9 | 9 |
|
| 10 | +import javax.crypto.BadPaddingException; |
10 | 11 | import javax.crypto.Cipher; |
11 | | -import java.security.KeyFactory; |
12 | | -import java.security.PrivateKey; |
13 | | -import java.security.PublicKey; |
14 | | -import java.security.Security; |
| 12 | +import javax.crypto.IllegalBlockSizeException; |
| 13 | +import javax.crypto.NoSuchPaddingException; |
| 14 | +import java.security.*; |
| 15 | +import java.security.spec.InvalidKeySpecException; |
15 | 16 | import java.security.spec.PKCS8EncodedKeySpec; |
16 | 17 | import java.security.spec.X509EncodedKeySpec; |
17 | 18 | import java.util.Base64; |
@@ -39,47 +40,80 @@ public class EccEncryption implements SecureTransmitTemplate { |
39 | 40 | * @param plaintext 代加密的数据 |
40 | 41 | */ |
41 | 42 | @Override |
42 | | - public String encrypt(String pubKey, String plaintext) throws Exception { |
| 43 | + public String encrypt(String pubKey, String plaintext) { |
43 | 44 | PublicKey publicKey = stringToKey(pubKey); |
44 | | - Cipher cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC); |
45 | | - cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
46 | | - byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes()); |
47 | | - return Base64Util.encode(encryptedBytes); |
| 45 | + Cipher cipher = null; |
| 46 | + try { |
| 47 | + cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC); |
| 48 | + cipher.init(Cipher.ENCRYPT_MODE, publicKey); |
| 49 | + byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes()); |
| 50 | + return Base64Util.encode(encryptedBytes); |
| 51 | + } catch (NoSuchAlgorithmException | |
| 52 | + NoSuchProviderException | |
| 53 | + NoSuchPaddingException | |
| 54 | + IllegalBlockSizeException | |
| 55 | + BadPaddingException | |
| 56 | + InvalidKeyException e) { |
| 57 | + log.error("EccEncryption 解密失败 {}", e.getMessage()); |
| 58 | + throw new EncryptionException(e.getMessage()); |
| 59 | + } |
| 60 | + |
48 | 61 | } |
49 | 62 |
|
50 | 63 | /** |
51 | 64 | * @param priKey 解密私钥 {@link EccProperties#getPrivateKey()} |
52 | 65 | * @param encryptedData 待解密的数据 |
53 | 66 | */ |
54 | 67 | @Override |
55 | | - @SneakyThrows |
56 | 68 | public String decrypt(String priKey, String encryptedData) { |
57 | 69 | PrivateKey privateKey = stringToPrivateKey(priKey); |
58 | | - Cipher cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC); |
59 | | - cipher.init(Cipher.DECRYPT_MODE, privateKey); |
60 | | - byte[] encryptedBytes = Base64Util.decode(encryptedData); |
61 | | - byte[] decryptedBytes = cipher.doFinal(encryptedBytes); |
62 | | - return Base64Util.encode(decryptedBytes); |
| 70 | + Cipher cipher; |
| 71 | + try { |
| 72 | + cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC); |
| 73 | + cipher.init(Cipher.DECRYPT_MODE, privateKey); |
| 74 | + byte[] encryptedBytes = Base64Util.decode(encryptedData); |
| 75 | + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); |
| 76 | + return Base64Util.encode(decryptedBytes); |
| 77 | + } catch (NoSuchAlgorithmException | |
| 78 | + NoSuchProviderException | |
| 79 | + NoSuchPaddingException | |
| 80 | + IllegalBlockSizeException | |
| 81 | + BadPaddingException | |
| 82 | + InvalidKeyException e) { |
| 83 | + log.error("EccEncryption 解密失败 {}", e.getMessage()); |
| 84 | + throw new EncryptionException(e.getMessage()); |
| 85 | + } |
| 86 | + |
63 | 87 | } |
64 | 88 |
|
65 | 89 | /** |
66 | 90 | * 反算公钥 |
67 | 91 | */ |
68 | | - @SneakyThrows |
69 | 92 | private static PublicKey stringToKey(String keyString) { |
70 | 93 | byte[] keyBytes = Base64.getDecoder().decode(keyString); |
71 | | - KeyFactory keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC); |
72 | | - return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes)); |
| 94 | + KeyFactory keyFactory; |
| 95 | + try { |
| 96 | + keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC); |
| 97 | + return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes)); |
| 98 | + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) { |
| 99 | + log.error("EccEncryption 解密失败 {}", e.getMessage()); |
| 100 | + throw new EncryptionException(e.getMessage()); |
| 101 | + } |
73 | 102 | } |
74 | 103 |
|
75 | 104 | /** |
76 | 105 | * 反算私钥 |
77 | 106 | */ |
78 | | - @SneakyThrows |
79 | 107 | private static PrivateKey stringToPrivateKey(String keyString) { |
80 | 108 | byte[] keyBytes = Base64Util.decode(keyString); |
81 | | - KeyFactory keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC); |
82 | | - return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); |
| 109 | + KeyFactory keyFactory; |
| 110 | + try { |
| 111 | + keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC); |
| 112 | + return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); |
| 113 | + } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) { |
| 114 | + log.error("EccEncryption 解密失败 {}", e.getMessage()); |
| 115 | + throw new EncryptionException(e.getMessage()); |
| 116 | + } |
83 | 117 | } |
84 | 118 |
|
85 | 119 | } |
0 commit comments