Skip to content

Commit

Permalink
missing gost cms test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Fasjeit committed May 16, 2022
1 parent 2062069 commit e567c7e
Showing 1 changed file with 214 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;
using Xunit;

using Test.Cryptography;
using System.Security.Cryptography.Pkcs.Tests;

namespace System.Security.Cryptography.Pkcs.EnvelopedCmsTests.Tests
{
public static partial class GostEnvelopedTests
{
[Fact]
public static void EnvelopedCmsGost()
{
// Èñõîäíîå ñîîáùåíèå.
const String msg = "Ýòî ñîîáùåíèå, êîòîðîå áóäåò çàøèôðîâàíî.";

Console.WriteLine("{0}Èñõîäíîå ñîîáùåíèå (äëèíà {1}): {2} ",
Environment.NewLine, msg.Length, msg);

// Ïåðåâîäèì èñõîäíîå ñîîáùåíèå â ìàññèâ áàéòîâ.
UnicodeEncoding unicode = new UnicodeEncoding();
byte[] msgBytes = unicode.GetBytes(msg);

Console.WriteLine("{0}{0}------------------------------",
Environment.NewLine);
Console.WriteLine(" Ïîèñê ñåðòèôèêàòîâ ");
Console.WriteLine("------------------------------{0}",
Environment.NewLine);

// Ñåðòèôèêàòû ïîëó÷àòåëåé íåîáõîäèìû äëÿ
// çàøèôðîâàíèÿ ñîîáùåíèÿ.
var cert = GetGost2012_256Certificate();

//X509Certificate2Collection recipientCerts =
// new X509Certificate2Collection(cert);

Console.WriteLine("{0}{0}------------------------------",
Environment.NewLine);
Console.WriteLine(" Íà ñòîðîíå îòïðàâèòåëÿ");
Console.WriteLine("------------------------------{0}",
Environment.NewLine);

byte[] encodedEnvelopedCms = EncryptMsg(msgBytes,
cert, true);
File.WriteAllBytes("encrypted2.bin", encodedEnvelopedCms);

Console.WriteLine("{0}Ñîîáùåíèå ïîñëå çàøèôðîâàíèÿ (äëèíà {1}): ",
Environment.NewLine, encodedEnvelopedCms.Length);
foreach (byte b in encodedEnvelopedCms)
{
Console.Write("{0:x}", b);
}
Console.WriteLine();

Console.WriteLine("{0}{0}------------------------------",
Environment.NewLine);
Console.WriteLine(" Íà ñòîðîíå ïîëó÷àòåëÿ ");
Console.WriteLine("------------------------------{0}",
Environment.NewLine);

// Ðàñøèôðîâûâàåì ñîîáùåíèå äëÿ îäíîãî èç ïîëó÷àòåëåé
// è âîçâðàùàåì ñîîáùåíèå äëÿ îòîáðàæåíèÿ.
Byte[] decryptedMsg = DecryptMsg(encodedEnvelopedCms, cert);

// Ïðåîáðàçóåì ðàñøèôðîâàííûå áàéòû â ñîîáùåíèå
Console.WriteLine("{0}Ðàñøèôðîâàííîå ñîîáùåíèå: {1}",
Environment.NewLine, unicode.GetString(decryptedMsg));
}

// Çàøèôðîâûâàåì ñîîáùåíèå, èñïîëüçóÿ îòêðûòûé êëþ÷
// ïîëó÷àòåëÿ, ïðè ïîìîùè êëàññà EnvelopedCms.
static byte[] EncryptMsg(
Byte[] msg,
X509Certificate2 recipientCert,
bool useDataContextType)
{
// Ïîìåùàåì ñîîáùåíèå â îáúåêò ContentInfo
// Ýòî òðåáóåòñÿ äëÿ ñîçäàíèÿ îáúåêòà EnvelopedCms.

ContentInfo contentInfo;
if (useDataContextType)
{
contentInfo = new ContentInfo(
new Oid("1.2.840.113549.1.7.1"),
msg);
}
else
{
contentInfo = new ContentInfo(
ContentInfo.GetContentType(msg),
msg);
}
//contentInfo = new ContentInfo(msg);

// Ñîçäàåì îáúåêò EnvelopedCms, ïåðåäàâàÿ åìó
// òîëüêî ÷òî ñîçäàííûé îáúåêò ContentInfo.
// Èñïîëüçóåì èäåíòèôèêàöèþ ïîëó÷àòåëÿ (SubjectIdentifierType)
// ïî óìîë÷àíèþ (IssuerAndSerialNumber).
// Íå óñòàíàâëèâàåì àëãîðèòì çàøèôðîâàíèÿ òåëà ñîîáùåíèÿ:
// ContentEncryptionAlgorithm óñòàíàâëèâàåòñÿ â
// RSA_DES_EDE3_CBC, íåñìîòðÿ íà ýòî, ïðè çàøèôðîâàíèè
// ñîîáùåíèÿ â àäðåñ ïîëó÷àòåëÿ ñ ÃÎÑÒ ñåðòèôèêàòîì,
// áóäåò èñïîëüçîâàí àëãîðèòì GOST 28147-89.
//EnvelopedCms envelopedCms = new EnvelopedCms(contentInfo, new AlgorithmIdentifier(new Oid("1.2.840.113549.3.7")));
EnvelopedCms envelopedCms = new EnvelopedCms(contentInfo);

// Ñîçäàåì îáúåêò CmsRecipient, êîòîðûé
// èäåíòèôèöèðóåò ïîëó÷àòåëÿ çàøèôðîâàííîãî ñîîáùåíèÿ.
CmsRecipient recip1 = new CmsRecipient(
SubjectIdentifierType.IssuerAndSerialNumber,
recipientCert);

Console.Write(
"Çàøèôðîâûâàåì äàííûå äëÿ îäíîãî ïîëó÷àòåëÿ " +
"ñ èìåíåì {0} ...",
recip1.Certificate.SubjectName.Name);
// Çàøèôðîâûâàåì ñîîáùåíèå.
envelopedCms.Encrypt(recip1);
Console.WriteLine("Âûïîëíåíî.");

// Çàêîäèðîâàííîå EnvelopedCms ñîîáùåíèå ñîäåðæèò
// çàøèôðîâàííûé òåêñò ñîîáùåíèÿ è èíôîðìàöèþ
// î êàæäîì ïîëó÷àòåëå äàííîãî ñîîáùåíèÿ.
return envelopedCms.Encode();
}

// Ðàñøèôðîâàíèå çàêîäèðîâàííîãî EnvelopedCms ñîîáùåíèÿ.
static Byte[] DecryptMsg(byte[] encodedEnvelopedCms, X509Certificate2 cert)
{
// Ñîçäàåì îáúåêò äëÿ äåêîäèðîâàíèÿ è ðàñøèôðîâàíèÿ.
EnvelopedCms envelopedCms = new EnvelopedCms();

// Äåêîäèðóåì ñîîáùåíèå.
envelopedCms.Decode(encodedEnvelopedCms);

// Âûâîäèì êîëè÷åñòâî ïîëó÷àòåëåé ñîîáùåíèÿ
// (â äàííîì ïðèìåðå äîëæíî áûòü ðàâíî 1) è
// àëãîðèòì çàøèôðîâàíèÿ.
DisplayEnvelopedCms(envelopedCms, false);

// Ðàñøèôðîâûâàåì ñîîáùåíèå äëÿ åäèíñòâåííîãî
// ïîëó÷àòåëÿ.
Console.Write("Ðàñøèôðîâàíèå ... ");
envelopedCms.Decrypt(new X509Certificate2Collection(cert));
Console.WriteLine("Âûïîëíåíî.");

// Ïîñëå âûçîâà ìåòîäà Decrypt â ñâîéñòâå ContentInfo
// ñîäåðæèòñÿ ðàñøèôðîâàííîå ñîîáùåíèå.
return envelopedCms.ContentInfo.Content;
}

// Îòîáðàæàåì ñâîéñòâî ContentInfo îáúåêòà EnvelopedCms
static private void DisplayEnvelopedCmsContent(String desc,
EnvelopedCms envelopedCms)
{
Console.WriteLine(desc + " (äëèíà {0}): ",
envelopedCms.ContentInfo.Content.Length);
foreach (byte b in envelopedCms.ContentInfo.Content)
{
Console.Write(b.ToString() + " ");
}
Console.WriteLine();
}

// Îòîáðàæàåì íåêîòîðûå ñâîéñòâà îáúåêòà EnvelopedCms.
static private void DisplayEnvelopedCms(EnvelopedCms e,
Boolean displayContent)
{
Console.WriteLine("{0}Çàêîäèðîâàííîå CMS/PKCS #7 Ñîîáùåíèå.{0}" +
"Èíôîðìàöèÿ:", Environment.NewLine);
Console.WriteLine("\tÀëãîðèòì øèôðîâàíèÿ ñîîáùåíèÿ:{0}",
e.ContentEncryptionAlgorithm.Oid.FriendlyName);
Console.WriteLine(
"\tÊîëè÷åñòâî ïîëó÷àòåëåé çàêîäèðîâàííîãî CMS/PKCS #7 ñîîáùåíèÿ:{0}",
e.RecipientInfos.Count);
for (int i = 0; i < e.RecipientInfos.Count; i++)
{
Console.WriteLine(
"\tÏîëó÷àòåëü #{0} òèï {1}.",
i + 1,
e.RecipientInfos[i].RecipientIdentifier.Type);
}
if (displayContent)
{
DisplayEnvelopedCmsContent("Çàêîäèðîâàííîå CMS/PKCS " +
"#7 ñîäåðæèìîå", e);
}
Console.WriteLine();
}

private static X509Certificate2 GetGost2012_256Certificate()
{
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
return store.Certificates.Find(X509FindType.FindBySubjectName, "G2012256", false)[0];
}
}
}
}

1 comment on commit e567c7e

@Fasjeit
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#56

Please sign in to comment.