2121
2222import org .bouncycastle .jce .provider .BouncyCastleProvider ;
2323
24- /**
25- * 密钥协商工具类
26- * 实现ECIES (Elliptic Curve Integrated Encryption Scheme) 密钥协商方案
27- */
2824public class KeyAgreementUtil {
2925 static {
3026 Security .addProvider (new BouncyCastleProvider ());
@@ -60,12 +56,6 @@ public String getSessionToken() {
6056 }
6157 }
6258
63- /**
64- * 生成ECIES密钥对并计算共享密钥
65- * @param publicKey 服务器公钥
66- * @return SessionTokenData 包含加密密钥、随机数和会话令牌
67- * @throws GeneralSecurityException 安全异常
68- */
6959 public static SessionData generateEciesKeyPair (PublicKey publicKey ) throws GeneralSecurityException {
7060 KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance ("EC" , "BC" );
7161 ECParameterSpec ecSpec = ((java .security .interfaces .ECPublicKey ) publicKey ).getParams ();
@@ -91,15 +81,6 @@ public static SessionData generateEciesKeyPair(PublicKey publicKey) throws Gener
9181 return new SessionData (cryptoKey , cryptoNonce , sessionToken );
9282 }
9383
94- /**
95- * 使用HKDF从共享密钥派生AES密钥和Nonce
96- * @param sharedSecret 共享密钥
97- * @param salt 盐值
98- * @param info 信息
99- * @param length 输出长度
100- * @return byte[] 派生的密钥材料
101- * @throws GeneralSecurityException 安全异常
102- */
10384 public static byte [] hkdf (byte [] sharedSecret , byte [] salt , byte [] info , int length )
10485 throws GeneralSecurityException {
10586 Mac hmacExtract = Mac .getInstance (HKDF_ALGORITHM );
@@ -132,11 +113,6 @@ public static byte[] hkdf(byte[] sharedSecret, byte[] salt, byte[] info, int len
132113 return result ;
133114 }
134115
135- /**
136- * 将EC公钥序列化为字节数组
137- * @param publicKey EC公钥
138- * @return byte[] 序列化后的公钥字节数组
139- */
140116 public static byte [] marshalEcPublicKey (java .security .interfaces .ECPublicKey publicKey ) {
141117 try {
142118 ECPoint point = publicKey .getW ();
@@ -158,12 +134,7 @@ public static byte[] marshalEcPublicKey(java.security.interfaces.ECPublicKey pub
158134 }
159135 }
160136
161- /**
162- * 将BigInteger转换为指定长度的无符号大端字节数组
163- * @param value BigInteger值
164- * @param length 输出字节数组长度
165- * @return byte[] 无符号大端字节数组
166- */
137+
167138 public static byte [] toUnsignedBigEndian (BigInteger value , int length ) {
168139 byte [] bytes = value .toByteArray ();
169140 byte [] result = new byte [length ];
@@ -179,11 +150,7 @@ public static byte[] toUnsignedBigEndian(BigInteger value, int length) {
179150 return result ;
180151 }
181152
182- /**
183- * 验证密文本格式是否有效
184- * @param ciphertext 密文本
185- * @return boolean 是否有效
186- */
153+
187154 public static boolean decryptValidate (String ciphertext ) {
188155 try {
189156 byte [] cipherBytes = ciphertext .getBytes (StandardCharsets .UTF_8 );
@@ -199,13 +166,7 @@ public static boolean decryptValidate(String ciphertext) {
199166 }
200167 }
201168
202- /**
203- * 使用密钥解密字符串
204- * @param key 加密密钥
205- * @param nonce 随机数
206- * @param encryptedContent 加密内容
207- * @return String 解密后的明文
208- */
169+
209170 public static String decryptStringWithKey (byte [] key , byte [] nonce , String encryptedContent ) {
210171 try {
211172 String content ;
@@ -226,13 +187,6 @@ public static String decryptStringWithKey(byte[] key, byte[] nonce, String encry
226187 }
227188 }
228189
229- /**
230- * 使用密钥加密字符串
231- * @param key 加密密钥
232- * @param nonce 随机数
233- * @param plaintext 明文
234- * @return String Base64编码的加密结果
235- */
236190 public static String encryptStringWithKey (byte [] key , byte [] nonce , String plaintext ) {
237191 try {
238192 Cipher cipher = javax .crypto .Cipher .getInstance ("AES/GCM/NoPadding" );
@@ -249,13 +203,7 @@ public static String encryptStringWithKey(byte[] key, byte[] nonce, String plain
249203 }
250204 }
251205
252- /**
253- * 解密Base64编码的AES-GCM加密字符串
254- * @param key 加密密钥
255- * @param nonce 随机数
256- * @param ciphertext Base64编码的密文
257- * @return String 解密后的明文
258- */
206+
259207 public static String aesGcmDecryptBase64String (byte [] key , byte [] nonce , String ciphertext ) {
260208 try {
261209 String cleaned = ciphertext .replaceAll ("\\ s" , "" );
@@ -275,14 +223,7 @@ public static String aesGcmDecryptBase64String(byte[] key, byte[] nonce, String
275223 }
276224 }
277225
278- /**
279- * AES-GCM解密核心方法
280- * @param key 加密密钥
281- * @param iv 初始化向量
282- * @param cipherBytes 密文字节数组
283- * @return String 解密后的明文
284- * @throws GeneralSecurityException 安全异常
285- */
226+
286227 public static String aesGcmDecrypt (byte [] key , byte [] iv , byte [] cipherBytes ) throws GeneralSecurityException {
287228 try {
288229 Cipher decryptor = Cipher .getInstance ("AES/GCM/NoPadding" , "BC" );
@@ -297,13 +238,6 @@ public static String aesGcmDecrypt(byte[] key, byte[] iv, byte[] cipherBytes) th
297238 }
298239 }
299240
300- /**
301- * 解密包含多个Base64块的密文
302- * @param key 加密密钥
303- * @param nonce 随机数
304- * @param ciphertext 包含多个Base64块的密文
305- * @return String 拼接后的解密结果
306- */
307241 public static String aesGcmDecryptBase64List (byte [] key , byte [] nonce , String ciphertext ) {
308242 List <String > result = new ArrayList <>();
309243
@@ -322,11 +256,7 @@ public static String aesGcmDecryptBase64List(byte[] key, byte[] nonce, String ci
322256 return String .join ("" , result );
323257 }
324258
325- /**
326- * 从字符串中提取所有Base64编码块
327- * @param ciphertext 原始字符串
328- * @return List<String> Base64块列表
329- */
259+
330260 public static List <String > extractBase64Blocks (String ciphertext ) {
331261 List <String > blocks = new ArrayList <>();
332262
@@ -344,13 +274,7 @@ public static List<String> extractBase64Blocks(String ciphertext) {
344274 return blocks ;
345275 }
346276
347- /**
348- * 处理边界情况的递归解密方法
349- * @param key 加密密钥
350- * @param nonce 随机数
351- * @param data 待解密数据
352- * @return String 解密结果
353- */
277+
354278 private static String decryptCornerCase (byte [] key , byte [] nonce , String data ) {
355279 for (int i = 20 ; i < data .length (); i += 4 ) {
356280 try {
0 commit comments