예제 #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);
        }
예제 #2
0
        public string SellarCadena(string cadena, string strRutaLlave, string strPswdLlave)
        {
            StringBuilder sbPrivateKey;

            sbPrivateKey = new StringBuilder();
            HashAlgorithm hashAlgo;

            byte[] abDigest;
            byte[] abBlock;
            System.Text.UTF8Encoding encod;
            encod = new System.Text.UTF8Encoding();
            int    nBlockLen;
            string strBase64;

            //cadena = Encoding.UTF8.GetString(System.Text.Encoding.Default.GetBytes(cadena));
            hashAlgo = HashAlgorithm.Sha1;

            abDigest = Hash.BytesFromBytes(encod.GetBytes(cadena), hashAlgo);

            if (abDigest.Length <= 0)
            {
                throw new Exception("Error de encripcion");
            }

            sbPrivateKey = CryptoSysPKI.Rsa.ReadEncPrivateKey(strRutaLlave, strPswdLlave);

            // 2.2 Encode the digest ready for signing with `Encoded Message for Signature' block using PKCS#1 v1.5 method
            nBlockLen = Rsa.KeyBytes(sbPrivateKey.ToString());
            abBlock   = Rsa.EncodeDigestForSignature(nBlockLen, abDigest, hashAlgo);

            if (abBlock.Length == 0)
            {
                throw new Exception("ERROR con Rsa.EncodeDigestForSignature");
            }


            // 2.3 Sign using the RSA private key
            abBlock = Rsa.RawPrivate(abBlock, sbPrivateKey.ToString());


            // 3. Convert to base64 and output result
            strBase64 = System.Convert.ToBase64String(abBlock);

            return(strBase64);
        }