Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the RSACryptoServiceProvider to sign a hash from a server and then verifying the hash from client side
    primarykey
    data
    text
    <p>Hi there I've been looking all over the internet and I can't quite find a solution to my problem of signing and verifying. First of all I want to make sure I'm on the right track when it comes to understanding what happens so please correct me. The first thing when it comes to signing is that the server first creates two keys one public and one private. The server then puts data text, the public key, and then hashed and encrypted(using it's private key) data text on a file. The server sends the file to the client and the client starts to verify the data by getting the public key in the file and uses that to decrypt the encrypted data. Finally, the client uses the hash algorithm (same one as the server) to the text data and compares it to the data that was decrypted.</p> <p>If this is all right then I don't understand why my code isn't working:</p> <p>Server:</p> <pre><code>string name = textBox1.Text; string GUID = textBox2.Text; string startDate = textBox3.Text; string EndDate = textBox4.Text; string macAddress = GetMacAddress(); FileStream fs = File.Create(@"cert.txt"); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); UnicodeEncoding ByteConverter = new UnicodeEncoding(); StreamWriter write = new StreamWriter(fs); write.Write(name + "\r\n"); write.Write(GUID + "\r\n"); write.Write(startDate + "\r\n"); write.Write(EndDate + "\r\n"); write.Write(macAddress + "\r\n"); string pkey = RSA.ToXmlString(false); write.Write(pkey + "\r\n"); SHA1Managed Sha = new SHA1Managed(); string info = name + GUID + startDate + EndDate + macAddress; byte [] hashed = Sha.ComputeHash(Encoding.UTF8.GetBytes(info)); byte []signature = RSA.SignData(hashed,CryptoConfig.MapNameToOID("SHA1")); write.Write(Convert.ToBase64String(signature)); textBox5.Text = Convert.ToBase64String(hashed); write.Close(); fs.Close(); </code></pre> <p>Client:</p> <pre><code>FileStream fsSource = new FileStream(@"cert.txt", FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fsSource); string name = reader.ReadLine(); string GUID = reader.ReadLine(); string startDate = reader.ReadLine(); string EndDate = reader.ReadLine(); string macAddress = reader.ReadLine(); string pkey = reader.ReadLine(); string signed = reader.ReadLine(); RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(pkey); string info = name + GUID + startDate + EndDate + macAddress; SHA1Managed Sha = new SHA1Managed(); byte[] checkinghash = Sha.ComputeHash(Encoding.UTF8.GetBytes(info)); if (RSA.VerifyHash(checkinghash, CryptoConfig.MapNameToOID("SHA1"), Encoding.UTF8.GetBytes(signed))) { Console.WriteLine("verfied"); } else { Console.WriteLine("denied"); } Console.WriteLine(); //Console.WriteLine(signed); Console.ReadKey(); </code></pre> <p>This always produces denied as I'm not sure if I'm missing the public key or if this is wrong way to go about it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload