本文整理汇总了C#中System.Security.Cryptography.Pkcs.EnvelopedCms类的典型用法代码示例。如果您正苦于以下问题:C# EnvelopedCms类的具体用法?C# EnvelopedCms怎么用?C# EnvelopedCms使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
EnvelopedCms类属于System.Security.Cryptography.Pkcs命名空间,在下文中一共展示了EnvelopedCms类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Rc4AndCngWrappersDontMixTest
public static void Rc4AndCngWrappersDontMixTest()
{
//
// Combination of RC4 over a CAPI certificate.
//
// This works as long as the PKCS implementation opens the cert using CAPI. If he creates a CNG wrapper handle (by passing CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG),
// the test fails with a NOTSUPPORTED crypto exception inside Decrypt(). The same happens if the key is genuinely CNG.
//
byte[] content = { 6, 3, 128, 33, 44 };
AlgorithmIdentifier rc4 = new AlgorithmIdentifier(new Oid(Oids.Rc4));
EnvelopedCms ecms = new EnvelopedCms(new ContentInfo(content), rc4);
CmsRecipientCollection recipients = new CmsRecipientCollection(new CmsRecipient(Certificates.RSAKeyTransferCapi1.GetCertificate()));
ecms.Encrypt(recipients);
byte[] encodedMessage = ecms.Encode();
ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
using (X509Certificate2 cert = Certificates.RSAKeyTransferCapi1.TryGetCertificateWithPrivateKey())
{
if (cert == null)
return; // Sorry - CertLoader is not configured to load certs with private keys - we've tested as much as we can.
X509Certificate2Collection extraStore = new X509Certificate2Collection();
extraStore.Add(cert);
ecms.Decrypt(extraStore);
}
ContentInfo contentInfo = ecms.ContentInfo;
Assert.Equal<byte>(content, contentInfo.Content);
}
开发者ID:MichalStrehovsky,项目名称:corefx,代码行数:33,代码来源:EdgeCasesTests.cs
示例2: VerifyCertificates0
private static void VerifyCertificates0(byte[] encodedMessage)
{
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
X509Certificate2Collection certs = ecms.Certificates;
Assert.Equal(0, certs.Count);
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:7,代码来源:CertificateTests.cs
示例3: VerifyVersion0
private static void VerifyVersion0(byte[] encodedMessage)
{
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
int version = ecms.Version;
Assert.Equal(0, version);
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:7,代码来源:GeneralTests.cs
示例4: VerifyUnprotectedAttributes0
private static void VerifyUnprotectedAttributes0(byte[] encodedMessage)
{
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort();
Assert.Equal(0, attributes.Length);
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:7,代码来源:UnprotectedAttributeTests.cs
示例5: ImportEdgeCase
public static void ImportEdgeCase()
{
//
// Pfx's imported into a certificate collection propagate their "delete on Dispose" behavior to its cloned instances:
// a subtle difference from Pfx's created using the X509Certificate2 constructor that can lead to premature or
// double key deletion. Since EnvelopeCms.Decrypt() has no legitimate reason to clone the extraStore certs, this shouldn't
// be a problem, but this test will verify that it isn't.
//
byte[] encodedMessage =
("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
+ "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e"
+ "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c"
+ "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4"
+ "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d"
+ "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray();
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.LoadPfxUsingCollectionImport())
{
X509Certificate2Collection extraStore = new X509Certificate2Collection(cert);
ecms.Decrypt(extraStore);
byte[] expectedContent = { 1, 2, 3 };
ContentInfo contentInfo = ecms.ContentInfo;
Assert.Equal<byte>(expectedContent, contentInfo.Content);
}
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:30,代码来源:EdgeCasesTests.cs
示例6: VerifyUnprotectedAttributes1_DocumentDescription
private static void VerifyUnprotectedAttributes1_DocumentDescription(byte[] encodedMessage)
{
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
AsnEncodedData[] attributes = ecms.UnprotectedAttributes.FlattenAndSort();
Assert.Equal(1, attributes.Length);
attributes[0].AssertIsDocumentationDescription("My Description");
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:8,代码来源:UnprotectedAttributeTests.cs
示例7: EncryptedBytes
private byte[] EncryptedBytes(byte[] bytes)
{
var contentInfo = new ContentInfo(bytes);
var encryptAlgoOid = new Oid("2.16.840.1.101.3.4.1.42"); // AES-256-CBC
var envelopedCms = new EnvelopedCms(contentInfo, new AlgorithmIdentifier(encryptAlgoOid));
var recipient = new CmsRecipient(CryptographicCertificate);
envelopedCms.Encrypt(recipient);
return envelopedCms.Encode();
}
开发者ID:difi,项目名称:sikker-digital-post-klient-dotnet,代码行数:9,代码来源:AsiceArchive.cs
示例8: VerifyAlgorithmRc2_128
private static void VerifyAlgorithmRc2_128(byte[] encodedMessage)
{
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
AlgorithmIdentifier algorithm = ecms.ContentEncryptionAlgorithm;
Assert.NotNull(algorithm.Oid);
Assert.Equal(Oids.Rc2, algorithm.Oid.Value);
Assert.Equal(128, algorithm.KeyLength);
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:9,代码来源:ContentEncryptionAlgorithmTests.cs
示例9: Encrypt
/// <summary>
/// Encrypts the specified string.
/// </summary>
/// <param name="plaintext">The plaintext to be encrypted.</param>
/// <param name="certificate">The certificate to be used for encryption.</param>
/// <returns>The encrypted text.</returns>
public static string Encrypt(this string plaintext, X509Certificate2 certificate)
{
var contentInfo = new ContentInfo(Encoding.UTF8.GetBytes(plaintext));
var envelopedCms = new EnvelopedCms(contentInfo);
var cmsRecipient = new CmsRecipient(certificate);
envelopedCms.Encrypt(cmsRecipient);
return Convert.ToBase64String(envelopedCms.Encode());
}
开发者ID:JayBazuzi,项目名称:Its.Configuration,代码行数:16,代码来源:CryptographyExtensions.cs
示例10: DecryptEnvelop
public static string DecryptEnvelop(string base64EncryptedString)
{
var encryptedBytes = Convert.FromBase64String(base64EncryptedString);
var envelope = new EnvelopedCms();
envelope.Decode(encryptedBytes);
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
envelope.Decrypt(store.Certificates);
return Encoding.UTF8.GetString(envelope.ContentInfo.Content);
}
开发者ID:s-innovations,项目名称:S-Innovations.Azure.Demos,代码行数:10,代码来源:UnitTest2.cs
示例11: DecodeCertificates0_RoundTrip
public static void DecodeCertificates0_RoundTrip()
{
ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
EnvelopedCms ecms = new EnvelopedCms(contentInfo);
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
{
CmsRecipient cmsRecipient = new CmsRecipient(cert);
ecms.Encrypt(cmsRecipient);
}
byte[] encodedMessage = ecms.Encode();
VerifyCertificates0(encodedMessage);
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:13,代码来源:CertificateTests.cs
示例12: DecodeAlgorithmDes_RoundTrip
public static void DecodeAlgorithmDes_RoundTrip()
{
AlgorithmIdentifier algorithm = new AlgorithmIdentifier(new Oid(Oids.Des));
ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
EnvelopedCms ecms = new EnvelopedCms(contentInfo, algorithm);
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
{
CmsRecipient cmsRecipient = new CmsRecipient(cert);
ecms.Encrypt(cmsRecipient);
}
byte[] encodedMessage = ecms.Encode();
VerifyAlgorithmDes(encodedMessage);
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:13,代码来源:ContentEncryptionAlgorithmTests.cs
示例13: DecodeRecipients3_RoundTrip
public static void DecodeRecipients3_RoundTrip()
{
ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
EnvelopedCms ecms = new EnvelopedCms(contentInfo);
CmsRecipientCollection recipients = new CmsRecipientCollection();
foreach (X509Certificate2 cert in s_certs)
{
recipients.Add(new CmsRecipient(cert));
}
ecms.Encrypt(recipients);
byte[] encodedMessage = ecms.Encode();
VerifyRecipients3(encodedMessage);
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:14,代码来源:GeneralTests.cs
示例14: DefaultProperties
private void DefaultProperties (EnvelopedCms ep, int contentLength, int version)
{
Assert.AreEqual (0, ep.Certificates.Count, "Certificates");
Assert.AreEqual (0, ep.ContentEncryptionAlgorithm.KeyLength, "ContentEncryptionAlgorithm.KeyLength");
Assert.AreEqual (tdesName, ep.ContentEncryptionAlgorithm.Oid.FriendlyName, "ContentEncryptionAlgorithm.Oid.FriendlyName");
Assert.AreEqual (tdesOid, ep.ContentEncryptionAlgorithm.Oid.Value, "ContentEncryptionAlgorithm.Oid.Value");
Assert.AreEqual (0, ep.ContentEncryptionAlgorithm.Parameters.Length, "ContentEncryptionAlgorithm.Parameters");
Assert.AreEqual (p7DataName, ep.ContentInfo.ContentType.FriendlyName, "ContentInfo.ContentType.FriendlyName");
Assert.AreEqual (p7DataOid, ep.ContentInfo.ContentType.Value, "ContentInfo.ContentType.Value");
Assert.AreEqual (contentLength, ep.ContentInfo.Content.Length, "ContentInfo.Content");
Assert.AreEqual (0, ep.RecipientInfos.Count, "RecipientInfos");
Assert.AreEqual (0, ep.UnprotectedAttributes.Count, "UnprotectedAttributes");
Assert.AreEqual (version, ep.Version, "Version");
}
开发者ID:Profit0004,项目名称:mono,代码行数:14,代码来源:EnvelopedCmsTest.cs
示例15: Decrypt
public static string Decrypt(string encryptedString)
{
// パスワードを復号
var store = new X509Store(StoreLocation.LocalMachine); // (StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var encPasswordBase64 = Convert.FromBase64String(encryptedString);
var enveloped = new EnvelopedCms();
enveloped.Decode(encPasswordBase64);
enveloped.Decrypt(store.Certificates);
return Encoding.UTF8.GetString(enveloped.ContentInfo.Content);
}
开发者ID:sleepytaka,项目名称:PS30,代码行数:14,代码来源:Program.cs
示例16: GetEnvelopedMime
/// <summary>
/// Decrypts enveloped mime content.
/// </summary>
/// <param name="cert">Decrypting certificate.</param>
/// <returns>Returns decrypted enveloped mime content.</returns>
/// <exception cref="ArgumentNullException">Is raised when <b>cert</b> is null reference.</exception>
/// <exception cref="InvalidOperationException">Is raised when <b>smime-type != enveloped-data</b>.</exception>
public MIME_Message GetEnvelopedMime(X509Certificate2 cert)
{
if(cert == null){
throw new ArgumentNullException("cert");
}
if(!string.Equals(this.Entity.ContentType.Parameters["smime-type"],"enveloped-data",StringComparison.InvariantCultureIgnoreCase)){
throw new InvalidOperationException("The VerifySignature method is only valid if Content-Type parameter smime-type=enveloped-data.");
}
EnvelopedCms envelopedCms = new EnvelopedCms();
envelopedCms.Decode(this.Data);
X509Certificate2Collection certificates = new X509Certificate2Collection(cert);
envelopedCms.Decrypt(certificates);
return MIME_Message.ParseFromStream(new MemoryStream(envelopedCms.Encode()));
}
开发者ID:ChuckLafferty,项目名称:bugnet,代码行数:24,代码来源:MIME_b_ApplicationPkcs7Mime.cs
示例17: Decrypt
/// <summary>
/// Decrypts the specified string.
/// </summary>
/// <param name="ciphertext">The ciphertext to be decrypted.</param>
/// <param name="certificates">A set of certificates containing the one that was used to encrypt the ciphertext.</param>
/// <returns>The decrypted text.</returns>
public static string Decrypt(this string ciphertext, params X509Certificate2[] certificates)
{
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certCollection = store.Certificates;
if (certificates != null && certificates.Length > 0)
{
certCollection.AddRange(certificates);
}
var envelopedCms = new EnvelopedCms();
envelopedCms.Decode(Convert.FromBase64String(ciphertext));
envelopedCms.Decrypt(certCollection);
return Encoding.UTF8.GetString(envelopedCms.ContentInfo.Content);
}
开发者ID:JayBazuzi,项目名称:Its.Configuration,代码行数:24,代码来源:CryptographyExtensions.cs
示例18: ZeroLengthContent_RoundTrip
public static void ZeroLengthContent_RoundTrip()
{
ContentInfo contentInfo = new ContentInfo(Array.Empty<byte>());
EnvelopedCms ecms = new EnvelopedCms(contentInfo);
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
{
CmsRecipient cmsRecipient = new CmsRecipient(cert);
try
{
ecms.Encrypt(cmsRecipient);
}
catch (CryptographicException e)
{
throw new Exception("ecms.Encrypt() threw " + e.Message + ".\nIf you're running on the desktop CLR, this is actually an expected result.");
}
}
byte[] encodedMessage = ecms.Encode();
ValidateZeroLengthContent(encodedMessage);
}
开发者ID:MichalStrehovsky,项目名称:corefx,代码行数:19,代码来源:EdgeCasesTests.cs
示例19: ImportEdgeCaseSki
public static void ImportEdgeCaseSki()
{
byte[] encodedMessage =
("3081f206092a864886f70d010703a081e43081e10201023181ae3081ab0201028014f2008aa9fa3742e8370cb1674ce1d158"
+ "2921dcc3300d06092a864886f70d01010105000481804336e978bc72ba2f5264cd854867fac438f36f2b3df6004528f2df83"
+ "4fb2113d6f7c07667e7296b029756222d6ced396a8fffed32be838eec7f2e54b9467fa80f85d097f7d1f0fbde57e07ab3d46"
+ "a60b31f37ef9844dcab2a8eef4fec5579fac5ec1e7ee82409898e17d30c3ac1a407fca15d23c9df2904a707294d78d4300ba"
+ "302b06092a864886f70d010701301406082a864886f70d03070408355c596e3e8540608008f1f811e862e51bbd").HexToByteArray();
EnvelopedCms ecms = new EnvelopedCms();
ecms.Decode(encodedMessage);
using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.LoadPfxUsingCollectionImport())
{
X509Certificate2Collection extraStore = new X509Certificate2Collection(cert);
ecms.Decrypt(extraStore);
byte[] expectedContent = { 1, 2, 3 };
ContentInfo contentInfo = ecms.ContentInfo;
Assert.Equal<byte>(new byte[] { 1, 2, 3 }, contentInfo.Content);
Assert.Equal<byte>(expectedContent, contentInfo.Content);
}
}
开发者ID:ESgarbi,项目名称:corefx,代码行数:23,代码来源:EdgeCasesTests.cs
示例20: SmimeEnvelopeAndEncryptFor
/// <summary>
/// Encrypts the message and envelopes it for multiple recipients.
/// </summary>
/// <param name="recipients">An object containing the recipients' certificates with public keys.</param>
/// <example>
/// <code>
/// [C#]
///
/// CmsRecipientCollection recipients = new CmsRecipientCollection();
///
/// recipients.Add(new CmsRecipient(new X509Certificate2("C:\\recipient1.cer")));
/// recipients.Add(new CmsRecipient(new X509Certificate2("C:\\recipient2.cer")));
///
/// message.SmimeEnvelopeAndEncryptFor(recipients);
/// </code>
/// </example>
public void SmimeEnvelopeAndEncryptFor(CmsRecipientCollection recipients)
{
string mimeString = this.ToMimeString();
byte[] toencrypt = Encoding.ASCII.GetBytes(mimeString);
EnvelopedCms cms = new EnvelopedCms(new ContentInfo(toencrypt));
cms.Encrypt(recipients);
MimePart envelope = new MimePart();
envelope.ContentType.MimeType = "application/pkcs7-mime";
envelope.ContentType.Parameters.Add("smime-type", "encrypted-data");
envelope.ContentType.Parameters.Add("name", "smime.p7m");
envelope.ContentDisposition.Disposition = "attachment";
envelope.ContentDisposition.FileName = "smime.p7m";
envelope.ContentTransferEncoding = ContentTransferEncoding.Base64;
envelope.BinaryContent = cms.Encode();
this.PartTreeRoot = envelope;
this.ContentType = this.PartTreeRoot.ContentType;
this.ContentDisposition = this.PartTreeRoot.ContentDisposition;
this.ContentTransferEncoding = this.PartTreeRoot.ContentTransferEncoding;
}
开发者ID:JBTech,项目名称:MailSystem.NET,代码行数:40,代码来源:Message.cs
注:本文中的System.Security.Cryptography.Pkcs.EnvelopedCms类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论