CLR セキュリティ ライブラリ Security.Cryptography.dll (ホームページ: http://clrsecurity.codeplex.com )を使用して自己署名証明書を作成しようとしています。
証明書を作成し、それを使用して暗号化することもできますが、「指定されたプロバイダーの種類が無効です」というエラーで復号化に失敗します。
証明書作成コードは次のとおりです。
public static bool generateCertificate(string distinguishedName)
{
// Generate Key
CngKeyCreationParameters keyParams = new CngKeyCreationParameters();
keyParams.KeyCreationOptions = CngKeyCreationOptions.MachineKey | CngKeyCreationOptions.OverwriteExistingKey;
keyParams.KeyUsage = CngKeyUsages.AllUsages; //CngKeyUsages.Decryption | CngKeyUsages.Signing;
keyParams.Provider = CngProvider.MicrosoftSoftwareKeyStorageProvider;
keyParams.ExportPolicy = CngExportPolicies.AllowExport;
CngKey newKey = CngKey.Create(CngAlgorithm2.Rsa, Guid.NewGuid().ToString(), keyParams);
// Init certificate
X509CertificateCreationParameters certParams = new X509CertificateCreationParameters(new X500DistinguishedName(distinguishedName));
certParams.SignatureAlgorithm = X509CertificateSignatureAlgorithm.RsaSha1;
certParams.StartTime = DateTime.Now;
certParams.EndTime = DateTime.Now.AddYears(10);
// Create cert
X509Certificate2 newCert = newKey.CreateSelfSignedCertificate(certParams);
// Save to store
X509Store lmStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
lmStore.Open(OpenFlags.ReadWrite);
lmStore.Add(newCert);
lmStore.Close();
return true;
}
暗号化方法は次のとおりです。
public static byte[] encryptStringPKCS7(string toEncrypt, List<byte[]> recipients)
{
// get bytes from encrypt text
UnicodeEncoding unicode = new UnicodeEncoding();
byte[] msgBytes = unicode.GetBytes(toEncrypt);
// Create the certificate collection of the intended recipients
X509Certificate2Collection recipientsCollection = new X509Certificate2Collection();
foreach (byte[] currCertificate in recipients)
{
recipientsCollection.Add(new X509Certificate2(currCertificate));
}
// Place message in a ContentInfo object.
ContentInfo contentInfo = new ContentInfo(msgBytes);
EnvelopedCms envelopedCms = new EnvelopedCms(contentInfo);
// Formulate a CmsRecipientCollection object that
// represents information about the set of recipients
// to encrypt the message for.
CmsRecipientCollection cmsRecipients = new CmsRecipientCollection(SubjectIdentifierType.IssuerAndSerialNumber, recipientsCollection);
// Encrypt the message for the collection of recipients.
envelopedCms.Encrypt(cmsRecipients);
return envelopedCms.Encode();
}
Decrypt メソッドは次のとおりです。
public static string decryptStringPKCS7(byte[] toDecrypt)
{
// Place message in a ContentInfo object.
// This is required to build an EnvelopedCms object.
EnvelopedCms envelopedCms = new EnvelopedCms();
// Decrypt the message
envelopedCms.Decode(toDecrypt);
envelopedCms.Decrypt();
byte[] msgDecrypted = envelopedCms.ContentInfo.Content;
// Decode
string msgClearText = Encoding.Unicode.GetString(msgDecrypted);
return msgClearText;
}
makecert.exe で作成した証明書とまったく同じコードを使用すると、うまく機能します。私が使用するmakecertコマンドラインは次のとおりです。makecert.exe -sr LocalMachine -ss My -n CN=[SomeDN] -sk [SomeRandomGUID] -sky exchange
CngKey または証明書の作成時に追加のパラメーターを指定する必要がありますか? それとも、暗号化中に追加情報を渡しますか?
助けてくれてありがとう!