private void btnFirmaMasiva_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult dresult = fbd.ShowDialog(); if (dresult == DialogResult.OK) { foreach (string file in Directory.GetFiles(fbd.SelectedPath)) { FileInfo fInfo = new FileInfo(file); AsymmetricKeyParameter akp = ReadPrivateKey(lblFirma.Text); Sha1Digest dig = new Sha1Digest(); byte[] msgBytes = File.ReadAllBytes(file); dig.BlockUpdate(msgBytes, 0, msgBytes.Length); byte[] result = new byte[dig.GetDigestSize()]; dig.DoFinal(result, 0); Org.BouncyCastle.Crypto.Signers.RsaDigestSigner signer = new Org.BouncyCastle.Crypto.Signers.RsaDigestSigner(dig); signer.Init(true, akp); signer.BlockUpdate(result, 0, result.Length); byte[] sig = signer.GenerateSignature(); string signString = Convert.ToBase64String(sig); string signPath = string.Format("{0}{1}", fInfo.FullName, "sgn"); File.WriteAllText(signPath, signString, System.Text.Encoding.UTF8); } MessageBox.Show("Firma finalizada."); } }
private void btnFirmarDocumento_Click(object sender, EventArgs e) { AsymmetricKeyParameter akp = ReadPrivateKey(lblFirma.Text); Sha1Digest dig = new Sha1Digest(); byte[] msgBytes = File.ReadAllBytes(lblDocument.Text); dig.BlockUpdate(msgBytes, 0, msgBytes.Length); byte[] result = new byte[dig.GetDigestSize()]; dig.DoFinal(result, 0); Org.BouncyCastle.Crypto.Signers.RsaDigestSigner signer = new Org.BouncyCastle.Crypto.Signers.RsaDigestSigner(dig); signer.Init(true, akp); signer.BlockUpdate(result, 0, result.Length); byte[] sig = signer.GenerateSignature(); textBox2.Text = Convert.ToBase64String(sig); MessageBox.Show("Se ha firmado el documento."); }
public override byte[] SignHash(byte[] hash, HashAlgorithmName hashAlgorithm, RSASignaturePadding padding) { // https://github.com/neoeinstein/bouncycastle/blob/master/crypto/src/security/SignerUtilities.cs var dsi = new Org.BouncyCastle.Crypto.Signers.RsaDigestSigner( new Org.BouncyCastle.Crypto.Digests.Sha256Digest() ) ; dsi.Init(true, this.m_keyPair.Private); dsi.BlockUpdate(hash, 0, hash.Length); return(dsi.GenerateSignature()); System.Console.WriteLine(hashAlgorithm); return(null); }
private void btnVerificarDocumento_Click(object sender, EventArgs e) { byte[] sign = Convert.FromBase64String(textBox2.Text); AsymmetricKeyParameter akp = ReadPublicKey(lblFirmaPublica.Text); Sha1Digest dig = new Sha1Digest(); byte[] msgBytes = File.ReadAllBytes(lblDocument.Text); dig.BlockUpdate(msgBytes, 0, msgBytes.Length); byte[] result = new byte[dig.GetDigestSize()]; dig.DoFinal(result, 0); Org.BouncyCastle.Crypto.Signers.RsaDigestSigner signer = new Org.BouncyCastle.Crypto.Signers.RsaDigestSigner(dig); signer.Init(false, akp); signer.BlockUpdate(result, 0, result.Length); if (!signer.VerifySignature(sign)) { MessageBox.Show("RSA Digest Signer failed.", "AtenciĆ³n", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("RSA Digest Signer success.", "AtenciĆ³n", MessageBoxButtons.OK, MessageBoxIcon.Information); } }