Beispiel #1
0
        public static Signature Sign(SecretKey secretKey, string message)
        {
            var numberK = Utils.GenerateNumber().Mod(Static.Q - 2) + 2; // Crete 1 < k < q

            var matrixR =
                secretKey.MatrixD.Pow(secretKey.NumberW).Inverse() *
                secretKey.MatrixG.Pow(numberK) *
                secretKey.MatrixU.Pow(secretKey.NumberT);

            byte[] hash;

            using (SHA512 shaM = new SHA512Managed())
            {
                var bytes = Utils.ConcatBytes(Encoding.Unicode.GetBytes(message), matrixR.ToBytes());
                hash = shaM.ComputeHash(bytes);
            }

            var numberE      = new BigInteger(hash);
            var numberEModed = numberE.Mod();

            Static.NumberE = numberEModed;
            var numberS = (numberK + numberEModed * secretKey.NumberX).Mod(Static.Q);

            return(new Signature {
                NumberE = numberEModed, NumberS = numberS
            });
        }
Beispiel #2
0
        public static bool Verify(PublicKey publicKey, Signature signature, string message)
        {
            var matrixR =
                publicKey.MatrixY.Pow(
                    (publicKey.PrimeNumber
                     - signature.NumberE).Mod(Static.Q)) *
                publicKey.MatrixL *
                publicKey.MatrixZ.Pow(signature.NumberS);

            var matrixRModed = new Matrix(matrixR[1].Mod(), matrixR[2].Mod(), matrixR[3].Mod(), matrixR[4].Mod());

            byte[] hash;

            using (SHA512 shaM = new SHA512Managed())
            {
                var bytes = Utils.ConcatBytes(Encoding.Unicode.GetBytes(message), matrixRModed.ToBytes());
                hash = shaM.ComputeHash(bytes);
            }

            var numberE      = new BigInteger(hash);
            var numberEModed = numberE.Mod();

            return(numberEModed.Equals(signature.NumberE));
        }