简体   繁体   English

谁能给我一个使用BouncyCastle将.pem公共DSA密钥导入c#的示例吗?

[英]Can anyone give me an example of using BouncyCastle to import .pem public DSA key into c#?

I am trying co import a .pem key into c#, and I've found a library, which does that: BouncyCastle 我正在尝试将.pem密钥共同导入c#,并且我找到了一个库,该库可以做到这一点: BouncyCastle

I've created a code, which loads public key and is supposed to load the data into DSACryptoServiceProvider: 我创建了一个代码,该代码加载公钥,并且应该将数据加载到DSACryptoServiceProvider中:

        DSA dsa;

        using (StreamReader rdr = new StreamReader(@"pubkey.pem"))
        {
            PemReader pr = new PemReader(rdr);
            DsaPublicKeyParameters o = pr.ReadObject() as DsaPublicKeyParameters;
            CspParameters prm = new CspParameters(13);
            prm.Flags = System.Security.Cryptography.CspProviderFlags.UseMachineKeyStore; 
            //o.Parameters.
            dsa = new DSACryptoServiceProvider(prm);
            DSAParameters dp = new DSAParameters();
            dp.G = o.Parameters.G.ToByteArray();
            dp.P = o.Parameters.P.ToByteArray();
            dp.Q = o.Parameters.Q.ToByteArray();                
            dp.Y = o.Y.ToByteArray();

            if (o.Parameters.ValidationParameters != null)
            {
                dp.Counter = o.Parameters.ValidationParameters.Counter;
                dp.Seed = o.Parameters.ValidationParameters.GetSeed();
            }

            //todo: missing: J, X?
            dsa.ImportParameters(dp);
        }

it crashes on dsa.ImportParameters(dp); 它在dsa.ImportParameters(dp)上崩溃; with following exception: Cryptographic exception:bad data. 有以下例外:密码例外:不良数据。

What should I change for this to work? 我该如何更改才能正常工作?

您需要使用ToByteArrayUnsigned方法,而不是普通的ToByteArray一个否则存在以下情况的字节数组表达结束了一个领先的零字节打破一切:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将公共.pem dsa密钥导入C#代码? - How to I import public .pem dsa key into c# code? 如何在c#中使用bouncycastle将pem公钥转换为rsa公钥? - how can i convert pem public key to rsa public key with bouncycastle in c#? 从字节[] / pem字符串获取公共密钥-BouncyCastle C# - Get public key from byte [] / pem string - BouncyCastle C# 如何使用BouncyCastle(C#)以ASN.1格式导入DSA签名 - How to import DSA signature in ASN.1 format using BouncyCastle (C#) 如何从PEM文件中读取RSA公钥,并使用它在C#中的BouncyCastle中进行加密? - How do I read RSA public key from PEM file and use it to encrypt in BouncyCastle in C#? 如何使用BouncyCastle C#将RSA公钥转换为字符串 - How to convert RSA public key to string using BouncyCastle c# C#:谁能给我一个很好的例子,说明如何在运行时锚定控件? - C#: Could anyone give me good example on how anchoring controls at runtime is done? 如何使用C#使用公钥使用Bouncycastle解密pgp文件 - How to decrypt pgp file with bouncycastle using c# using the public key C#RSA在BouncyCastle的帮助下使用给定的公钥加密文本 - C# RSA Encrypting text using a given public Key with help of BouncyCastle 使用 BouncyCastle 和 C# 中的唯一公钥加密文件? - Encrypt file with BouncyCastle and only public key in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM