我正在尝试转换此代码:
/**
* Create a new PublicKey from encoded X.509 data
*/
public static PublicKey decodePublicKey(byte[] par0ArrayOfByte)
{
try
{
X509EncodedKeySpec var1 = new X509EncodedKeySpec(par0ArrayOfByte);
KeyFactory var2 = KeyFactory.getInstance("RSA");
return var2.generatePublic(var1);
}
catch (NoSuchAlgorithmException var3)
{
var3.printStackTrace();
}
catch (InvalidKeySpecException var4)
{
var4.printStackTrace();
}
System.err.println("Public key reconstitute failed!");
return null;
}
进入C#代码,但我在C#中找不到任何有效的解决方案
完整的代码,您可以在这里找到:代码
我的最后一个解决方案是
public PublicKey decodePublicKey(byte[] Data)
{
X509Certificate2 x509 = new X509Certificate2();
x509.Import(Data);
return x509.PublicKey;
}
但我得到错误
mscorlib.dll中发生了类型为'System.Security.Cryptography.CryptographicException'的未处理异常。其他信息:找不到请求的对象。
X509EncodedKeySpec
是SubjectPublicKey
证书的一部分。因此,您可能需要解码此结构。您可以查看BouncyCastle for C#并签出Org.BouncyCastle.Asn1.X509.SubjectPublicKeyInfo.GetInstance(byte[])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句