Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Dear All,

I have an encrypted file and I'm trying to decrypt it by using FileStream C#.

I'm passing the file path to create file-stream and when I'm trying to fetch the data from the FileStream, the data is null.

SampleCode
FileStream fs = new FileStream(@"" + offlinePath, FileMode.Create);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd(); "color: rgba(255, 0, 0, 1)">//Getting empty
                        WriteToFile("Data: " + data);
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("Offline result file DECRYPTED"); //log file
                    }


What I have tried:

public string UploadOfflineResult()
        {
            var pathSouce = Directory.GetParent(Directory.GetParent(Environment.SystemDirectory.ToString()).ToString()).FullName;
            var offlinePath = pathSouce +  ConfigurationManager.AppSettings["resultFile"];
            WriteToFile(offlinePath);
            FileInfo _file = new FileInfo(offlinePath);
            WriteToFile(_file.Exists ? "Offline result file exist" : "Offline result file does not exist");


            if (_file.Exists && getPing())
            {
                WriteToFile("Working ON offline exam result file");
                OfflineExamResult offlineExamResult = null;
                try
                {
                    FileStream fs = new FileStream(@"" + offlinePath, FileMode.Create);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd(); //Getting empty
                        WriteToFile("Data: " + data);
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("Offline result file DECRYPTED");
                    }
                }
                catch (Exception ex)
                {
                    ex = ex.InnerException; //Avoid warning
                    return JsonConvert.SerializeObject(
                        new { success = false, message = "Invalid result file content" },
                        Formatting.None,
                        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
                }


                CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService(JsonConvert.SerializeObject(offlineExamResult, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
                WriteToFile("UpdatedOfflineCandidateExamResult: CandidateSessionID=" + offlineExamResult.CandidateSessionID + " ");
                return true.ToString();


            }
            return false.ToString();
        }


Can anyone please help me.

Thanks
Posted
Updated 23-May-19 4:47am
v2
Comments
phil.o 23-May-19 5:48am    
Your forgot to describe the problem. Any error message? or is it that it behaves differently from what you expected?
Richard MacCutchan 23-May-19 8:19am    
Your code creates a new empty file. There is nothing in it to read.

When creating your FileStream you should be using FileMode.Open and not FileMode.Create, i.e.
C#
FileStream fs = new FileStream(@"" + offlinePath, FileMode.Open);
Using FileMode.Create will create a new file (overwriting the file if it already exists) while FileMode.Open is used to open an existing file.

Alternatively, you could pass the file name as a parameter when creating the StreamReader and not create a file stream at all.
C#
using (StreamReader sr = new StreamReader(@"" + offlinePath))
 
Share this answer
 
If data comes up with no data, there was nothing in the file to read.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900