Digital Signature Verification in .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hdsingh71
    New Member
    • Jul 2008
    • 2

    Digital Signature Verification in .NET

    We have two files, one is plain text another is signature file. We have to verify the integrity of the message.

    The details of SIG file is as under:
    Digital signature in PKCS7 format
    Signatures are attached in detach mode
    Algorithm : SHA1
    Signature rule: End certificate of the User. .

    What we are doing is
    1. Calculating the hash of plain text file say it is hasPlain
    2. Extracting the public key from SIG file
    3. Extracting the Encrypted Hash from SIG file
    4. Decrypting the above hash by using public key say it will give us hashSign
    5. Comparing hashPlain and hasSign, if both are equal then signature is verified
    6. We are using RSACryptoservic eprovider class of .net

    We are able to extract the public key from SIG file but we are not able to extract Encrypted Hash from SIG File.

    Please help
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    So what have you tried so far?

    Comment

    • hdsingh71
      New Member
      • Jul 2008
      • 2

      #3
      Code:
      X509Certificate2 x509_2 = new X509Certificate2("c:\\EP00000100008052201.sig")
      byte[] cer_data = x509_2.PublicKey.Key
      
      FileStream fsw = new FileStream("C:\\cc.cer", FileMode.OpenOrCreate, FileAccess.Write);
                  fsw.Write(cer_data, 0, cer_data.Length);
                  fsw.Close();
      // certificate has been generated 
      
                  FileStream fs1 = new FileStream("c:\\EP00000100008052201.sig", FileMode.Open, FileAccess.Read);
                  byte[] tot_bytes = new byte[fs1.Length];
                  fs1.Read(tot_bytes, 0, tot_bytes.Length);
                  fs1.Close();
                  fsw = new FileStream("C:\\left.sig", FileMode.OpenOrCreate,FileAccess.Write);
      
                  string shouldIWrite;
                  int tot = 0;
                  for (int i = 0; i < tot_bytes.Length; i++)
                  {
                      shouldIWrite = "Y";
                      for (int j = 0; j < cer_data.Length; j++)
                      {
                          if (tot_bytes[i] == cer_data[j])
                              shouldIWrite = "N";
                      }
                      if (shouldIWrite == "Y")
                      {
                          tot++;
                          fsw.WriteByte(tot_bytes[i]);
                      }
                  }
                  fsw.Close();
      
      
                  FileStream fs = new FileStream("c:\\EP00000100008052201.txt", FileMode.Open, FileAccess.Read);
                  byte[] plainbyte = new byte[fs.Length];
                  fs.Read(plainbyte, 0, plainbyte.Length);
                  SHA1 sha = new SHA1CryptoServiceProvider();
                  byte[] plain_hash = sha.ComputeHash(plainbyte);
                  fs.Close();
      
                  fs = new FileStream("C:\\left.sig", FileMode.Open, FileAccess.Read);
                  byte[] leftout_bytes = new byte[fs.Length];
                  fs.Read(leftout_bytes, 0, leftout_bytes.Length);
                  fs.Close();
      
                  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                  rsa.FromXmlString(x509_2.PublicKey.Key.ToXmlString(false));
      
                  byte[] encrypted_data = rsa.Decrypt(leftout_bytes,true);
      
      // here we are facing the problem
      // once we will decrypt it we can get the hash and then compare with the hash of plain file calculated earlier
                  
      
                  if (plainbyte == encrypted_data)
                      MessageBox.Show("Y");
                  else
                      MessageBox.Show("N");
      Last edited by Curtis Rutland; Aug 5 '08, 01:10 PM. Reason: Added Code Tags - Please use the # button

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Have you manually checked that the decryption is working properly?
        Are you sure that you are reading leftout_bytes contains all of the bytes required for the decryption to work correctly?

        -Frinny

        Comment

        Working...