如何簽名包含常規文本的文件?如何簽署文本文件或任何其他xml
我馬上解釋。
我在C#中創建了一個應用程序,它允許您使用USB令牌上的證書籤署XML文檔。我從集合中選擇一個證書:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
X509Certificate2 myCert = null;
X509Store st = new X509Store();
st.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = X509Certificate2UI.SelectFromCollection(st.Certificates, "Choose certificate:", "", X509SelectionFlag.SingleSelection);
然後我用的一切,我走到這一步,簽署這個XML文檔:
public static string SignXmlWithCertificate(XmlDocument Document, X509Certificate2 cert)
{
SignedXml signedXml = new SignedXml(Document);
// pure black magic
signedXml.SigningKey = cert.PrivateKey;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env =
new XmlDsigEnvelopedSignatureTransform(true);
reference.AddTransform(env);
//canonicalize
XmlDsigC14NTransform c14t = new XmlDsigC14NTransform();
reference.AddTransform(c14t);
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoData = new KeyInfoX509Data(cert);
KeyInfoName kin = new KeyInfoName();
kin.Value = "Public key of certificate";
RSACryptoServiceProvider rsaprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;
RSAKeyValue rkv = new RSAKeyValue(rsaprovider);
keyInfo.AddClause(kin);
keyInfo.AddClause(rkv);
keyInfo.AddClause(keyInfoData);
signedXml.KeyInfo = keyInfo;
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
Document.DocumentElement.AppendChild(
Document.ImportNode(xmlDigitalSignature, true));
return Document.OuterXml;
}
實際問題
我的問題是,該怎麼辦與純文本文件一樣。我的意思是當我使用簽名軟件時,我可以看到幾乎相同的文件,除了文本(而不是xml)是用base64編碼的。
我在C#中使用哪些方法來簽署常規文件?我無法將它加載到XmlDocument
,因爲它只是不是XML。
我知道你會問無論如何,所以是的。我試過在谷歌中找到它,但我想我只是用錯誤的詞來搜索它。所以,任何幫助感激。
看看是否有幫助:https://blogs.msdn.microsoft.com/alejacma/2008/06/25/how-to-sign-and-verify-the-signature-with-net-and-a- certificate-c/ – neo