开始使用BouncyCastle的加密DLL C#
问题描述:
开始我起动器加密开始使用BouncyCastle的加密DLL C#
竟被我喜欢用BouncyCastle的.dll文件的C#,但我不能找到文档和示例。
尤其需要使用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, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, 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。
小记:使用必须被添加到outStream,cryptoStream和binWriter。 p7m文件在没有任何数据的情况下创建,否则 – oleksa 2015-01-27 11:35:51
任何想法,我可能会发现一些文档或教程?我正在尝试实现分组密码模式,即。欧洲央行,加拿大广播公司,CTR是由充气城堡支持的;内置的.net不支持CTR。请告诉我是否有可用于充气城堡的文档。 – SutharMonil 2015-02-18 02:01:19