marius mifsud

marius mifsud

  • NA
  • 9
  • 951

CryptoStream error when loading from XML file

Feb 4 2018 3:09 PM
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:
  1. protected static List Load(string typeName) where T:Person  
  2. {  
  3. byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Where to store these keys is the tricky part,  
  4. // you may need to obfuscate them or get the user to input a password each time  
  5. byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };  
  6. // using (Rijndael rijAlg = Rijndael.Create())  
  7. //{  
  8. // rijAlg.Key = key;  
  9. // rijAlg.IV = iv;  
  10. // ICryptoTransform decryptor = rijAlg.CreateDecryptor(key, iv);  
  11. // }  
  12. //1. Create the result as an empty list  
  13. List result = new List();  
  14. //2. Create the target directory  
  15. string targetDirectory = CreateTargetDirectory(typeName);  
  16. //3. Find all the XML files in the target directory  
  17. string[] xmlFilePaths = Directory.GetFiles(targetDirectory, "*.xml");  
  18. //4. Load each file and deserialize it  
  19. //XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));  
  20. BinaryFormatter bformatter = new BinaryFormatter();  
  21. //added code start >>>  
  22. DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  23. //added code end <<<<<  
  24. foreach (string xmlFilePath in xmlFilePaths)  
  25. {  
  26. //using (FileStream dataStream = new FileStream(xmlFilePath, FileMode.Open))  
  27. // Decryption  
  28. using (var fs = new FileStream(xmlFilePath, FileMode.Open, FileAccess.Read))  
  29. using (var cryptoStream = new CryptoStream(fs, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))  
  30. {  
  31. BinaryFormatter formatter = new BinaryFormatter();  
  32. // This is where you deserialize the class  
  33. T person = (T)formatter.Deserialize(cryptoStream);  
  34. result.Add(person);  
  35. }  
  36. }  
  37. //5. Return the result  
  38. return result;  
  39. }  
The following is the save() method:
  1. protected void Save(string typeName)  
  2. {  
  3. string targetDirectory = CreateTargetDirectory(typeName);  
  4. byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 }; // Where to store these keys is the tricky part,  
  5. // you may need to obfuscate them or get the user to input a password each time  
  6. byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };  
  7. //2. Generate the path to the target file  
  8. string filePath = targetDirectory + ID + ".xml";  
  9. //added code start >>>  
  10. //using (Stream innerStream = File.Create(filePath));  
  11. //added code end <<<<  
  12. //3. Serialise the object  
  13. //4. Save the object to the target file  
  14. //XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());  
  15. //added code Start >>>>>  
  16. BinaryFormatter bformatter = new BinaryFormatter();  
  17. DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  18. //added code end <<<<<<  
  19. //using (Stream cryptoStream = new CryptoStream(innerStream, encryptor, CryptoStreamMode.Write))  
  20. //{  
  21. // 3. write to the cryptoStream  
  22. // bformatter.Serialize(cryptoStream, this);  
  23. //}  
  24. //using (FileStream dataStream = new FileStream(filePath, FileMode.Create))  
  25. //Encryption  
  26. using (var fs= new FileStream(filePath, FileMode.Create, FileAccess.Write))  
  27. using (var cryptoStream = new CryptoStream(fs, des.CreateEncryptor(key, iv), CryptoStreamMode.Write))  
  28. {  
  29. //xmlSerializer.Serialize(dataStream, this);  
  30. //added code start >>>>> https://www.codeproject.com/Articles/1789/Object-Serialization-using-C  
  31. //https://stackoverflow.com/questions/1154198/what-are-the-differences-between-the-xmlserializer-and-binaryformatter/1154429  
  32. //https://www.codeproject.com/Articles/311944/BinaryFormatter-or-Manual-serializing  
  33. //https://stackoverflow.com/questions/48610656/c-sharp-binary-serialization-vs-xmlserializer  
  34. //bformatter.Serialize(dataStream, this);  
  35. //added code end <<<<<  
  36. // dataStream.Close();  
  37. bformatter.Serialize(cryptoStream, this);  
  38. cryptoStream.Close();  
  39. }  
  40. }  
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:
  1. T person = (T)formatter.Deserialize(cryptoStream);