0

all. I've set up a file in a winforms project which needs an encrypted string, and i'm using the nuget plugin MirzaCryptoHelpers (here), which is a wrapper for common decryption/encryption methods. I'm trying to decrypt a previously encrypted file, however it is returning a NullException upon attempting to convert the decrypted bytes back to a string.

Here's my method:

StreamReader sr = new StreamReader("C:\file.txt")
encryptedstring = sr.ReadLine();
sr.Close() //finished using reader.

//Converting string to bytes.
byte[] encryptedbytes = BitHelpers.ConvertStringToBytes(encryptedstring);
//Decrypting bytes, returns "null" for unknown reason
byte[] unencryptedbytes = new AESCrypto().Decrypt(encryptedbytes, "pass");

//Error is thrown on the below line:
string finalbytes = BitHelpers.ConvertBytesToString(unencryptedbytes);

What's wrong with my method? Or is it the plugin i'm using?

Community
  • 1
  • 1
Larry Tang
  • 602
  • 5
  • 19
  • did you debug the values inside `encryptedstring`, `encryptedbytes`, `unencryptedbytes? check them for null? if so, its more reasonable to look for the errro inside `Bithelpers.ConvertBytesToString` – Patrick Artner Nov 18 '17 at 15:05
  • I've debugged the values, and unencryptedbytes turns up as null also. So, the error's actually in the decryption. – Larry Tang Nov 18 '17 at 15:09
  • `encryptedstring` is filled? – Patrick Artner Nov 18 '17 at 15:11
  • `encryptedstring` is indeed filled, as it converts to `encryptedbytes`, which has 29 values in it's array. – Larry Tang Nov 18 '17 at 15:12
  • So, I found this in the AESCrypto Decrypt function as a comment: `//returns null if decryption failed.` It has something to do with a faulty key or iv. – Larry Tang Nov 18 '17 at 15:15
  • ReadLine() will remove line termination characters. Might be you need them for the decryption to work... – Patrick Artner Nov 18 '17 at 15:17
  • Thanks, will try that. Using "ReadToEnd" instead. – Larry Tang Nov 18 '17 at 15:23
  • My advice to anyone who encounters the same issue with Mirza: Change to csharp-aes. The link can be found [here](https://gitlab.com/czubehead/csharp-aes), and it's much easier to implement, and works properly. – Larry Tang Nov 18 '17 at 16:12

0 Answers0