Regarding the case where Deserialize fails without the serialize throw of XmlSerializer.
This is a problem related to Serialize and Deserialize of XmlSerializer.
For a certain XML file, save it with [Code Flagment 1] and save it.
I am creating a program to read with [Code Flagment 2].
[Code Flagment1] is completed without entering the catch area,
When reading with [Code Flagment 2], an error of [Stack Trace] is output and There was a problem that it could not be read.
If you’re having trouble with Serialize in [Code Flagment 1],
UnknownAttribute, UnknownElement, UnknownNode, UnreferencedObject
I thought that one of the above would occur and pass the catch of [Code Flagment 1],
But, Did not pass.
This issue is not a constant issue and is very rare.
This program uses the .Net Framework 4.6.
What is the suspected cause of this problem?
I would appreciate it if you could give me some advice.
best regards.
[Code Flagment1]===============================================
try
{
serializer = new XmlSerializer("key");
using (SymmetricAlgorithm sa = new RijndaelManaged())
{
using (ICryptoTransform encryptor = sa.CreateEncryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
{
lock (lockObject)
{
using (FileStream msEncrypt = new FileStream(<< XML File Path >>, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
serializer.Serialize(csEncrypt, settingObject);
}
}
}
}
}
}
catch (Exception e)
{
// Not Into...
throw e;
}
[code flagment2]=================================================
try
{
serializer = new XmlSerializer(objectType);
using (SymmetricAlgorithm sa = new RijndaelManaged())
{
using (ICryptoTransform decryptor = sa.CreateDecryptor(<< KeyFile's Byte[] >>, << IVFile's Byte[] >>))
{
using (FileStream msDecrypt = new FileStream(<< XML File Path >>, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite))
{
if (msDecrypt.Length > 0)
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
settingObj = serializer.Deserialize(csDecrypt);
}
}
}
}
}
}
catch (System.Security.Cryptography.CryptographicException e2)
{
// Into here.
//
}
[ Stack Trace ] ======================================================
2021-02-09 09:42:41.67 SettingObject SEOJ_GSOB_0001 System.Security.Cryptography.CryptographicException パディングは無効なので、削除できません。
場所 System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
場所 System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
場所 System.Security.Cryptography.CryptoStream.FlushFinalBlock()
場所 System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
場所 System.IO.Stream.Close()
場所 System.IO.Stream.Dispose()
場所 {ユーザ作成クラス}.Settings.SettingObject.GetSettingObject(String settingFilePath, Type objectType)