title: How to use c# to decrypt an MD5 hash that is encrypted using RSA1024
body: <p>I trying to verify the integrity of a file at work and an having a hard time of it. I'm not very well versed with encryption and hashing, so bear with me.</p>
  
  <p>I have some files that have an MD5 hash located at the end of them. I have written code to grab the bytes that I think are the hash and they seen to be uniformly 128 bytes long. In the file, just before the hash, is the keyword "RSA1024", which I have taken to mean the hash is encrypted using RSA 1024.</p>
  
  <p>I have what I know is the RSA key in a file, and have read out the bytes (always 258 bytes long). I have seen many tutorials which use FromXmlString() to pull in the key, but this RSA key was not generated using the .net framework, and is not in an XML format.</p>
  
  <p>I have written the following method to decrypt the hash data using the key, and it throws this error when executing ImportCspBlob() - System.Security.Cryptography.CryptographicException: Bad Version of provider.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>    public byte[] DecryptRSA(byte[] encryptedData, byte[] keyData)
      {
          CspParameters param = new CspParameters();
          param.Flags = CspProviderFlags.UseExistingKey;
          RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(param);
  
          rsaProvider.ImportCspBlob(keyData);
  
          byte[] decryptedData = rsaProvider.Decrypt(encryptedData, false);
  
          return decryptedData;
      }
  </code></pre>
  
  <h1>Basic Algorithm</h1>
  
  <p>It may sound strange to want to <em>"decrypt an MD5 hash"</em>, and especially when one says that they want to <em>"decrypt it with a public key"</em>. But that is how digital signatures work. With RSA you can:</p>
  
  <ul>
  <li><strong>encrypt</strong> with private key</li>
  <li><strong>decrypt</strong> with the public key</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/HPU1x.png" alt="enter image description here"></p>
  
  <p>The message digest is <em>encrypted</em> with the private key, and can then only be decrypted with the <em>public</em> key. That way you know that only the person with the private key could have signed the message.</p>