Click here to Skip to main content
15,886,110 members
Home / Discussions / C#
   

C#

 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:03
Member 112336118-Sep-18 0:03 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:06
Member 112336118-Sep-18 0:06 
GeneralRe: Forms opening other forms Pin
OriginalGriff8-Sep-18 0:07
mveOriginalGriff8-Sep-18 0:07 
GeneralRe: Forms opening other forms Pin
Matias Lopez11-Sep-18 8:50
Matias Lopez11-Sep-18 8:50 
QuestionC# project - SMS Alert Pin
Member 124675946-Sep-18 21:51
Member 124675946-Sep-18 21:51 
AnswerRe: C# project - SMS Alert Pin
OriginalGriff6-Sep-18 22:05
mveOriginalGriff6-Sep-18 22:05 
AnswerRe: C# project - SMS Alert Pin
DerekT-P13-Sep-18 1:01
professionalDerekT-P13-Sep-18 1:01 
QuestionCrypto in C#/4.0 using PKCS#10 and SHA256 Pin
ninodago6-Sep-18 5:28
ninodago6-Sep-18 5:28 
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:

C#
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
    /// <summary>
    ///verify that the receiving message is signed
    ///and returns the data without sign
    /// </summary>
    /// <param name="signedData">dataflow with sign</param>
    /// <returns></returns>
    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;
        }
    }

    /// <summary>
    /// Returns the certificate used to sign the sending messages
    /// </summary>
    /// <returns></returns>
    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.FindBySubjectName, senderCert, false);

        //Check if certificate exists in the collection, otherwise returns null             
            if (certColl.Count == 0)
            return null;
        storeMy.Close();
        return certColl[0];
    }

    /// <summary>
    /// Returns the certificate used to control the sign of the receiving messages
    /// </summary>
    /// <returns></returns>
    private static X509Certificate2 GetReceiverCert()
    {
        //Open personal certificates' folder
        X509Store storeMy = new X509Store(StoreName.My,
            StoreLocation.CurrentUser);
        storeMy.Open(OpenFlags.ReadOnly);

        //find the certificate use for this kind of messages
        X509Certificate2Collection certColl = storeMy.Certificates.Find 
                 (X509FindType.FindBySubjectName, receiverCert, false);

        //Check if certificate exists in the collection, otherwise returns null
        if (certColl.Count == 0)
            return null;
        storeMy.Close();
        return certColl[0];
    }

    /// <summary>
    /// Add a sign to the message
    /// </summary>
    /// <param name="data">message data flow</param>
    /// <returns></returns>
    public static byte[] Sign(this byte[] data)
    {
        X509Certificate2 certificate = GetSenderCert();
        if (certificate == null) return null;            
        if (data == null)
            throw new ArgumentNullException("data");
        if (certificate == null)
            throw new ArgumentNullException("certificate");

        //Set the message to sign
        ContentInfo content = new ContentInfo(data);
        signedCms = new SignedCms(content, false);
        CmsSigner signer = new CmsSigner(certificate);            
        signer.IncludeOption = X509IncludeOption.EndCertOnly; 

        //Create the sign
        signedCms.ComputeSignature(signer);
        return signedCms.Encode();
    }

As I said before, this code works fine under the given scenario (SHA1+PKCS#7 at 1024 bit). Now the client asks an update of the crypthography, in particular using SHA256 instead of SHA1 and PKCS#10 instead of PKCS#7 with key length 2048 bit instead of 1024.

According to your experience, what should I do to get the new requirements?

The only thing I'm sure is that I cannot use a NET Framework greater than this one because the devices are old XPs and they cannot be changed for company issues. So I cannot use new libraries released for .NET 4.7.1 to solve my problem.

I suppose that the new certificates will be released as the previous method, a file for sender, another one for receiver, to be installed in the machine.

Honestly I haven't a lot of experience with crypto and I would like to find an easy path which can follow the logic of the current methods, since the fact their scope wouldn't be changed, but just the signing approach.

I have no other limitations in using other libraries, pinvoke and other stuff, if it can really help.

Thanks to all of you for your support.
Any suggestion is appreciated. Regards
QuestionProblems with Rx EventMessager [Solved] Pin
Kenneth Haugland5-Sep-18 4:10
mvaKenneth Haugland5-Sep-18 4:10 
AnswerRe: Problems with Rx EventMessager Pin
Pete O'Hanlon5-Sep-18 4:49
mvePete O'Hanlon5-Sep-18 4:49 
AnswerRe: Problems with Rx EventMessager Pin
Richard Deeming5-Sep-18 4:54
mveRichard Deeming5-Sep-18 4:54 
GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:49
mvaKenneth Haugland5-Sep-18 9:49 
GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:56
mvaKenneth Haugland5-Sep-18 9:56 
QuestionUnable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 22:55
Member 139418904-Sep-18 22:55 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Richard MacCutchan4-Sep-18 23:10
mveRichard MacCutchan4-Sep-18 23:10 
GeneralRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 23:19
Member 139418904-Sep-18 23:19 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418905-Sep-18 0:24
Member 139418905-Sep-18 0:24 
QuestionConstructor Injection Pin
jones298474-Sep-18 7:33
jones298474-Sep-18 7:33 
AnswerRe: Constructor Injection Pin
Mc_Topaz4-Sep-18 10:15
Mc_Topaz4-Sep-18 10:15 
QuestionAforge Video-Picture quality Pin
User 136751144-Sep-18 1:08
User 136751144-Sep-18 1:08 
QuestionC# convert image format to dpx Pin
Member 135795963-Sep-18 23:28
Member 135795963-Sep-18 23:28 
AnswerRe: C# convert image format to dpx Pin
OriginalGriff4-Sep-18 1:10
mveOriginalGriff4-Sep-18 1:10 
SuggestionRe: C# convert image format to dpx Pin
Richard Deeming4-Sep-18 1:48
mveRichard Deeming4-Sep-18 1:48 
Question"Hiding" functions from StackTrace Pin
Bernhard Hiller3-Sep-18 2:37
Bernhard Hiller3-Sep-18 2:37 
AnswerRe: "Hiding" functions from StackTrace Pin
OriginalGriff3-Sep-18 4:12
mveOriginalGriff3-Sep-18 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.