|
3 | 3 | import java.io.IOException;
|
4 | 4 | import java.math.BigInteger;
|
5 | 5 | import java.security.SecureRandom;
|
6 |
| -import org.bouncycastle.asn1.*; |
| 6 | +import org.bouncycastle.asn1.ASN1Encoding; |
| 7 | +import org.bouncycastle.asn1.ASN1Integer; |
| 8 | +import org.bouncycastle.asn1.ASN1Primitive; |
| 9 | +import org.bouncycastle.asn1.ASN1Sequence; |
| 10 | +import org.bouncycastle.asn1.DERSequence; |
7 | 11 | import org.bouncycastle.math.ec.ECCurve;
|
8 | 12 | import org.bouncycastle.math.ec.ECPoint;
|
9 | 13 | import org.fisco.bcos.web3j.crypto.gm.sm2.crypto.digests.SM3Digest;
|
@@ -114,8 +118,10 @@ public static byte[] decrypt(String pvk, byte[] data) {
|
114 | 118 | ECPoint s =
|
115 | 119 | calculateS(
|
116 | 120 | new BigInteger(pbX, 16), new BigInteger(pbY, 16), new BigInteger(pvk, 16));
|
117 |
| - BigInteger x2 = s.getAffineXCoord().toBigInteger(); |
118 |
| - BigInteger y2 = s.getAffineYCoord().toBigInteger(); |
| 121 | + |
| 122 | + ECPoint ecPoint = s.normalize(); |
| 123 | + BigInteger x2 = ecPoint.getAffineXCoord().toBigInteger(); |
| 124 | + BigInteger y2 = ecPoint.getAffineYCoord().toBigInteger(); |
119 | 125 |
|
120 | 126 | byte[] t = kdf(x2, y2, c2.length);
|
121 | 127 | if (isEmpty(t)) {
|
@@ -183,11 +189,13 @@ private static ECPoint calculateS(BigInteger x1, BigInteger y1, BigInteger k) {
|
183 | 189 | * 第4步:计算 [k]Pb=(x2,y2)
|
184 | 190 | */
|
185 | 191 | private static BigInteger calculateX2(ECPoint s) {
|
186 |
| - return s.getAffineXCoord().toBigInteger(); |
| 192 | + ECPoint ecPoint = s.normalize(); |
| 193 | + return ecPoint.getAffineXCoord().toBigInteger(); |
187 | 194 | }
|
188 | 195 |
|
189 | 196 | private static BigInteger calculateY2(ECPoint s) {
|
190 |
| - return s.getAffineYCoord().toBigInteger(); |
| 197 | + ECPoint ecPoint = s.normalize(); |
| 198 | + return ecPoint.getAffineYCoord().toBigInteger(); |
191 | 199 | }
|
192 | 200 |
|
193 | 201 | /*
|
@@ -269,8 +277,9 @@ private static byte[] calculateC3(BigInteger x2, byte[] m, BigInteger y2) {
|
269 | 277 | private static byte[] getC(ECPoint c1, byte[] c3, byte[] c2) {
|
270 | 278 | byte[] c = new byte[64 + c3.length + c2.length];
|
271 | 279 |
|
272 |
| - byte[] c1xBuf = padding(c1.getAffineXCoord().toBigInteger().toByteArray()); |
273 |
| - byte[] c1yBuf = padding(c1.getAffineYCoord().toBigInteger().toByteArray()); |
| 280 | + ECPoint ecPoint = c1.normalize(); |
| 281 | + byte[] c1xBuf = padding(ecPoint.getAffineXCoord().toBigInteger().toByteArray()); |
| 282 | + byte[] c1yBuf = padding(ecPoint.getAffineYCoord().toBigInteger().toByteArray()); |
274 | 283 |
|
275 | 284 | System.arraycopy(c1xBuf, 0, c, 0, 32);
|
276 | 285 | System.arraycopy(c1yBuf, 0, c, 32, 32);
|
@@ -367,7 +376,8 @@ private static BigInteger[] SignSm3(byte[] hash, BigInteger privateKeyS) {
|
367 | 376 | do {
|
368 | 377 | k = createRandom();
|
369 | 378 | kp = g256.multiply(k);
|
370 |
| - r = e.add(kp.getAffineXCoord().toBigInteger()); |
| 379 | + ECPoint ecPoint = kp.normalize(); |
| 380 | + r = e.add(ecPoint.getAffineXCoord().toBigInteger()); |
371 | 381 | r = r.mod(n);
|
372 | 382 | } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n));
|
373 | 383 | BigInteger da_1 = userD.add(BigInteger.ONE).modInverse(n);
|
@@ -415,8 +425,9 @@ private static boolean verify(byte[] msg, byte[] signData, BigInteger biX, BigIn
|
415 | 425 | BigInteger t = r.add(s).mod(n);
|
416 | 426 | if (t.equals(BigInteger.ZERO)) return false;
|
417 | 427 | ECPoint x1y1 = g256.multiply(s);
|
| 428 | + ECPoint ecPoint = x1y1.normalize(); |
418 | 429 | x1y1 = x1y1.add(userKey.multiply(t));
|
419 |
| - BigInteger R = e.add(x1y1.getAffineXCoord().toBigInteger()).mod(n); |
| 430 | + BigInteger R = e.add(ecPoint.getAffineXCoord().toBigInteger()).mod(n); |
420 | 431 |
|
421 | 432 | return r.equals(R);
|
422 | 433 | }
|
@@ -460,8 +471,10 @@ private static byte[] sm2GetZ(byte[] userId, ECPoint publicKey) {
|
460 | 471 | sm3BlockUpdate(sm3, getEncoded(b));
|
461 | 472 | sm3BlockUpdate(sm3, getEncoded(gx));
|
462 | 473 | sm3BlockUpdate(sm3, getEncoded(gy));
|
463 |
| - sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineXCoord().toBigInteger())); |
464 |
| - sm3BlockUpdate(sm3, getEncoded(publicKey.getAffineYCoord().toBigInteger())); |
| 474 | + |
| 475 | + ECPoint ecPoint = publicKey.normalize(); |
| 476 | + sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineXCoord().toBigInteger())); |
| 477 | + sm3BlockUpdate(sm3, getEncoded(ecPoint.getAffineYCoord().toBigInteger())); |
465 | 478 |
|
466 | 479 | byte[] md = new byte[sm3.getDigestSize()];
|
467 | 480 | sm3.doFinal(md, 0);
|
|
0 commit comments