0

I Have a hash that I've created from a PDF file with the function below:

using System.Security.Cryptography;

private readonly SHA256 fileToSha256 = SHA256.Create();

public string FileToBase64SHA256(string fileName)
{
   using(FileStream stream = File.OpenRead(fileName))
   {
      byte[] streamToSHA = fileToSha256.ComputeHash(stream);                
      return Convert.ToBase64String(streamToSHA);
   }
}

How could I Decrypt the result hash above and save it in a PDF file again? I am trying something like this, but the file saved is corrupted, with strange characters:

public string HashToFile(string hashedFile, string fileName)
{
    BinaryWriter Writer = null;        
    byte[] data;

    try
    {
        Writer = new BinaryWriter(File.OpenWrite(fileName));

        data = Convert.FromBase64String(hashedFile);              
        Writer.Write(data);
        Writer.Flush();
        Writer.Close();
        
        return "Saved Successfully";
    }
    catch
    {
        return "Error converting to File";
    }
}
JPT
  • 13
  • 2
  • 5
    I'm not really sure if I understand what you want to do. A [hash is not decryptable](https://www.techsolvency.com/passwords/dehashing-reversing-decrypting/). What are you doing in your second code snippet is just decoding the base64 encoded hash and store the binary representation into a file. But just think for a moment: how long is your base64 encoded hash? I guess it's just 44 characters long (256 bit hash) . How large is the pdf-file? 1k, 10k, 100k? Would you really expect to condense a file of at least more than 1k size to just 44 bytes and then restore the orignal? – jps May 09 '21 at 21:50
  • the suggested duplicate target is just one example, there are many similar questions around this topic and it doesn't matter which hashing algorithm or which programming language is involved, the answer is alwasy the same. – jps May 11 '21 at 08:28
  • @jps I think you should post the answer. Although this is practically a duplicate, it's different as written (about SHA256) and will draw different traffic from search engines. The SHA256 people also need to know that hashes aren't encryption. – Vimes May 11 '21 at 19:50
  • @vimes: if has really been answered so many times, e.g. [How to decrypt a SHA-256 encrypted string?](https://stackoverflow.com/q/9316437), but will be asked again, no matter how often it was answered. – jps May 12 '21 at 07:44
  • I see. That wasn't posted as the duplicate link, so I didn't know. – Vimes May 12 '21 at 15:31

0 Answers0