2

For quite some time, I search an alternative for “System.Security.Cryptography.Pkcs” on a Windows Mobile application developed with C# and DotNetFramework 3.5. In fact, I want sign and encrypt a XML file. I’ve already realized this operation on a desktop application:

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);

How can I adapt this code for WinCE? Does it have an alternative for Windows Mobile?

Thank you for your help!

Amatukami
  • 93
  • 1
  • 7

1 Answers1

0

Windows Mobile has the Cryptography API. I don't think Microsoft has created a nice C# BCL wrapper for it, so you will need to P/Invoke the functions.

There is a file encryption example here

PaulH
  • 7,759
  • 8
  • 66
  • 143