如何从Web服务器获取和比较证书?

时间:2011-11-22 05:23:02

标签: c# java authentication httpwebrequest certificate

我正在尝试将Java代码移植到C#,到目前为止我的成功落后于Web证书...... 我可以成功地获得相同的结果传递参数w / o比较,但我觉得这需要实现...

这是我用来登录服务器并提交数据的基本技术:How to use HttpWebRequest to login to website? ...之后,我从响应流中提取数据。一切正常,但我想知道我是否应该进行比较。

[Java]〜最初做了什么

// connection is an HttpsURLConnection object
Certificate[] certs = connection.getServerCertificates();

byte[] bytes = getKey(); // gets the file.key byte array

Certificate c = certs[0];
PublicKey pk = c.getPublicKey();
byte[] data = pk.getEncoded();

for (int i = 0; i < data.length; i++) {
     if (data[i] == bytes[i]) continue;
     throw new RuntimeException("Public key mismatch");
}

[C#]〜我目前正在尝试做什么

byte[] bytes = Properties.Resources.Key;
// connection is a HttpWebRequest object
X509CertificateCollection collection = connection.ClientCertificates;
X509Certificate c = collection[0];
// I'm practically guessing on the parameters because I'm new to the
// web handling in C#...
PublicKey pk = new PublicKey(
    new Oid(), 
    new AsnEncodedData(Encoding.UTF8.GetBytes(urlParameters)), 
    new AsnEncodedData(c.GetPublicKey()));
byte[] data = pk.EncodedKeyValue.RawData;
for (int i = 0; i < data.Length; i++)
    if (data[i] == bytes[i]) continue; 
    else throw new Exception("Public key mismatch");

0 个答案:

没有答案