public static string Encrypt(string publicKey_x, string publicKey_y, string data) { publicKey_x = xTool.ConvertTool.RemoveSpace(publicKey_x); publicKey_y = xTool.ConvertTool.RemoveSpace(publicKey_y); data = xTool.ConvertTool.RemoveSpace(data); if (data.Length % 2 != 0) { throw new Exception("Invalid Data"); } ECDSABase ecdsa = new ECDSABase(new ECCurve("SM2")); string c1, c2, c3, t; c1 = c2 = c3 = t = ""; string x2, y2; x2 = y2 = ""; //publicKey_x = "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A"; //publicKey_y = "75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42"; BigInteger x = BigInteger.Parse("00" + publicKey_x, System.Globalization.NumberStyles.AllowHexSpecifier); BigInteger y = BigInteger.Parse("00" + publicKey_y, System.Globalization.NumberStyles.AllowHexSpecifier); ECPoint pubkey = new ECPoint(x, y); bool isContinue = true; while (isContinue) { //BigInteger k = BigInteger.Parse("004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F", System.Globalization.NumberStyles.AllowHexSpecifier); //data = "656E6372797074696F6E207374616E64617264"; BigInteger k = ECCMath.RandomIntegerBelow(ECCurve.n); ECPoint point1 = ECCMath.ScalarMult(k, ECCurve.G); c1 = Util.ToHexString(point1.X, ECCurve.BitLength) + Util.ToHexString(point1.Y, ECCurve.BitLength); ECPoint S = ECCMath.ScalarMult(ECCurve.h, pubkey); if (ECCMath.IsInfinityPoint(S)) { throw new Exception("S is Infinity Point"); } ECPoint point2 = ECCMath.ScalarMult(k, pubkey); x2 = Util.ToHexString(point2.X, ECCurve.BitLength); y2 = Util.ToHexString(point2.Y, ECCurve.BitLength); t = KDF(x2 + y2, data.Length * 4); for (int i = 0; i < t.Length; i++) { if (t.Substring(i, 1) != "0") { isContinue = false; break; } } } c2 = XOR(data, t); c3 = ALG.GuoMi.SM3(x2 + data + y2); return(c1 + c3 + c2); }
public static bool VerifyPublickey(string pub_x, string pub_y, string curve = "") { pub_x = xTool.ConvertTool.RemoveSpace(pub_x); pub_y = xTool.ConvertTool.RemoveSpace(pub_y); ECDSABase ecdsa = new ECDSABase(new ECCurve(curve)); BigInteger x = BigInteger.Parse("00" + pub_x, System.Globalization.NumberStyles.AllowHexSpecifier); BigInteger y = BigInteger.Parse("00" + pub_y, System.Globalization.NumberStyles.AllowHexSpecifier); ECPoint p = new ECPoint(x, y); if (ECCMath.IsInfinityPoint(p)) { return(false); } if (!ECCMath.RangeBetween(x, BigInteger.One, ECCurve.p - 1)) { return(false); } if (!ECCMath.RangeBetween(y, BigInteger.One, ECCurve.p - 1)) { return(false); } if (!ECCMath.IsOnCurve(p)) { return(false); } ECPoint O = ECCMath.ScalarMult(ECCurve.n, p); if (!ECCMath.IsInfinityPoint(O)) { return(false); } //throw new Exception("O is NOT Infinity Point"); return(true); }
public static string Decrypt(string privateKey, string data) { privateKey = xTool.ConvertTool.RemoveSpace(privateKey); data = xTool.ConvertTool.RemoveSpace(data); if (data.Length % 2 != 0) { throw new Exception("Invalid Cipher"); } if (data.Length < 96 * 2) { throw new Exception("Invalid Cipher"); } ECDSABase ecdsa = new ECDSABase(new ECCurve("SM2")); string c1, c2, c3, t; c1 = data.Substring(0, 128); c3 = data.Substring(128, 64); c2 = data.Substring(128 + 64); t = ""; string x2, y2; x2 = y2 = ""; bool isZero = true; BigInteger x = BigInteger.Parse("00" + c1.Substring(0, 64), System.Globalization.NumberStyles.AllowHexSpecifier); BigInteger y = BigInteger.Parse("00" + c1.Substring(64), System.Globalization.NumberStyles.AllowHexSpecifier); ECPoint C1 = new ECPoint(x, y); if (!ECCMath.IsOnCurve(C1)) { throw new Exception("SM2 Decrypt Failed, C1 Not On Curve"); } ECPoint S = ECCMath.ScalarMult(ECCurve.h, C1); if (ECCMath.IsInfinityPoint(S)) { throw new Exception("S is Infinity Point"); } BigInteger prikey = BigInteger.Parse("00" + privateKey, System.Globalization.NumberStyles.AllowHexSpecifier); ECPoint point2 = ECCMath.ScalarMult(prikey, C1); x2 = Util.ToHexString(point2.X, ECCurve.BitLength); y2 = Util.ToHexString(point2.Y, ECCurve.BitLength); t = KDF(x2 + y2, c2.Length * 4); for (int i = 0; i < t.Length; i++) { if (t.Substring(i, 1) == "0") { continue; } else { isZero = false; break; } } if (isZero) { throw new Exception("t = 0"); } string plaindata = XOR(c2, t); string u = ALG.GuoMi.SM3(x2 + plaindata + y2); if (!u.Equals(c3, StringComparison.OrdinalIgnoreCase)) { throw new Exception("SM2 Decrypt Failed, u != C3"); } return(plaindata); }