Ejemplo n.º 1
0
        public bool VerificarSelloComprobante(string cadenaOriginal, string sello, HashAlgorithm algoritmo)
        {
            byte[]        digest       = Hash.BytesFromBytes(Encoding.UTF8.GetBytes(cadenaOriginal), algoritmo);
            StringBuilder llavePublica = Rsa.GetPublicKeyFromCert(Convert.ToBase64String(Convert.FromBase64String(_Base64)));

            if (llavePublica.Length == 0)
            {
                return(false);
            }
            byte[] sellado = System.Convert.FromBase64String(sello);
            if (sellado.Length != Rsa.KeyBytes(llavePublica.ToString()))
            {
                return(false);
            }
            sellado = Rsa.RawPublic(sellado, llavePublica.ToString());
            if (sellado.Length == 0)
            {
                return(false);
            }
            sellado = Rsa.DecodeDigestForSignature(sellado);
            if (sellado.Length == 0)
            {
                return(false);
            }
            return(String.Compare(Cnv.ToHex(sellado), Cnv.ToHex(digest), true) == 0);
        }
Ejemplo n.º 2
0
        public static void V_Test_RSA_DecodeMsg()
        {
            Console.WriteLine("Testing RSA_DecodeMsg ...");
            byte[] abData    = null;
            byte[] abBlock   = null;
            byte[] abDigest  = null;
            byte[] abDigInfo = null;
            //'Dim nDataLen As Integer
            int nBlockLen = 0;

            // 0. Create an encoded test block ready for for signing
            abData = System.Text.Encoding.Default.GetBytes("abc");
            //'nDataLen = UBound(abData) - LBound(abData) + 1
            nBlockLen = 64;
            abBlock   = Rsa.EncodeMsgForSignature(nBlockLen, abData, HashAlgorithm.Sha1);
            Console.WriteLine("BLOCK   =" + Cnv.ToHex(abBlock));

            // 1. Extract the message digest =SHA1("abc")
            abDigest = Rsa.DecodeDigestForSignature(abBlock);
            if (abDigest.Length == 0)
            {
                Console.WriteLine("Decryption Error");
                return;
            }
            Console.WriteLine("Message digest is " + abDigest.Length + " bytes long");
            Console.WriteLine("HASH    =" + Cnv.ToHex(abDigest));

            // 2. Extract the full DigestInfo data
            abDigInfo = Rsa.DecodeDigestForSignature(abBlock, true);
            if (abDigInfo.Length == 0)
            {
                Console.WriteLine("Decryption Error");
                return;
            }
            Console.WriteLine("DigestInfo is " + abDigInfo.Length + " bytes long");
            Console.WriteLine("DIGINFO=" + Cnv.ToHex(abDigInfo));
        }