private void button3_Click(object sender, EventArgs e) { FpPoint G = (FpPoint)parameters.G; BigInteger k = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); tbk.Text = k.ToString(16); FpPoint C = (FpPoint)G.Multiply(k); tbCX.Text = C.X.ToBigInteger().ToString(16); tbCY.Text = C.Y.ToBigInteger().ToString(16); }
private void button5_Click(object sender, EventArgs e) { FpPoint G = (FpPoint)parameters.G; FpPoint Q = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))); FpPoint C = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCY))); BigInteger mu = TextBoxToBigInteger16(tbVoterMu); BigInteger epsilon = TextBoxToBigInteger16(tbVoterEpsilon); BigInteger delta = TextBoxToBigInteger16(tbVoterDelta); BigInteger tau = TextBoxToBigInteger16(tbVoterTau); BigInteger q = parameters.N; FpPoint Cs = (FpPoint)G.Multiply(epsilon).Add(Q.Multiply(mu)).Add(C.Multiply(delta.ModInverse(q))); tbCsX.Text = Cs.X.ToBigInteger().ToString(16); tbCsY.Text = Cs.Y.ToBigInteger().ToString(16); }
private void button3_Click(object sender, EventArgs e) //генерация K - G etc. { FpPoint G = (FpPoint)parameters.G; BigInteger k = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); tbk.Text = k.ToString(16); FpPoint C = (FpPoint)G.Multiply(k); tbCX.Text = C.X.ToBigInteger().ToString(16); tbCY.Text = C.Y.ToBigInteger().ToString(16); BigInteger mu = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); BigInteger epsilon = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); BigInteger delta = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); BigInteger tau = (new BigInteger(random.Next(1, parameters.N.BitCount - 1), random)).Add(BigInteger.One); tbVoterMu.Text = mu.ToString(16); tbVoterEpsilon.Text = epsilon.ToString(16); tbVoterDelta.Text = delta.ToString(16); tbVoterTau.Text = tau.ToString(16); }
public FpPoint Exctract(string ID, bool decrypt = false) { if (decrypt) { string sStr = File.ReadAllText("mk"); s = int.Parse(sStr); } // y^2 = x^3 + 117050x^2 + x BigInteger x = GeneralFunctions.H1hash(ID, p); BigInteger y = x.Pow(3).Add(x.Pow(2).Multiply(new BigInteger("117050", 10))).Add(x).Pow(2).ModInverse(p); FpFieldElement x_Qid = new FpFieldElement(q, x); FpFieldElement y_Qid = new FpFieldElement(q, y); FpPoint Qid = new FpPoint(E, x_Qid, y_Qid); FpPoint d_id = (FpPoint)Qid.Multiply(new BigInteger(s.ToString(), 10)); // privatni ključ return(d_id); }
public Setup() { n = 3; do { Random r = new Random(); s = r.Next(1, int.MaxValue - 1); } while (s == 0); // p i q p = new BigInteger("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10); //q = p.Pow(n); q = p; k = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494337", 10); // E - krivulja secp256k1 - y ^ 2 = x ^ 3 + 0*x + 7 BigInteger a = new BigInteger("0", 10); BigInteger b = new BigInteger("7", 10); E = new FpCurve(q, a, b); // P BigInteger x1 = new BigInteger("55066263022277343669578718895168534326250603453777594175500187360389116729240", 10); BigInteger y1 = new BigInteger("32670510020758816978083085130507043184471273380659243275938904335757337482424", 10); FpFieldElement x = (FpFieldElement)E.FromBigInteger(x1); // new FpFieldElement(q, x1); FpFieldElement y = (FpFieldElement)E.FromBigInteger(y1); // new FpFieldElement(q, y1); P = new FpPoint(E, x, y); BigInteger mtp = new BigInteger(s.ToString(), 10); Ppub = (FpPoint)P.Multiply(mtp); File.WriteAllText("mk", s.ToString() + Environment.NewLine); }
public Cypher GetCypher(string message) { BigInteger x = GeneralFunctions.H1hash(ID, prim); BigInteger y = x.Pow(3).Add(new BigInteger("7", 10)).Pow(2).ModInverse(prim); FpFieldElement x_Qid = new FpFieldElement(E.Q, x); FpFieldElement y_Qid = new FpFieldElement(E.Q, y); FpPoint Qid = new FpPoint(E, x_Qid, y_Qid); int r = 0; do { Random rnd = new Random(); r = rnd.Next(1, int.MaxValue - 1); } while (r == 0); FpPoint rP = (FpPoint)P.Multiply(new BigInteger(r.ToString(), 10)); BigInteger gid = GeneralFunctions.Pair(Qid, Ppub, k, prim); gid = gid.ModPow(new BigInteger(r.ToString(), 10), prim); char[] M = message.ToCharArray(); char[] cArray = new char[M.Length]; char[] hash = GeneralFunctions.H2hash(gid, prim).ToCharArray(); for (int i = 0; i < message.Length; i++) { cArray[i] = (char)(M[i] ^ hash[i % hash.Length]); } string c = new String(cArray); return(new Cypher { U = rP, V = c }); }
public static bool Verify(byte[] msg, byte[] signData, byte[] certData) { var x5092 = new System.Security.Cryptography.X509Certificates.X509Certificate2(certData); byte[] certPK = x5092.GetPublicKey(); certPK = ByteUtils.SubBytes(certPK, 1, 64); byte[] certPKX = ByteUtils.SubBytes(certPK, certPK.Length - 32 - 32, 32); byte[] certPKY = ByteUtils.SubBytes(certPK, certPK.Length - 32, 32); System.String strcertPKX = ByteUtils.ByteArrayToHex(certPKX); System.String strcertPKY = ByteUtils.ByteArrayToHex(certPKY); BigInteger biX = new BigInteger(strcertPKX, 16); BigInteger biY = new BigInteger(strcertPKY, 16); ECFieldElement x = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biX); ECFieldElement y = new FpFieldElement(SM2CryptoServiceProvider.Instance.ecc_p, biY); ECPoint userKey = new FpPoint(SM2CryptoServiceProvider.Instance.ecc_curve, x, y); SM3Digest sm3 = new SM3Digest(); byte[] z = SM2CryptoServiceProvider.Instance.Sm2GetZ(Encoding.Default.GetBytes(SM2CryptoServiceProvider.Instance.userId), userKey); sm3.BlockUpdate(z, 0, z.Length); byte[] p = msg; sm3.BlockUpdate(p, 0, p.Length); byte[] md = new byte[32]; sm3.DoFinal(md, 0); byte[] btR = ByteUtils.SubBytes(signData, 0, 32); byte[] btS = ByteUtils.SubBytes(signData, 32, 32); System.String strR = ByteUtils.ByteArrayToHex(btR); System.String strS = ByteUtils.ByteArrayToHex(btS); BigInteger r = new BigInteger(strR, 16); BigInteger s = new BigInteger(strS, 16); // e_ BigInteger e = new BigInteger(1, md); // t BigInteger t = r.Add(s).Mod(SM2CryptoServiceProvider.Instance.ecc_n); if (t.Equals(BigInteger.Zero)) { return(false); } // x1y1 ECPoint x1y1 = SM2CryptoServiceProvider.Instance.ecc_point_g.Multiply(s); x1y1 = x1y1.Add(userKey.Multiply(t)); // R BigInteger R = e.Add(x1y1.X.ToBigInteger()).Mod(SM2CryptoServiceProvider.Instance.ecc_n); return(r.Equals(R)); }
public override GroupElement Exponentiate(BigInteger exponent) { return(new ECElement(point.Multiply(exponent) as FpPoint)); }
private void button2_Click(object sender, EventArgs ea) { FpPoint G = (FpPoint)parameters.G; FpPoint Q = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))); FpPoint C = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbCY))); BigInteger mu = TextBoxToBigInteger16(tbVoterMu); BigInteger epsilon = TextBoxToBigInteger16(tbVoterEpsilon); BigInteger delta = TextBoxToBigInteger16(tbVoterDelta); BigInteger tau = TextBoxToBigInteger16(tbVoterTau); BigInteger q = parameters.N; FpPoint Cs = (FpPoint)G.Multiply(epsilon).Add(Q.Multiply(mu)).Add(C.Multiply(delta.ModInverse(q))); tbCsX.Text = Cs.X.ToBigInteger().ToString(16); tbCsY.Text = Cs.Y.ToBigInteger().ToString(16); BigInteger H = TextBoxToBigInteger16(tbH); //BigInteger q = parameters.N; //BigInteger mu = TextBoxToBigInteger16(tbVoterMu); //BigInteger delta = TextBoxToBigInteger16(tbVoterDelta); //BigInteger tau = TextBoxToBigInteger16(tbVoterTau); BigInteger csx = TextBoxToBigInteger16(tbCsX); BigInteger rs = csx.Mod(q); BigInteger es = H.Mod(q); BigInteger r = (tau.Multiply(delta).Multiply(rs.Add(mu.Multiply(es)))).Mod(q); BigInteger e = (es.Multiply(tau)).Mod(q); tbrs.Text = rs.ToString(16); tbr.Text = r.ToString(16); tbes.Text = es.ToString(16); tbe.Text = e.ToString(16); BigInteger k = TextBoxToBigInteger16(tbk); //BigInteger e = TextBoxToBigInteger16(tbe); BigInteger d = TextBoxToBigInteger16(tbValPrivate); //BigInteger r = TextBoxToBigInteger16(tbr); //BigInteger q = parameters.N; BigInteger s = (k.Multiply(e).Add(d.Multiply(r))).Mod(q); tbs.Text = s.ToString(16); BigInteger ss = (s.Multiply(delta.ModInverse(q)).Multiply(tau.ModInverse(q)).Mod(q).Add(epsilon.Multiply(es).Mod(q))).Mod(q); tbss.Text = ss.ToString(16); ECGost3410Signer signer = new ECGost3410Signer(); ECPublicKeyParameters pubKey = new ECPublicKeyParameters( "ECGOST3410", new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), // x new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))), // y parameters); //BigInteger H = TextBoxToBigInteger16(tbH); //BigInteger rs = TextBoxToBigInteger16(tbrs); //BigInteger ss = TextBoxToBigInteger16(tbss); //BigInteger q = parameters.N; //FpPoint G = (FpPoint)parameters.G; //FpPoint Q = new FpPoint(curve, new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicX)), new FpFieldElement(mod_p, TextBoxToBigInteger16(tbValPublicY))); //BigInteger e = H.Mod(q); byte[] ee = e.ToByteArray(); byte[] message = H.ToByteArray(); Array.Reverse(message); signer.Init(false, pubKey); MessageBox.Show(signer.VerifySignature(message, rs, ss).ToString(), "Проверка подписи"); //FpPoint C = (FpPoint)(G.Multiply(e.ModInverse(q).Multiply(ss).Mod(q)).Subtract(Q.Multiply(e.ModInverse(q).Multiply(rs).Mod(q)))); //BigInteger x = C.X.ToBigInteger(); }