替代" System.Security.Cryptography.Pkcs"在Windows Mobile 6上

时间:2014-10-10 13:16:09

标签: c# cryptography windows-mobile-6.5

很长一段时间以来,我在使用C#和DotNetFramework 3.5开发的 Windows Mobile 应用程序中搜索“System.Security.Cryptography.Pkcs”的替代方案。实际上,我想要签名并加密XML文件。我已经在桌面应用程序上实现了这个操作:

X509Certificate2 cert = myCert;

// create ContentInfo (what is signed)
String msg = System.IO.File.ReadAllText(xmlPath);
byte[] msgBytes = Encoding.UTF8.GetBytes(msg);
ContentInfo content = new ContentInfo(msgBytes);

// object representing a signed message
SignedCms signedMessage = new SignedCms(content);

CmsSigner signer = new CmsSigner(cert);

signer.DigestAlgorithm = new Oid("SHA1");
signer.IncludeOption = X509IncludeOption.EndCertOnly;

// sign the message
signedMessage.ComputeSignature(signer, false);
byte[] cmsMessage = signedMessage.Encode();

// Encrypt message
X509Certificate2 certificatePK = new X509Certificate2(File.ReadAllBytes(publicKeyPDAPath));

ContentInfo contentSm = new ContentInfo(cmsMessage);
EnvelopedCms envelopedCms = new EnvelopedCms(contentSm);

CmsRecipient recipient = new CmsRecipient(SubjectIdentifierType.SubjectKeyIdentifier, certificatePK);
envelopedCms.Encrypt(recipient);

byte[] encryptedBytes = envelopedCms.Encode();

string str = System.Text.Encoding.Default.GetString(encryptedBytes);

如何为WinCE调整此代码?它是否有Windows Mobile的替代品?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Windows Mobile拥有Cryptography API。我不认为微软为它创建了一个很好的C#BCL包装器,所以你需要P / Invoke这些函数。

有文件加密example here