3

CngKey で生成されたキーのペアから x509 証明書を生成しようとしています。私は以下を使用してキーを作成します。

        var parameters = new CngKeyCreationParameters 
        { 
            Provider = CngProvider.MicrosoftSoftwareKeyStorageProvider, 
            ExportPolicy = CngExportPolicies.AllowPlaintextExport, 
            KeyCreationOptions = CngKeyCreationOptions.OverwriteExistingKey, 
            KeyUsage = CngKeyUsages.AllUsages, 
            UIPolicy = new CngUIPolicy(CngUIProtectionLevels.None) 
        }; 
        var key = CngKey.Create(CngAlgorithm.ECDsaP384, container, parameters); 
        byte[] ecPriKey = key.Export(CngKeyBlobFormat.Pkcs8PrivateBlob); 
        byte[] ecPubKey = key.Export(CngKeyBlobFormat.EccPublicBlob); 

次の方法で BouncyCastle 秘密鍵を取得します。

        AsymmetricKeyParameter akPrivate = PrivateKeyFactory.CreateKey(ecPriKey); 

キーから適切なカーブのパラメータを確認できるので、問題はありません。

ただし、次の方法で公開鍵を取得しようとすると:

          string publicKeyBase64 = Convert.ToBase64String(ecPubKey); 
          byte[] ecPubKey2 = Base64.Decode(publicKeyBase64); 
          byte[] ecPublicKey = new byte[ecPubKey.Length -7]; 
          ecPublicKey[0] = 0x04; 
          Array.Copy(ecPubKey, 8, ecPublicKey, 7, ecPublicKey.Length); 
          AsymmetricKeyParameter akPublic = PublicKeyFactory.CreateKey(ecPublicKey - 1); 

CngKey から最初の 8 桁を消去し、圧縮されていない const 値 0x04 を追加する必要があることを読みました。「PublicKeyFactory.CreateKey(ecPublicKey)」を実行すると、例外が発生します。

ex  {"extra data found after object"}   System.Exception {System.IO.IOException} 
Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal} 
            HResult 0x80131620  int 
            HelpLink    null    string 
InnerException  null    System.Exception 
            Message "extra data found after object" string 
            Source  "BouncyCastle.Crypto"   string 
            StackTrace  "   at Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(Byte[] data)\r\n   at Org.BouncyCastle.Security.PublicKeyFactory.CreateKey(Byte[] keyInfoData)\r\n   at Plpm.Csp.Security.KeyTool.SecurityKeyTool.OpGenEc(String[] args) in ..."  string 
TargetSite  {Org.BouncyCastle.Asn1.Asn1Object FromByteArray(Byte[])}    System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo} 
Static members  
Non-Public members  

とにかく、次のようにキーを使用して直接これを行うと、同じ例外が発生します。

        AsymmetricKeyParameter akPublic = PublicKeyFactory.CreateKey(ecPubKey); 

公開鍵でこのエラーが発生する理由について、誰か教えてください。

どうもありがとう。

4

0 に答える 0