How can I encrypt a file in C# using advapi32?

216 views Asked by At

I need to encrypt a file in C# using the same process demonstrated in the following Microsoft example doc (but I need SHA instead of MD5). https://learn.microsoft.com/en-us/windows/win32/seccrypto/example-c-program-encrypting-a-file

I found this question online, which seems to be exactly what I want... However, I'm curious if there is a native way without having to use DLLImports... Unable to decrypt with CryptEncrypt/CryptDecrypt in C#

I've been looking at several other sources, but can't seem to find a working solution. This errors out saying my key isn't correct...

     byte[] file_contents = File.ReadAllBytes("DescryptedFile.txt");

     string password = "PASSWORD";
     CspParameters cspParams = new CspParameters(1, "Microsoft Strong Cryptographic Provider");
     RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
     rsaProvider.ImportCspBlob(Encoding.ASCII.GetBytes(password));
     byte[] encryptedBytes = rsaProvider.Encrypt(file_contents, false);
0

There are 0 answers