2010-10-15 6 views
5

Je suis entrée avec la cryptographieCommencer avec l'dll Crypto BouncyCastle C#

Je woul aime utiliser BouncyCastle .dll pour C#, mais je ne peux pas trouver la documentation et des exemples.

En particulier je dois utiliser pour signer des fichiers avec pkcs # 7 (résultats .p7m) et leur ajouter RFC 3161-conforme, horodatages du serveur de confiance (résultats .m7m).

Quelqu'un peut suggérer où je peux trouver des exemples et de la documentation pour le faire?

Remerciant à l'avance

Meilleures salutations

Répondre

6

Je mis en place ce petit exemple pour une autre question ici sur #SO, mais il applique à vous aussi:

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; 
     } 
    } 
} } 

Hope this helps.

Je l'ai également posté on my blog.

+0

Petite note: l'utilisation doit être ajoutée à outStream, cryptoStream et binWriter. Le fichier p7m est créé sans aucune donnée à l'intérieur – oleksa

+0

Toute idée où je pourrais trouver de la documentation ou un tutoriel? J'essaye de mettre en application des modes de chiffrage de bloc viz. BCE, CBC, CTR qui sont soutenus par le château gonflable; le .net dans construit ne supporte pas CTR. S'il vous plaît dites-moi s'il y a un doc disponible pour le château gonflable. – SutharMonil

Questions connexes