MCPcopy Index your code
hub / github.com/SeanDragon/protools / decrypt

Method decrypt

security/src/main/java/pro/tools/security/sm/SM2.java:271–329  ·  view source on GitHub ↗

私钥解密 @param encryptData 密文数据字节数组 @param privateKey 解密私钥 @return

(byte[] encryptData, BigInteger privateKey)

Source from the content-addressed store, hash-verified

269 * @return
270 */
271 public String decrypt(byte[] encryptData, BigInteger privateKey) {
272
273 byte[] C1Byte = new byte[65];
274 System.arraycopy(encryptData, 0, C1Byte, 0, C1Byte.length);
275
276 ECPoint C1 = curve.decodePoint(C1Byte).normalize();
277
278 /*
279 * 计算椭圆曲线点 S = [h]C1 是否为无穷点
280 */
281 BigInteger h = ecc_bc_spec.getH();
282 if (h != null) {
283 ECPoint S = C1.multiply(h);
284 if (S.isInfinity()) {
285 throw new IllegalStateException();
286 }
287 }
288 /* 计算[dB]C1 = (x2, y2) */
289 ECPoint dBC1 = C1.multiply(privateKey).normalize();
290
291 /* 计算t = KDF(x2 || y2, klen) */
292 byte[] dBC1Bytes = dBC1.getEncoded(false);
293 int klen = encryptData.length - 65 - DIGEST_LENGTH;
294 byte[] t = KDF(dBC1Bytes, klen);
295 // DerivationFunction kdf = new KDF1BytesGenerator(new
296 // ShortenedDigest(new SHA256Digest(), DIGEST_LENGTH));
297 // if (debug)
298 // System.out.println("klen = " + klen);
299 // kdf.init(new ISO18033KDFParameters(dBC1Bytes));
300 // kdf.generateBytes(t, 0, t.length);
301
302 if (allZero(t)) {
303 System.err.println("all zero");
304 throw new IllegalStateException();
305 }
306
307 /* 5 计算M'=C2^t */
308 byte[] M = new byte[klen];
309 for (int i = 0; i < M.length; i++) {
310 M[i] = (byte) (encryptData[C1Byte.length + i] ^ t[i]);
311 }
312 /* 6 计算 u = Hash(x2 || M' || y2) 判断 u == C3是否成立 */
313 byte[] C3 = new byte[DIGEST_LENGTH];
314
315 System.arraycopy(encryptData, encryptData.length - DIGEST_LENGTH, C3, 0, DIGEST_LENGTH);
316 byte[] u = sm3hash(dBC1.getXCoord().toBigInteger().toByteArray(), M,
317 dBC1.getYCoord().toBigInteger().toByteArray());
318 if (Arrays.equals(u, C3)) {
319 try {
320 return new String(M, "UTF8");
321 } catch (UnsupportedEncodingException e) {
322 e.printStackTrace();
323 }
324 return null;
325 } else {
326 return null;
327 }
328

Callers

nothing calls this directly

Calls 5

KDFMethod · 0.95
allZeroMethod · 0.95
sm3hashMethod · 0.95
toByteArrayMethod · 0.80
equalsMethod · 0.45

Tested by

no test coverage detected