Title: How do I get and compare Certificates from web severs?
Tags: c#;java;authentication;httpwebrequest;certificate
Question: I am trying to port Java code to C# and my success so far falls behind web certificates...
I can successfully get the same results passing in parameters w/o the comparison, but I feel this needs to be implemented...
This is the basic technique I'm using to sign on to the server and submit data: How to use HttpWebRequest to login to website?
...after that, I extract the data from the response stream. Everything works, but I am wondering if I should even need the comparison.
[Java] ~what is originally done
// 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#] ~what I'm currently trying to do
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");



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:11/22/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><java><authentication><httpwebrequest><certificate>', 'Title': 'How do I get and compare Certificates from web severs?', 'ViewCount': 317, 'id': 8222352, 'user_AboutMe': '', 'user_AccountId': 914792.0, 'user_DisplayName': 'TekuConcept', 'user_Reputation': 1214.0, 'user_Views': 122.0, 'user_id': 1416320.0}}