示例#1
0
        public static string GetSignature(string unsignedMessage, string p, string g, string x)
        {
            // Trim message
            unsignedMessage = unsignedMessage.Trim();

            var        sha       = new SHA256();
            BigInteger hash      = sha.GetMessageDigestToBigInteger(unsignedMessage);
            string     signature = SiGamalGenerator.signature(BigInteger.Parse(p), BigInteger.Parse(g), BigInteger.Parse(x), hash);

            return(signature);
        }
示例#2
0
        /// <summary>
        /// Verify the authenticity and integrity of a string which contains a message
        /// along with its signature.
        /// </summary>
        /// <param name="signedMessage">A string containing a message and its signature</param>
        /// <returns>True if the signature matches, false if it doesn't.</returns>
        public static bool VerifySignedMessage(string signedMessage, string p, string g, string y)
        {
            string body = signedMessage.Substring(0, signedMessage.IndexOf("\n\n<sign>")).Trim();

            string rs = signedMessage.Substring(signedMessage.IndexOf("<sign>"));

            rs = rs.Substring(6);
            rs = rs.Substring(0, rs.IndexOf("<sign>"));

            BigInteger r = BigInteger.Parse("0" + rs.Substring(0, rs.IndexOf('-')), System.Globalization.NumberStyles.HexNumber);
            BigInteger s = BigInteger.Parse("0" + rs.Substring(rs.IndexOf('-') + 1), System.Globalization.NumberStyles.HexNumber);


            System.Diagnostics.Debug.WriteLine(r);

            System.Diagnostics.Debug.WriteLine(s);

            SHA256 sha = new SHA256();

            bool isValid = SiGamalGenerator.verification(r, s, BigInteger.Parse(g), sha.GetMessageDigestToBigInteger(body), BigInteger.Parse(y), BigInteger.Parse(p));

            return(isValid);
        }
示例#3
0
        private void SignEmail(Key.PrivateKey key)
        {
            Outlook.Application application = Globals.ThisAddIn.Application;
            Outlook.Inspector inspector = application.ActiveInspector();
            Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;
            //Outlook.MailItem item = Outlook. Inspector.CurrentItem as Outlook.MailItem;
            if (item != null)
            {
                string body = item.Body;
                item.Body = body;
                // Put Algorithm Sign Here
                SHA256 sha = new SHA256();
                BigInteger hash = sha.GetMessageDigestToBigInteger(body);
                //System.Windows.Forms.MessageBox.Show("Hash=" + sha.GetMessageDigestToBigInteger(item.Body).ToString());
                item.Body += "<sign>" + SiGamalGenerator.signature(key.P, key.G, key.X, hash) + "<sign>";

            }
        }
示例#4
0
        private void VerifyEmail(Key.PublicKey pubKey)
        {
            Outlook.Application application = Globals.ThisAddIn.Application;
            Outlook.Inspector inspector = application.ActiveInspector();
            Outlook.MailItem item = (Outlook.MailItem)inspector.CurrentItem;
            //Outlook.MailItem item = Outlook. Inspector.CurrentItem as Outlook.MailItem;
            if (item != null)
            {
                // Put Algorithm Verify Here
                string rs = item.Body.Substring(item.Body.IndexOf("<sign>"));
                rs = rs.Substring(6);
                rs = rs.Substring(0, rs.IndexOf("<sign>"));
                /*System.Windows.Forms.MessageBox.Show(rs);
                System.Windows.Forms.MessageBox.Show(rs.Substring(0, rs.IndexOf('-')));
                System.Windows.Forms.MessageBox.Show(item.Body.Substring(0, item.Body.IndexOf("\n<sign>") - 1));
                */
                BigInteger r = BigInteger.Parse("0" + rs.Substring(0, rs.IndexOf('-')), System.Globalization.NumberStyles.HexNumber);
                BigInteger s = BigInteger.Parse("0" + rs.Substring(rs.IndexOf('-') + 1), System.Globalization.NumberStyles.HexNumber);
                SHA256 sha = new SHA256();
                if (SiGamalGenerator.verification(r, s, pubKey.G, sha.GetMessageDigestToBigInteger(item.Body.Substring(0, item.Body.IndexOf("\n<sign>") - 2)), pubKey.Y, pubKey.P))
                {
                    System.Windows.Forms.MessageBox.Show("TRUE : Message is Valid");
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("FALSE ; Message was edited");
                }

            }
        }