rsaProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
key = rsaProvider.ImportPublicKey(publicKeyBuffer);
我们如何加载他们的公钥?
我发现MSDN论坛上的这篇文章很有帮助。Carlos Lopez发布了一些代码来从Base64编码的证书中获取公钥。
http://social.msdn.microsoft.com/forums/EN-US/17E1467A-2DE7-47D2-8D8C-130518EAAC68/how-to-use-a-x509-certifice-not-a-pfx-to-verific-a-signature
代码如下:
public static CryptographicKey GetCryptographicPublicKeyFromCert(string strCert)
{
int length;
CryptographicKey CryptKey = null;
byte[] bCert = Convert.FromBase64String(strCert);
// Assume Cert contains RSA public key
// Find matching OID in the certificate and return public key
byte[] rsaOID = EncodeOID("1.2.840.113549.1.1.1");
int index = FindX509PubKeyIndex(bCert, rsaOID, out length);
// Found X509PublicKey in certificate so copy it.
if (index > -1)
{
byte[] X509PublicKey = new byte[length];
Array.Copy(bCert, index, X509PublicKey, 0, length);
AsymmetricKeyAlgorithmProvider AlgProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptKey = AlgProvider.ImportPublicKey(CryptographicBuffer.CreateFromByteArray(X509PublicKey));
}
return CryptKey;
}
static int FindX509PubKeyIndex(byte[] Reference, byte[] value, out int length)
{
int index = -1;
bool found;
length = 0;
for (int n = 0; n < Reference.Length; n++)
{
if ((Reference[n] == value[0]) && (n + value.Length < Reference.Length))
{
index = n;
found = true;
for (int m = 1; m < value.Length; m++)
{
if (Reference[n + m] != value[m])
{
found = false;
break;
}
}
if (found) break;
else index = -1;
}
}
if (index > -1)
{
// Find outer Sequence
while (index > 0 && Reference[index] != 0x30) index--;
index--;
while (index > 0 && Reference[index] != 0x30) index--;
}
if (index > -1)
{
// Find the length of encoded Public Key
if ((Reference[index + 1] & 0x80) == 0x80)
{
int numBytes = Reference[index + 1] & 0x7F;
for (int m = 0; m < numBytes; m++)
{
length += (Reference[index + 2 + m] << ((numBytes - 1 - m) * 8));
}
length += 4;
}
else
{
length = Reference[index + 1] + 2;
}
}
return index;
}
static public byte[] EncodeOID(string szOID)
{
int[] OIDNums;
byte[] pbEncodedTemp = new byte[64];
byte[] pbEncoded = null;
int n, index, num, count;
OIDNums = ParseOID(szOID);
pbEncodedTemp[0] = 6;
pbEncodedTemp[2] = Convert.ToByte(OIDNums[0] * 40 + OIDNums[1]);
count = 1;
for (n = 2, index = 3; n < OIDNums.Length; n++)
{
num = OIDNums[n];
if (num >= 16384)
{
pbEncodedTemp[index++] = Convert.ToByte(num / 16384 | 0x80);
num = num % 16384;
count++;
}
if (num >= 128)
{
pbEncodedTemp[index++] = Convert.ToByte(num / 128 | 0x80);
num = num % 128;
count++;
}
pbEncodedTemp[index++] = Convert.ToByte(num);
count++;
}
pbEncodedTemp[1] = Convert.ToByte(count);
pbEncoded = new byte[count + 2];
Array.Copy(pbEncodedTemp, 0, pbEncoded, 0, count + 2);
return pbEncoded;
}
static public int[] ParseOID(string szOID)
{
int nlast, n = 0;
bool fFinished = false;
int count = 0;
int[] dwNums = null;
do
{
nlast = n;
n = szOID.IndexOf(".", nlast);
if (n == -1) fFinished = true;
count++;
n++;
} while (fFinished == false);
dwNums = new int[count];
count = 0;
fFinished = false;
do
{
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++;
count++;
} while (fFinished == false);
return dwNums;
}
org.apache.axis2.AxisFault:连接已关闭:javax.net.ssl.sslhandShakeException:sun.security.validator.validatoreXception:PKIX路径验证失败:java.security.cert.certPathValidatoreXception:basic constraints检查失败:这不是CA证书
问题内容: 我使用以下步骤创建了一个带有一对私钥/公钥的新Java密钥库,供具有TLS的Java(内部)服务器使用。请注意,证书是自签名的: 1)使用AES256生成密钥 2)生成CA的证书请求 3)生成自签名有效期10年 4)使用KeyStoreExplorer之类的程序将对(私钥和自签名证书)导入新的JKS中 这可行,但是我想在不使用GUI的情况下实现最后一步。 我知道如何仅导入自签名证书:
2)生成CA证书请求 3)生成自签有效期-10年 4)使用KeyStoreExplorer这样的程序将密钥对(私钥和自签名证书)导入到新的JKS中
我已经尝试了各种方法,通过导入一个基于BouncyCastle的X509Certificate实例和相关的私钥(RsaPrivateCrtKeyParameters)。NET X509Certificate2一个RSACryptServiceProvider实例,并将其保存到证书存储区(NET的X509Store,My/CurrentUser)。 在证书存储MMC管理单元中,似乎有一个与证书相关联
如何从Go中的字符串导入RSA公钥,以便用于加密数据? 我的程序应该执行以下操作: > 接收一个用Base64编码的公钥 将此公钥从Base64解码为字节 导入公钥,以便Go的RSA实现可以使用(问题在这个阶段) 加密AES密钥: 提前谢谢! 解决方案: 公钥必须使用crypto/x509包进行解码。 例如: 然后可以使用带有RSA的进行加密。
我正在尝试在BouncyCastle中导入公共和私有ECDH密钥。为了导入公钥,我使用了下面的C#代码,代码运行良好: 公钥:042E3E5CCF6B9AB04BE7A22F3FACCDE73C87E87155394A34815408A896CA18A374DAC669AF36220FC863767F4AF47507C5BC221FC4A19874DAF39B074E3EB8 私钥:be3f9bf