0

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);
Rick
  • 421
  • 3
  • 15
  • advapi32 is most likely an unmanaged DLL, so the `import` statements are unavoidable. – Robert Harvey Jul 29 '21 at 14:22
  • The managed c# methods are often just wrapper that call windows dlls. What often happens is the c# doesn't include all the options for the windows dlls and c# you cannot override the parameters like in c++. If you are using a encryption mode (and options) that are supported in c# you do not need the windows dlls. If you are using an encrytion mode not supported in c# than you have to use the windows dlls. – jdweng Jul 29 '21 at 14:41

0 Answers0