I am encrypting my data before saving it into the XML file but I am being given the following error when trying to read (load()) from files :
System.Security.Cryptography.CryptographicException: 'Bad Data.'
The data is written (save()) correctly and is encypted.
The following is the save() method:
- protected static List Load(string typeName) where T:Person
- {
- byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Where to store these keys is the tricky part,
- // you may need to obfuscate them or get the user to input a password each time
- byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
- // using (Rijndael rijAlg = Rijndael.Create())
- //{
- // rijAlg.Key = key;
- // rijAlg.IV = iv;
- // ICryptoTransform decryptor = rijAlg.CreateDecryptor(key, iv);
- // }
- //1. Create the result as an empty list
- List result = new List();
- //2. Create the target directory
- string targetDirectory = CreateTargetDirectory(typeName);
- //3. Find all the XML files in the target directory
- string[] xmlFilePaths = Directory.GetFiles(targetDirectory, "*.xml");
- //4. Load each file and deserialize it
- //XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
- BinaryFormatter bformatter = new BinaryFormatter();
- //added code start >>>
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- //added code end <<<<<
- foreach (string xmlFilePath in xmlFilePaths)
- {
- //using (FileStream dataStream = new FileStream(xmlFilePath, FileMode.Open))
- // Decryption
- using (var fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))
- using (var cryptoStream = new CryptoStream(fs, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
- {
- BinaryFormatter formatter = new BinaryFormatter();
- // This is where you deserialize the class
- T person = (T)formatter.Deserialize(cryptoStream);
- result.Add(person);
- }
- }
- //5. Return the result
- return result;
- }
The following is the save() method:
- protected void Save(string typeName)
- {
- string targetDirectory = CreateTargetDirectory(typeName);
- byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Where to store these keys is the tricky part,
- // you may need to obfuscate them or get the user to input a password each time
- byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
- //2. Generate the path to the target file
- string filePath = targetDirectory + ID + ".xml";
- //added code start >>>
- //using (Stream innerStream = File.Create(filePath));
- //added code end <<<<
- //3. Serialise the object
- //4. Save the object to the target file
- //XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
- //added code Start >>>>>
- BinaryFormatter bformatter = new BinaryFormatter();
- DESCryptoServiceProvider des = new DESCryptoServiceProvider();
- //added code end <<<<<<
- //using (Stream cryptoStream = new CryptoStream(innerStream, encryptor, CryptoStreamMode.Write))
- //{
- // 3. write to the cryptoStream
- // bformatter.Serialize(cryptoStream, this);
- //}
- //using (FileStream dataStream = new FileStream(filePath, FileMode.Create))
- //Encryption
- using (var fs= new FileStream(filePath, FileMode.Create, FileAccess.Write))
- using (var cryptoStream = new CryptoStream(fs, des.CreateEncryptor(key, iv), CryptoStreamMode.Write))
- {
- //xmlSerializer.Serialize(dataStream, this);
- //added code start >>>>> https://www.codeproject.com/Articles/1789/Object-Serialization-using-C
- //https://stackoverflow.com/questions/1154198/what-are-the-differences-between-the-xmlserializer-and-binaryformatter/1154429
- //https://www.codeproject.com/Articles/311944/BinaryFormatter-or-Manual-serializing
- //https://stackoverflow.com/questions/48610656/c-sharp-binary-serialization-vs-xmlserializer
- //bformatter.Serialize(dataStream, this);
- //added code end <<<<<
- // dataStream.Close();
- bformatter.Serialize(cryptoStream, this);
- cryptoStream.Close();
- }
- }
Can anyone help please, as I am new in C Sharp. The error is being pointed to the line of code at the end of the load method (line 44), which is:
- T person = (T)formatter.Deserialize(cryptoStream);
Replies
Know the answer? Post it — somebody with the same question will find it here.