Exemple #1
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);
        }