我程序中的以下代码将字节数组序列化,加密并写入磁盘。从它的外观来看,加密工作正常。当程序解密然后反序列化文件时会出现问题。
Visual Studio显示以下错误: System.Runtime.Serialization.dll中发生'System.Runtime.Serialization.SerializationException'
其他信息:反序列化Test.Store类型的对象时出错。根级别的数据无效。
我已经被困在这一天两天了,所以任何帮助都会非常感激!
以下代码已从原始格式中删除:
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
namespace Test
{
public class K
{
#region Non-Public Data Members
private string _n = string.Empty;
private string _u = string.Empty;
private string _p = string.Empty;
private string _u2 = null;
private string _n2 = string.Empty;
public K()
{
}
public string T
{
get { return _n; }
set { _n = value; }
}
public string U
{
get { return _u; }
set { _u = value; }
}
public string P
{
get { return _p; }
set { _p = value; }
}
public string U2
{
get { return _u2; }
set { _u2 = value; }
}
public string N2
{
get { return _n2; }
set { _n2 = value; }
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
//stuff
return sb.ToString();
}
#endregion
}
public class Group
{
private string _text = string.Empty;
private int _imageIndex = 0;
private List<K> _credentials = new List<K>();
public Group()
{
}
public Group(string text, int imageIndex)
{
_text = text;
_imageIndex = imageIndex;
}
public string Text
{
get { return _text; }
set { _text = value; }
}
public int ImageIndex
{
get { return _imageIndex; }
set { _imageIndex = value; }
}
public List<K> Ks
{
get { return _credentials; }
set { _credentials = value; }
}
public override string ToString()
{
return Text;
}
}
public class Store
{
private List<Group> _groups = new List<Group>();
private string _docname = string.Empty;
private bool _dirtyBit = false;
public bool DirtyBit
{
get { return _dirtyBit; }
set { _dirtyBit = value; }
}
public List<Group> Groups
{
get { return _groups; }
set { _groups = value; }
}
public string DocName
{
get { return _docname; }
set { _docname = value; }
}
}
public class Crypto
{
public static string secretKey = "abcdefgh";
[System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")]
public static extern bool ZeroMemory(ref string Destination, int Length);
static string GenerateKey()
{
DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
}
public static byte[] PerformCrypto(ICryptoTransform cryptoTransform, byte[] data)
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
}
}
public static byte[] Decrypt(byte[] cipherTextBytes)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(secretKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(secretKey);
using (var decryptor = DES.CreateDecryptor(DES.Key, DES.IV))
{
return PerformCrypto(decryptor, cipherTextBytes);
}
}
public static byte[] Encrypt(byte[] plainTextBytes)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(secretKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(secretKey);
using (var encryptor = DES.CreateEncryptor(DES.Key, DES.IV))
{
return PerformCrypto(encryptor, plainTextBytes);
}
}
}
public static class StoreMgr
{
private static Store _doc = new Store();
public static void FileSaveAs(string fn)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Store));
serializer.WriteObject(ms, _doc);
byte[] byteArrayInput = ms.GetBuffer();
byte[] encryptedBuffer = Crypto.Encrypt(byteArrayInput);
File.WriteAllBytes(fn, encryptedBuffer);
}
}
public static void OpenFile(string fn)
{
StoreMgr._doc = new Store();
using (MemoryStream ms = new MemoryStream(Crypto.Decrypt(File.ReadAllBytes(fn))))
{
DataContractSerializer deserializer = new DataContractSerializer(typeof(Store));
StoreMgr._doc = (Store)new DataContractSerializer(typeof(Store)).ReadObject(ms);
}
}
}
}
我已经坚持了很长一段时间。非常感谢任何帮助!
答案 0 :(得分:0)
如果您在web.config中配置System.Diagnostics.XmlWriterTraceListener
,可以在*.svclog
答案 1 :(得分:0)
问题出在FileSaveAs方法中。 byte [] byteArrayInput = ms.GetBuffer()返回未使用的字节。将其切换为byte [] byteArrayInput = ms.ToArray()程序运行正常。感谢大家的帮助。