当前位置: 首页 > 编程语言 > C#

从证书导入RSA公钥共享

时间:2023-04-10 20:58:59 C#

从证书导入RSA公钥我们的客户将他们的RSA公钥存储在证书中。我们需要在WinRT应用程序中硬编码此密钥,以便我们可以加密客户端。但是,我们在将密钥导入CryptographicKey类的实例时遇到了问题。我们在RSAProvider上使用ImportPublicKey:rsaProvider=AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);key=rsaProvider.ImportPublicKey(publicKeyBuffer);我们已经尝试将几样东西加载到publicKeyBuffer中:证书,从各种格式的公钥证书中导出。我们如何加载他们的公钥?我发现MSDN论坛上的这篇文章非常有帮助。CarlosLopez发布了一些用于从Base64编码证书中获取公钥的代码。http://social.msdn.microsoft.com/Forums/en-US/17e1467a-2de7-47d2-8d8c-130518eaac68/how-to-use-a-x509-certificate-not-a-pfx-to-verify-这里的一个签名代码:publicstaticCryptographicKeyGetCryptographicPublicKeyFromCert(stringstrCert){intlength;CryptographicKeyCryptKey=null;byte[]bCert=Convert.FromBase64String(strCert);//假设Cert包含RSA公钥//在证书中找到匹配的OID并返回公钥byte[]rsaOID=EncodeOID("1.2.840.113549.1.1.1");intindex=FindX509PubKeyIndex(bCert,rsaOID,outlength);//在证书中找到X509PublicKey,所以复制它。if(index>-1){byte[]X509PublicKey=newbyte[length];Array.Copy(bCert,索引,X509PublicKey,0,长度);AsymmetricKeyAlgorithmProviderAlgProvider=AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);CryptKey=AlgProvider.ImportPublicKey(CryptographicBuffer.CreateFromByteArray(X509PublicKey));}返回密码密钥;}静态整数查找X509PubKeyIndex(byte[]Reference,byte[]value,outintlength){intindex=-1;布尔找到;长度=0;for(intn=0;n-1){//查找外部序列while(index>0&&Reference[index]!=0x30)index--;指数-;while(index>0&&Reference[index]!=0x30)index--;}if(index>-1){//查找编码公钥的长度if((Reference[index+1]&0x80)==0x80){intnumBytes=Reference[index+1]&0x7F;对于(intm=0;m=16384){pbEncodedTemp[index++]=Convert.ToByte(num/16384|0x80);num=num%16384;计数++;}if(num>=128){pbEncodedTemp[index++]=Convert.ToByte(num/128|0x80);数=数%128;计数++;}pbEncodedTemp[index++]=Convert.ToByte(num);计数++;}pbEncodedTemp[1]=Convert.ToByte(count);pbEncoded=新字节[count+2];Array.Copy(pbEncodedTemp,0,pbEncoded,0,count+2);返回pbEncoded;}staticpublicint[]ParseOID(stringszOID){intnlast,n=0;布尔fFinished=假;整数计数=0;int[]dwNums=null;做{nlast=n;n=szOID.IndexOf(".",nlast);如果(n==-1)fFinished=true;计数++;n++;}while(fFinished==false);dwNums=newint[计数];计数=0;f完成=假;做{nlast=n;n=szOID.IndexOf(".",nlast);if(n!=-1){dwNums[count]=Convert.ToInt32(szOID.Substring(nlast,n-nlast),10);}else{fFinished=true;dwNums[count]=Convert.ToInt32(szOID.Substring(nlast,szOID.Length-nlast),10);}n++;计数++;}while(fFinished==false);返回dwNums;}两件事:ImportPublicKey键的参数是一个IBuffer最简单的方法是使用ToBuffer扩展方法来获取byte[]。使用ImportPublicKey覆盖,它接受IBuffer和CryptographicPublicKeyBlobType,特别是CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo。从证书中传入主题公钥信息字段。对于那些了解如何在WinRT应用程序中使用存储在证书中的公钥的人,让我来减轻你的痛苦:你不能,至少不能直接使用。AsymmetricKeyAlgorithmProvider.ImportPublicKey函数接受一个I??Buffer和一个CryptographicPublicKeyBlobType,keyBlob(IBuffer)参数是证书的公钥,不是完整的证书,只有公钥。但是如果不首先解析证书就无法获取证书的公钥,这就是问题所在,无法在WinRT上解析证书,因为最常用于此任务的类X509Certificate不可用,其命名空间也不可用,证书工具仅用于网络服务连接。解决此问题的唯一方法是实现证书解析器,或从BouncyCastle等开源项目移植此类功能。所以,如果你知道其中一个,请在评论中留下它。顺便说一句,要以可在WinRT应用程序中使用的格式从证书(在.NET中)导出公钥,请使用:X509Certificate2Certificate;....byte[]CertificatePublicKey=Certificate.PublicKey.EncodedKeyValue.RawData;然后在WinRT应用程序中使用这样:IBufferKeyBuffer=CryptographicBuffer.DecodeFromBase64String(CertificatePublicKeyContent);CryptographicKeykey=algorithm.ImportPublicKey(KeyBuffer,CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);请注意,我首先在base64中对公钥进行编码,但您可以使用原始二进制数据(CryptographicBuffer类有更多用于此目的的方法)。以上就是C#学习教程:从证书导入RSA公钥共享的全部内容。侵权请点击右侧联系管理员删除。如需转载请注明出处: