問題描述
此代碼與反垃圾郵件工作中使用的 DKIM 簽名驗證有關(guān).
This code relates to DKIM signature verification used in anti-spam efforts.
我有一個來自 s1024._domainkey.yahoo.com
的 byte[]
是 ASN.1 編碼的,但我不知道僅此一項是否包含足夠的信息實現(xiàn)公鑰.
I have a byte[]
from s1024._domainkey.yahoo.com
that is ASN.1 encoded, but I don't know if that alone contains enough information to materialize a public key.
基于這個類,看來我可以將 ASN.1 密鑰轉(zhuǎn)換為 X509Certificate 公鑰,但我需要提供 OID 和一些 ASN.1 編碼的參數(shù).
Based on this class, it appears I can convert an ASN.1 key into a X509Certificate Public key, but I need to supply an OID and some ASN.1-encoded parameters.
在此示例中,我有 ASN1 密鑰為的元數(shù)據(jù):
In this example I have metadata that the ASN1 key is:
- RSA 編碼的密鑰(ASN.1 DER 編碼的 [ITU-X660-1997] RSAPublicKey 每 RFC3447)
- 與任一 sha1 sha256 哈希算法一起使用
- 使用 RFC3447 的 A.2 節(jié)中與下表相關(guān)的 OID(盡管我不知道如何將此信息轉(zhuǎn)換為完整的 OID)
/*
* 1.2.840.113549.1
*
MD2 md2WithRSAEncryption ::= {pkcs-1 2}
MD5 md5WithRSAEncryption ::= {pkcs-1 4}
SHA-1 sha1WithRSAEncryption ::= {pkcs-1 5}
SHA-256 sha256WithRSAEncryption ::= {pkcs-1 11}
SHA-384 sha384WithRSAEncryption ::= {pkcs-1 12}
SHA-512 sha512WithRSAEncryption ::= {pkcs-1 13}
*/
代碼示例
<代碼>串PUBKEY = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDrEee0Ri4Juz + QfiWYui/E9UGSXau/2P8LjnTD8V4Unn + 2FAZVGE3kL23bzeoULYv4PeleB3gfmJiDJOKU3Ns5L4KJAUUHjFwDebt0NP + sBK0VKeTATL2Yr/S3bT/XHY + 1xtj4RkdV7fVxTn56Lb4udUnwuxK4V5b5PdOKj/+ XcwIDAQAB";byte[] pubkeyByteArray = Convert.FromBase64String(pubkey);AsnEncodedData aData = new AsnEncodedData(pubkeyByteArray);//OID 不能為空,但它就在這里.它是什么?System.Security.Cryptography.X509Certificates.PublicKey pubKeyRdr = new System.Security.Cryptography.X509Certificates.PublicKey(aData.Oid, null, aData);
問題
- 我應該使用什么 OID?
- 有哪些 ASN.1 參數(shù)示例?
推薦答案
更新
這是您在使用 鏈接 @erickson 提供的解析時提供的數(shù)據(jù):
This is the data you have provided when it is parsed using the link @erickson provided:
SEQUENCE (2 elem)
SEQUENCE (2 elem)
OBJECT IDENTIFIER 1.2.840.113549.1.1.1
NULL
BIT STRING (1 elem)
SEQUENCE (2 elem)
INTEGER(1024 bit)
INTEGER 65537
前面代碼之所以拋出ASN1 bad tag value met.
異常是因為aData
包含不??正確的數(shù)據(jù)(包含以上所有數(shù)據(jù)).據(jù)我所知,System.Security.Cryptography.X509Certificates.PublicKey
的 3 個參數(shù)是如何分解的.
The reason the previous code throws a ASN1 bad tag value met.
exception is because aData
contains incorrect data (contains all the data above). From what I've seen, the is how the 3 arguments to System.Security.Cryptography.X509Certificates.PublicKey
are broken down.
- 第一個參數(shù)是OID,也就是上面的OBJECT IDENTIFIER.
- 第二個參數(shù)是公鑰參數(shù).在上面的解析中,你可以看到它是NULL.
- 第三個參數(shù)是實際的公鑰值.這由上面的第三個序列組成.該序列有 2 個整數(shù),一個 1024 位模數(shù),后跟公共指數(shù).
我使用下面的代碼對其進行了測試.如果不編寫 DER 解析器,我找不到解析數(shù)據(jù)的內(nèi)置方法.
I tested it using the code below. I couldn't find a built-in method to parse the data without writing a DER parser.
Oid oid = new Oid("1.2.840.113549.1.1.1");
AsnEncodedData keyValue = new AsnEncodedData(getBytes("30818902818100EB11E7B4462E09BB3F907E2598BA2FC4F541925DABBFD8FF0B8E74C3F15E149E7FB6140655184DE42F6DDBCDEA142D8BF83DE95E07781F98988324E294DCDB392F82890145078C5C0379BB7434FFAC04AD1529E4C04CBD98AFF4B76D3FF1872FB5C6D8F8464755EDF5714E7E7A2DBE2E7549F0BB12B85796F93DD38A8FFF97730203010001"));
AsnEncodedData keyParam = new AsnEncodedData(new byte[] {05, 00});
PublicKey pubKeyRdr = new System.Security.Cryptography.X509Certificates.PublicKey(oid, keyParam, keyValue);
System.Diagnostics.Debug.WriteLine(pubKeyRdr.Key.KeyExchangeAlgorithm);
System.Diagnostics.Debug.WriteLine(pubKeyRdr.Key.KeySize);
它輸出RSA-PKCS1-KeyEx
和1024
.
這篇關(guān)于將 ASN.1 數(shù)據(jù)轉(zhuǎn)換為公鑰需要什么?例如如何確定 OID?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!