开始使用BouncyCastle加密dll c#
发布时间:2020-12-14 01:58:43 所属栏目:Windows 来源:网络整理
导读:我是密码学的先驱 我想在c#中使用BouncyCastle .dll,但我找不到文档和示例. 特别是我需要使用pkcs#7(.p7m结果)对文件进行签名,并添加符合RFC 3161标准的可信服务器时间戳(.m7m结果). 有人可以建议我在哪里可以找到实例和文档吗? 提前感谢 最好的祝福 解决方
我是密码学的先驱
我想在c#中使用BouncyCastle .dll,但我找不到文档和示例. 特别是我需要使用pkcs#7(.p7m结果)对文件进行签名,并添加符合RFC 3161标准的可信服务器时间戳(.m7m结果). 有人可以建议我在哪里可以找到实例和文档吗? 提前感谢 最好的祝福 解决方法
我把这个小例子放在#SO上的另一个问题,但它也适用于你:
using System; using System.IO; using System.Linq; using System.Windows.Forms; using Org.BouncyCastle.Cms; using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.X509; namespace ConsoleApplicationSignWithBouncyCastle { class Program { [STAThread] static void Main(string[] args) { try { // First load a Certificate,filename/path and certificate password Cert = ReadCertFromFile("./test.pfx","test"); // Select a binary file var dialog = new OpenFileDialog { Filter = "All files (*.*)|*.*",InitialDirectory = "./",Title = "Select a text file" }; var filename = (dialog.ShowDialog() == DialogResult.OK) ? dialog.FileName : null; // Get the file var f = new FileStream(filename,System.IO.FileMode.Open); // Reading through this code stub to be sure I get it all :-) [ Different subject entirely ] var fileContent = ReadFully(f); // Create the generator var dataGenerator = new CmsEnvelopedDataStreamGenerator(); // Add receiver // Cert is the user's X.509 Certificate set bellow dataGenerator.AddKeyTransRecipient(Cert); // Make the output stream var outStream = new FileStream(filename + ".p7m",FileMode.Create); // Sign the stream var cryptoStream = dataGenerator.Open(outStream,CmsEnvelopedGenerator.Aes128Cbc); // Store in our binary stream writer and write the signed content var binWriter = new BinaryWriter(cryptoStream); binWriter.Write(fileContent); } catch (Exception ex) { Console.WriteLine("So,you wanna make an exception huh! : " + ex.ToString()); Console.ReadKey(); } } public static byte[] ReadFully(Stream stream) { stream.Seek(0,0); var buffer = new byte[32768]; using (var ms = new MemoryStream()) { while (true) { int read = stream.Read(buffer,buffer.Length); if (read <= 0) return ms.ToArray(); ms.Write(buffer,read); } } } public static Org.BouncyCastle.X509.X509Certificate Cert { get; set; } // This reads a certificate from a file. // Thanks to: http://blog.softwarecodehelp.com/2009/06/23/CodeForRetrievePublicKeyFromCertificateAndEncryptUsingCertificatePublicKeyForBothJavaC.aspx public static X509Certificate ReadCertFromFile(string strCertificatePath,string strCertificatePassword) { try { // Create file stream object to read certificate var keyStream = new FileStream(strCertificatePath,FileMode.Open,FileAccess.Read); // Read certificate using BouncyCastle component var inputKeyStore = new Pkcs12Store(); inputKeyStore.Load(keyStream,strCertificatePassword.ToCharArray()); //Close File stream keyStream.Close(); var keyAlias = inputKeyStore.Aliases.Cast<string>().FirstOrDefault(n => inputKeyStore.IsKeyEntry(n)); // Read Key from Alieases if (keyAlias == null) throw new NotImplementedException("Alias"); //Read certificate into 509 format return (X509Certificate)inputKeyStore.GetCertificate(keyAlias).Certificate; } catch (Exception ex) { Console.WriteLine("So,you wanna make an exception huh! : " + ex.ToString()); Console.ReadKey(); return null; } } } } 希望这可以帮助. 我也发布了它on my blog. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读