Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Crypto in C#/4.0 using PKCS#10 and SHA256

Crypto in C#/4.0 using PKCS#10 and SHA256

Scheduled Pinned Locked Moved C#
csharpcryptographydotnetlinqsecurity
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ninodago
    wrote on last edited by
    #1

    I'm facing a new situation involving a program, written in .NET Framework 4.0/C#, which has to encode/decode and sign messages to be sent/received in a particular kind of WAN. This is the current scenario, which is working fine. We have a .p12 file (which contains the sender certificate) and a .cer file (which contains the receiver certificate), which are both installed in the pc. The scenario is working using SHA1 and PKCS#7 at 1024 bit, so the following piece of code works:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography.X509Certificates;
    using System.Security.Cryptography.Pkcs;
    using System.Security.Cryptography;
    using System.Windows;
    using System.Windows.Controls;
    using System.Collections;
    private static string senderCert = "sendername"; //the name registered in .p12 file
    private static string receiverCert = "receivername"; //the name registered in .cer file
    /// ///verify that the receiving message is signed
    ///and returns the data without sign
    ///
    /// dataflow with sign
    ///
    public static byte[] Verify(this byte[] signedData)
    {
    X509Certificate2 certPub = GetReceiverCert();
    if (certPub == null) return null;

        ContentInfo decodeContentInfo = new ContentInfo(signedData);
        SignedCms decodeCMS = new SignedCms(decodeContentInfo, false);
    
        try
        {
            //decode the message, if it isn't signed, raise an exception
            decodeCMS.Decode(signedData);
            SignerInfo signerInfo = decodeCMS.SignerInfos\[0\];
    
            X509Certificate2Collection certCollection = new 
                        X509Certificate2Collection(certPub);                
            return decodeCMS.ContentInfo.Content;
        }
        catch (CryptographicException err)
        {
            Logger.Log(err);
            return null;
        }
    }
    
    /// /// Returns the certificate used to sign the sending messages
    /// 
    /// 
    private static X509Certificate2 GetSenderCert()
    {
        //Open the personal certificates folder
        X509Store storeMy = new X509Store(StoreName.My, StoreLocation.CurrentUser);
        storeMy.Open(OpenFlags.ReadOnly);
    
        //find the proper certificate
        X509Certificate2Collection certColl = storeMy.Certificates.Find 
        (X509FindType.
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups