Exemplo n.º 1
0
        static void Main(string[] args)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            RSAParameters            key = rsa.ExportParameters(true);
            RSAKey pr = new RSAKey(key.Modulus, key.D);
            RSAKey pu = new RSAKey(key.Modulus, key.Exponent);

            string text = "im retarded";
            string sign = Crypto.SignData(text, pr);

            Console.WriteLine(Crypto.VerifyData(sign, text, pu));

            Console.ReadKey();
        }
Exemplo n.º 2
0
        public static string SignData(string data, RSAKey key)
        {
            BigInteger d = key.exponent;
            BigInteger n = key.modulus;
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            Encoding asc = Encoding.ASCII;

            byte[]     textBytes = asc.GetBytes(data);
            byte[]     hash      = sha.ComputeHash(textBytes);
            BigInteger m         = new BigInteger(Correct(hash.Reverse().ToArray()));
            BigInteger s         = BigInteger.ModPow(m, d, n);

            byte[] signed = s.ToByteArray().Reverse().ToArray();
            return(ToStrHex(signed, false));
        }
Exemplo n.º 3
0
        public static bool VerifyData(string verData, string sigData, RSAKey key)
        {
            BigInteger e = key.exponent;
            BigInteger n = key.modulus;
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            Encoding asc = Encoding.ASCII;

            byte[]     textBytes = asc.GetBytes(sigData);
            byte[]     hash      = sha.ComputeHash(textBytes);
            byte[]     signed    = HexToByteArray(verData);
            BigInteger s         = new BigInteger(Correct(signed.Reverse().ToArray()));
            BigInteger m         = BigInteger.ModPow(s, e, n);

            return(m == new BigInteger(Correct(hash.Reverse().ToArray())));
        }