本文整理汇总了C#中System.Security.Cryptography.Pkcs.SignedCms类的典型用法代码示例。如果您正苦于以下问题:C# SignedCms类的具体用法?C# SignedCms怎么用?C# SignedCms使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SignedCms类属于System.Security.Cryptography.Pkcs命名空间,在下文中一共展示了SignedCms类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: CheckSig
protected string CheckSig()
{
var formData = Request.Form;
var text = formData["txtSign"];
var sig = formData["txtSig"];
string output = "INVALID!";
if (!string.IsNullOrEmpty(sig))
{
try
{
ContentInfo contentInfo = new ContentInfo(Encoding.UTF8.GetBytes(text));
SignedCms signedCms = new SignedCms(contentInfo, true);
signedCms.Decode(Convert.FromBase64String(sig));
// This checks if the signature is valid, but doensn't actually verify the cert (TODO)
signedCms.CheckSignature(true);
output = "Signature valid.";
signedCms.CheckSignature(false);
output += "<br>Cert valid";
}
catch (Exception e)
{
output += "<br>" + e.ToString();
}
}
return output;
}
开发者ID:CACBridge,项目名称:ChromeCAC,代码行数:35,代码来源:Verify.aspx.cs
示例2: FirmarMensaje
/// <summary>
/// Firma el mensaje PKCS #7 con el certificado del firmante
/// </summary>
/// <param name="pMensaje">Mensaje (como cadena de bytes)</param>
/// <param name="pCertificadoFirmante">Certificado usado para firmar</param>
/// <returns>Mensaje Firmado (como cadena de bytes)</returns>
/// <remarks></remarks>
public static byte[] FirmarMensaje(byte[] pMensaje, X509Certificate2 pCertificadoFirmante)
{
byte[] msjFirmado;
try
{
// Se pone el Mensaje recibido en un objeto ContentInfo
ContentInfo infoContenidoMsj = new ContentInfo(pMensaje);
// Se instancia el CMS Firmado con el ContentInfo
SignedCms cmsFirmado = new SignedCms(infoContenidoMsj);
// Se instancia el objeto CmsSigner con las caracteristicas del firmante
CmsSigner cmsFirmante = new CmsSigner(pCertificadoFirmante);
cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly;
// Se firma el mensaje PKCS #7 con el certificado
cmsFirmado.ComputeSignature(cmsFirmante);
msjFirmado = cmsFirmado.Encode();
// Retorno el mensaje PKCS #7 firmado .
return msjFirmado;
}
catch (Exception excepcionAlFirmar)
{
throw new Exception("ERROR: Procedimiento: FirmarMensaje. Al intentar firmar el mensaje con el certificado del firmante: " + excepcionAlFirmar.Message);
}
}
开发者ID:fedepazw,项目名称:OpenReceArg,代码行数:35,代码来源:CertificadosX509.cs
示例3: ValidateToken
public bool ValidateToken(byte[] token, byte[] nonce, byte[] certificate, byte[] signature)
{
SignedCms cms = new SignedCms();
cms.Decode(certificate);
var certificates = cms.Certificates.Cast<X509Certificate2>().ToArray();
var leaf = certificates.Single(cert => cert.Extensions.Cast<X509Extension>().Any(usage =>
{
var eku = usage as X509EnhancedKeyUsageExtension;
if (eku != null)
{
return eku.EnhancedKeyUsages.Cast<Oid>().Any(oid => oid.Value == "1.3.6.1.4.1.311.10.5.40");
}
return false;
}));
var signedData = nonce.Concat(token).ToArray();
var publicKeyProvider = leaf.PublicKey.Key as System.Security.Cryptography.RSACryptoServiceProvider;
return publicKeyProvider.VerifyData(signedData, CryptoConfig.MapNameToOID("SHA1"), signature);
// not working either (same results)
//
//SHA1Managed hash = new SHA1Managed();
//byte[] hashedData;
//hashedData = hash.ComputeHash(signedData);
//if (!publicKeyProvider.VerifyHash(hashedData, CryptoConfig.MapNameToOID("SHA1"), signature))
// throw new Exception("Invalid or Corrupted HardwareToken");
}
开发者ID:sandorfr,项目名称:sandor,代码行数:32,代码来源:ValidationService.svc.cs
示例4: T1_ValidSignature
public void T1_ValidSignature ()
{
byte[] data = GetData ("SignedValidSignaturesTest1.eml");
SignedCms cms = new SignedCms ();
cms.Decode (data);
Assert.IsTrue (CheckHash (cms), "CheckHash");
Assert.IsTrue (CheckSignature (cms), "CheckSignature");
X509Certificate2 ee = GetCertificate ("ValidCertificatePathTest1EE.crt");
// certificates aren't in any particuliar order
Assert.IsTrue (cms.Certificates.Contains (ee), "EE");
Assert.IsTrue (cms.Certificates.Contains (GoodCACert), "GoodCACert");
Assert.IsFalse (cms.Detached, "Detached");
Assert.AreEqual (1, cms.Version, "Version");
Assert.AreEqual ("1.2.840.113549.1.7.1", cms.ContentInfo.ContentType.Value, "ContentInfo.Oid");
Assert.AreEqual ("43-6F-6E-74-65-6E-74-2D-54-79-70-65-3A-20-74-65-78-74-2F-70-6C-61-69-6E-3B-20-63-68-61-72-73-65-74-3D-69-73-6F-2D-38-38-35-39-2D-31-0D-0A-43-6F-6E-74-65-6E-74-2D-54-72-61-6E-73-66-65-72-2D-45-6E-63-6F-64-69-6E-67-3A-20-37-62-69-74-0D-0A-0D-0A-54-68-69-73-20-69-73-20-61-20-73-61-6D-70-6C-65-20-73-69-67-6E-65-64-20-6D-65-73-73-61-67-65-2E", BitConverter.ToString (cms.ContentInfo.Content), "ContentInfo.Content");
Assert.AreEqual (1, cms.SignerInfos.Count, "SignerInfos.Count");
Assert.AreEqual (ee, cms.SignerInfos[0].Certificate, "SignerInfos[0].Certificate");
Assert.AreEqual (0, cms.SignerInfos[0].CounterSignerInfos.Count, "SignerInfos[0].CounterSignerInfos.Count");
Assert.AreEqual ("1.3.14.3.2.26", cms.SignerInfos[0].DigestAlgorithm.Value, "cms.SignerInfos[0].DigestAlgorithm");
Assert.AreEqual (0, cms.SignerInfos[0].SignedAttributes.Count, "SignerInfos[0].SignedAttributes.Count");
Assert.AreEqual (SubjectIdentifierType.IssuerAndSerialNumber, cms.SignerInfos[0].SignerIdentifier.Type, "SignerInfos[0].SignerIdentifier.Type");
X509IssuerSerial xis = (X509IssuerSerial) cms.SignerInfos[0].SignerIdentifier.Value;
Assert.AreEqual ("CN=Good CA, O=Test Certificates, C=US", xis.IssuerName, "SignerInfos[0].SignerIdentifier.Value.IssuerName");
Assert.AreEqual ("01", xis.SerialNumber, "SignerInfos[0].SignerIdentifier.Value.SerialNumber");
Assert.AreEqual (0, cms.SignerInfos[0].UnsignedAttributes.Count, "SignerInfos[0].UnsignedAttributes.Count");
Assert.AreEqual (1, cms.SignerInfos[0].Version, "SignerInfos[0].Version");
}
开发者ID:nlhepler,项目名称:mono,代码行数:28,代码来源:Pkits_4_01_SignatureVerification.cs
示例5: DefaultProperties
private void DefaultProperties (SignedCms sp, int version)
{
// unaffected by constructors
Assert.AreEqual (0, sp.Certificates.Count, "Certificates");
Assert.AreEqual (0, sp.SignerInfos.Count, "SignerInfos");
Assert.AreEqual (version, sp.Version, "Version");
}
开发者ID:Profit0004,项目名称:mono,代码行数:7,代码来源:SignedCmsTest.cs
示例6: FirmaBytesMensaje
/// <summary>
/// Firma mensaje
/// </summary>
/// <param name="argBytesMsg">Bytes del mensaje</param>
/// <param name="argCertFirmante">Certificado usado para firmar</param>
/// <returns>Bytes del mensaje firmado</returns>
/// <remarks></remarks>
public static byte[] FirmaBytesMensaje(byte[] argBytesMsg, X509Certificate2 argCertFirmante)
{
try
{
// Pongo el mensaje en un objeto ContentInfo (requerido para construir el obj SignedCms)
ContentInfo infoContenido = new ContentInfo(argBytesMsg);
SignedCms cmsFirmado = new SignedCms(infoContenido);
// Creo objeto CmsSigner que tiene las caracteristicas del firmante
CmsSigner cmsFirmante = new CmsSigner(argCertFirmante);
cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly;
if (VerboseMode)
{
Console.WriteLine("***Firmando bytes del mensaje...");
}
// Firmo el mensaje PKCS #7
cmsFirmado.ComputeSignature(cmsFirmante);
if (VerboseMode)
{
Console.WriteLine("***OK mensaje firmado");
}
// Encodeo el mensaje PKCS #7.
return cmsFirmado.Encode();
}
catch (Exception excepcionAlFirmar)
{
throw new Exception("***Error al firmar: " + excepcionAlFirmar.Message);
}
}
开发者ID:javierpernias-santex,项目名称:facturaelectronica,代码行数:39,代码来源:CertificadosX509Lib.cs
示例7: SignerInfo
internal SignerInfo(SignedCms signedCms, System.Security.Cryptography.SafeLocalAllocHandle pbCmsgSignerInfo)
{
this.m_signedCms = signedCms;
this.m_parentSignerInfo = null;
this.m_encodedSignerInfo = null;
this.m_pbCmsgSignerInfo = pbCmsgSignerInfo;
this.m_cmsgSignerInfo = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO));
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:8,代码来源:SignerInfo.cs
示例8: Sign
public byte[] Sign(byte[] data)
{
ContentInfo contentInfo = new ContentInfo(_md5.ComputeHash(data));
SignedCms signedCms = new SignedCms(contentInfo);
CmsSigner cmsSigner = new CmsSigner(_cert);
cmsSigner.IncludeOption = X509IncludeOption.WholeChain;
signedCms.ComputeSignature(cmsSigner);
return signedCms.Encode();
}
开发者ID:NomadPL,项目名称:Nomad,代码行数:9,代码来源:PkiSignatureAlgorithm.cs
示例9: SignerInfo
internal SignerInfo (SignedCms signedCms, SafeLocalAllocHandle pbCmsgSignerInfo) {
// Sanity check.
Debug.Assert(signedCms != null && pbCmsgSignerInfo != null && !pbCmsgSignerInfo.IsInvalid);
m_signedCms = signedCms;
m_parentSignerInfo = null;
m_encodedSignerInfo = null;
m_pbCmsgSignerInfo = pbCmsgSignerInfo;
m_cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));
}
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:10,代码来源:SignerInfo.cs
示例10: GetCertificates
/// <summary>
/// Gets certificates contained in pkcs 7.
/// </summary>
/// <returns>Returns certificates contained in pkcs 7. Returns null if no certificates.</returns>
public X509Certificate2Collection GetCertificates()
{
if(this.Data == null){
return null;
}
SignedCms signedCms = new SignedCms();
signedCms.Decode(this.Data);
return signedCms.Certificates;
}
开发者ID:ChuckLafferty,项目名称:bugnet,代码行数:15,代码来源:MIME_b_ApplicationPkcs7Mime.cs
示例11: CheckSig
public static void CheckSig(byte[] sig, byte[] data)
{
ContentInfo contentInfo = new ContentInfo(data);
SignedCms signedCms = new SignedCms(contentInfo, true);
signedCms.Decode(sig);
// This checks if the signature is valid, but doensn't actually verify the cert (TODO)
signedCms.CheckSignature(true);
signedCms.CheckSignature(false);
}
开发者ID:CACBridge,项目名称:ChromeCAC,代码行数:13,代码来源:CAC.cs
示例12: VerifySign
public static bool VerifySign(byte[] data)
{
try
{
SignedCms signed = new SignedCms();
signed.Decode(data);
}
catch
{
return false; // Arquivo não assinado
}
return true;
}
开发者ID:ozeraydin57,项目名称:Addon-SAP-B1-Default,代码行数:13,代码来源:XmlSignUtil.cs
示例13: Verify
public bool Verify(byte[] data, byte[] signature)
{
var signedCms = new SignedCms();
signedCms.Decode(signature);
try
{
signedCms.CheckSignature(_certificate2Collection, false);
}
catch(Exception e)
{
return false;
}
return signedCms.ContentInfo.Content.SequenceEqual(_md5.ComputeHash(data));
}
开发者ID:NomadPL,项目名称:Nomad,代码行数:14,代码来源:PkiSignatureAlgorithm.cs
示例14: GetSignedMime
/// <summary>
/// Gets signed mime content. Value null means no content.
/// </summary>
/// <returns>Returns signed mime content. Value null means no content.</returns>
/// <remarks>This method is valid only if <b>Content-Type</b> parameter <b>smime-type=signed-data</b>.</remarks>
/// <exception cref="InvalidOperationException">Is raised when <b>smime-type != signed-data</b>.</exception>
public MIME_Message GetSignedMime()
{
if(!string.Equals(this.Entity.ContentType.Parameters["smime-type"],"signed-data",StringComparison.InvariantCultureIgnoreCase)){
throw new InvalidOperationException("The VerifySignature method is only valid if Content-Type parameter smime-type=signed-data.");
}
if(this.Data != null){
SignedCms signedCms = new SignedCms();
signedCms.Decode(this.Data);
return MIME_Message.ParseFromStream(new MemoryStream(signedCms.ContentInfo.Content));
}
else{
return null;
}
}
开发者ID:ChuckLafferty,项目名称:bugnet,代码行数:22,代码来源:MIME_b_ApplicationPkcs7Mime.cs
示例15: CheckFileSignature
public static String CheckFileSignature(ContentInfo content, byte[] signature)
{
var verifyCms = new SignedCms(content, true);
verifyCms.Decode(signature);
var cert = verifyCms.SignerInfos[0].Certificate;
try
{
verifyCms.CheckSignature(new X509Certificate2Collection(cert), false);
return @"Signature is valid";
}
catch (CryptographicException)
{
return @"Signature is not valid for content";
}
}
开发者ID:myagincourt,项目名称:SimpleSmevSigner,代码行数:17,代码来源:Signer.cs
示例16: FirmaBytesMensaje
private byte[] FirmaBytesMensaje( byte[] argBytesMsg, X509Certificate2 argCertFirmante )
{
ContentInfo infoContenido = new ContentInfo( argBytesMsg );
SignedCms cmsFirmado = new SignedCms( infoContenido );
CmsSigner cmsFirmante = new CmsSigner( argCertFirmante );
try
{
cmsFirmante.IncludeOption = X509IncludeOption.EndCertOnly;
cmsFirmado.ComputeSignature( cmsFirmante );
}
catch ( Exception error )
{
this.manejadorErrores.ManejarError( error, "FirmaBytesMensaje", error.Message );
}
return cmsFirmado.Encode();
}
开发者ID:GonzaloFernandoA,项目名称:FacturacionElectronica,代码行数:18,代码来源:FirmadorDeCertificado.cs
示例17: IsSignedBy
public static bool IsSignedBy(this X509Certificate thisCertificate, X509Certificate signerCertificate)
{
X509Certificate2 c = new X509Certificate2(thisCertificate.GetTbsCertificate());
X509Certificate2 i = new X509Certificate2(signerCertificate.GetTbsCertificate());
X509Certificate2 c2 = new X509Certificate2(@"c:\temp\der.cer");
X509Certificate2 i2 = new X509Certificate2(@"c:\temp\cader.cer");
/*byte[] pvSubject = thisCertificate.GetTbsCertificate();
byte[] pvIssuer = signerCertificate.GetTbsCertificate();
*/
System.Text.Encoding.ASCII.GetString(c.RawData);
IntPtr pvSubject = c.Handle;
IntPtr pvIssuer = i.Handle;
int res = SspiProvider.CryptVerifyCertificateSignatureEx(IntPtr.Zero, X509_ASN_ENCODING,
CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, pvSubject,
CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, pvIssuer, 0,
IntPtr.Zero);
Marshal.GetLastWin32Error();
CmsSigner signer = new CmsSigner(i);
SignedCms signedMessage = new SignedCms();
// deserialize PKCS #7 byte array
signedMessage.Decode(thisCertificate.GetTbsCertificate());
Log.Write("Veryfy old");
Log.Write("EndVeryfy old");
Log.Write("Get signer's public key");
var publicKey = signerCertificate.GetPublicKey();
Log.Write("Got signer's public key");
try
{
Log.Write("Veryfy signature");
//TODO: log errors
thisCertificate.Verify(publicKey);
Log.Write("Verified");
}
catch (CertificateException)
{
return false;
}
catch (InvalidKeyException)
{
return false;
}
return true;
}
开发者ID:demonix,项目名称:CertVerifier,代码行数:44,代码来源:X509CertificateExtensions.cs
示例18: GenerateHtmlMessage
private MailMessage GenerateHtmlMessage(string from, string to, string subject, string content, string[] attachmentFilepaths)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(from);
mail.To.Add(to);
mail.Subject = subject;
string body = null;
if (attachmentFilepaths != null && attachmentFilepaths.Length > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append("MIME-Version: 1.0\r\n");
sb.Append("Content-Type: multipart/mixed; boundary=unique-boundary-1\r\n");
sb.Append("\r\n");
sb.Append("This is a multi-part message in MIME format.\r\n");
sb.Append("--unique-boundary-1\r\n");
sb.Append("Content-Type: text/html\r\n"); //could use text/plain as well here if you want a plaintext message
sb.Append("Content-Transfer-Encoding: 7Bit\r\n\r\n");
sb.Append(content);
if (!content.EndsWith("\r\n"))
sb.Append("\r\n");
sb.Append("\r\n\r\n");
foreach (string filepath in attachmentFilepaths)
{
sb.Append(GenerateRawAttachement(filepath));
}
body = sb.ToString();
}
else
{
body = "Content-Type: text/html\r\nContent-Transfer-Encoding: 7Bit\r\n\r\n" + content;
}
//input your certification and private key.
X509Certificate2 cert = new X509Certificate2("emailcertification.pfx", "6522626", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
ContentInfo contentInfo = new ContentInfo(Encoding.UTF8.GetBytes(body));
SignedCms signedCms = new SignedCms(contentInfo, false);
CmsSigner Signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, cert);
signedCms.ComputeSignature(Signer);
byte[] signedBytes = signedCms.Encode();
MemoryStream stream = new MemoryStream(signedBytes);
AlternateView view = new AlternateView(stream, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
mail.AlternateViews.Add(view);
return mail;
}
开发者ID:RandyCode,项目名称:SimpleTools,代码行数:44,代码来源:Program.cs
示例19: GetSignature
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="signingCertificate"></param>
/// <param name="encryptionCertificate"></param>
/// <returns></returns>
internal static byte[] GetSignature(Byte[] message, X509Certificate2 signingCertificate, X509Certificate2 encryptionCertificate)
{
SignedCms signedCms = new SignedCms(new ContentInfo(message), true);
CmsSigner cmsSigner = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, signingCertificate);
cmsSigner.IncludeOption = X509IncludeOption.WholeChain;
if (encryptionCertificate != null)
{
cmsSigner.Certificates.Add(encryptionCertificate);
}
Pkcs9SigningTime signingTime = new Pkcs9SigningTime();
cmsSigner.SignedAttributes.Add(signingTime);
signedCms.ComputeSignature(cmsSigner, false);
return signedCms.Encode();
}
开发者ID:fengweijp,项目名称:higlabo,代码行数:26,代码来源:Cryptography.cs
示例20: Sign
public static SignatureResponse Sign(byte[] data)
{
// TODO:
// padding configuration
// algorithm configuration
// encoding configuration
/*
SHA1Managed sha1 = new SHA1Managed();
byte[] hash = sha1.ComputeHash(data);
var sig = csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
//sig = csp.SignData(Encoding.UTF8.GetBytes(text), CryptoConfig.MapNameToOID("SHA1"));
MessageBox.Show("SignData");
*/
var content = new ContentInfo(data);
var cms = new SignedCms(content, true); // TODO detached config
var signer = new CmsSigner();
signer.IncludeOption = X509IncludeOption.EndCertOnly;
cms.ComputeSignature(signer, false);
var sig = cms.Encode();
//ensure my signature is correct before continuing.
cms.CheckSignature(true);
var newCMS = new SignedCms(content, false);
newCMS.Decode(sig);
newCMS.CheckSignature(true);
var cert = cms.Certificates[0];
CheckSig(sig, data);
return new SignatureResponse
{
publicKey = Convert.ToBase64String(cert.PublicKey.EncodedKeyValue.RawData),
signature = Convert.ToBase64String(sig),
fullSig = null // TODO
};
}
开发者ID:CACBridge,项目名称:ChromeCAC,代码行数:40,代码来源:CAC.cs
注:本文中的System.Security.Cryptography.Pkcs.SignedCms类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论