2013-08-06 165 views
1

我想計算X509證書的CERT_KEY_IDENTIFIER_PROP_ID,將其無提示地添加到Windows Mobile設備的註冊表中(分段期間)。作爲this site,計算公式如下:計算X509證書的密鑰標識

SEQ[SEQ[rsa], key]

我猜keycert.GetPublicKey(),但什麼的意思與rsa這裏(而不是算法我猜的)?

現在在網上搜索了三個小時,如果有人能指引我進入正確的方向,我將非常高興。

回答

3

讀書,我需要寫入註冊表鍵的屬性,我終於用下面的方法的CryptoAPI:

[DllImport("crypt32.dll", SetLastError = true)] 
private static extern IntPtr CertCreateCertificateContext(int dwCertEncodingType, byte[] pbCertEncoded, int cbCertEncoded); 

[DllImport("crypt32.dll", SetLastError = true)] 
private static extern bool CertFreeCertificateContext(IntPtr pCertContext); 

[DllImport("crypt32.dll", SetLastError = true)] 
private static extern bool CertGetCertificateContextProperty(IntPtr pCertContext, int dwPropId, IntPtr pvData, ref int pcbData); 

private byte[] GetKeyIdentifier(X509Certificate certificate) 
{ 
    var data = certificate.GetRawCertData(); 

    var context = CertCreateCertificateContext(1, data, data.Length); 

    try 
    { 
    return ReadProperty(context, 0x14); 
    } 
    finally 
    { 
    CertFreeCertificateContext(context); 
    } 
} 

private byte[] ReadProperty(IntPtr context, int property) 
{ 
    var length = 0; 

    // determine the ammount of memory to allocate for the data 
    if (CertGetCertificateContextProperty(context, property, IntPtr.Zero, ref length)) 
    { 
    var pointer = Marshal.AllocCoTaskMem(length); 

    try 
    { 
     // query the property which is written to the allocated memory 
     if (CertGetCertificateContextProperty(context, property, pointer, ref length) == false) 
     { 
     throw new InvalidOperationException(string.Format("Failed to query property {0}.", property)); 
     } 

     // extract the data from the unmanaged memory 
     var buffer = new byte[length]; 
     Marshal.Copy(pointer, buffer, 0, length); 

     return buffer; 
    } 
    finally 
    { 
     Marshal.FreeCoTaskMem(pointer); 
    } 
    } 
    else 
    { 
    throw new InvalidOperationException(string.Format("Failed to query property {0}.", property)); 
    } 
}