X509 証明書を使用した暗号化と復号化がどのように機能するかを示す簡単なサンプル コードを作成しています。
public static byte[] Encrypt(byte[] content, X509Certificate2Collection encryptingCertificates)
{
if (content == null)
{
throw new ApplicationException("NullContent");
}
if (encryptingCertificates == null || encryptingCertificates.Count == 0)
{
throw new ApplicationException("NoCertificates");
}
CmsRecipientCollection recipients = new CmsRecipientCollection(SubjectIdentifierType.IssuerAndSerialNumber, encryptingCertificates);
EnvelopedCms dataEnvelope = new EnvelopedCms(new ContentInfo(new Oid("1.2.840.113549.1.7.1"), content), new AlgorithmIdentifier(new Oid("2.16.840.1.101.3.4.1.2")));
dataEnvelope.Encrypt(recipients);
return dataEnvelope.Encode();
}
public static byte[] Decrypt(byte[] encryptedContent, X509Certificate2Collection decryptingCertificates)
{
if (decryptingCertificates == null || decryptingCertificates.Count == 0)
{
throw new ApplicationException("NoCertificates");
}
EnvelopedCms dataEnvelope = new EnvelopedCms();
dataEnvelope.Decode(encryptedContent);
dataEnvelope.Decrypt(decryptingCertificates);
ContentInfo contentInfo = dataEnvelope.ContentInfo;
return contentInfo.Content;
}
そして、私は問題に遭遇しました - (dataEnvelope.Decrypt(decryptingCertificates)) を復号化する必要があるコードは、CryptographicException: Access denied をスローします。
CryptographicException: Access denied.
at System.Security.Cryptography.Pkcs.EnvelopedCms.DecryptContent(RecipientInfoCollection recipientInfos, X509Certificate2Collection extraStore)
at CertificateTestingTool.CertificateResolver.Decrypt(Byte[] encryptedContent, X509Certificate2Collection decryptingCerti
ficates)
at CertificateTestingTool.Program.Main(String[] args)
Windows Server 2012 と Windows 8 で発生します。Win サーバー 2008 と Win 7 でこのコードを確認しましたが、正常に動作します。
追加情報: 私は PKI を使用しません。フォルダー (X509Certificate2Collection.Import(…)) から秘密鍵を含む *.pfx ファイルをインポートし、正常にインポートされました。
public static X509Certificate2Collection GetCertificates(string certPath, string password)
{
X509Certificate2Collection certs = null;
var logger = Log.Logger;
certs = new X509Certificate2Collection();
var flags = X509KeyStorageFlags.DefaultKeySet;
certs.Import(certPath, password, flags);
return certs;
}
誰でもこれで私を助けることができますか?私が理解しているように、新しい OS バージョンでいくつかの許可規則が導入されました。