1 回答
TA贡献1155条经验 获得超0个赞
是的,在这里使用绝对是您的朋友!:)
为什么要将对象序列化到一个文件中,只是为了在之后将其加密到另一个文件中?
我已经清理了您的代码,这应该可以按预期工作:
public void SerializeUserConfig(string fileName)
{
try
{
Encrypt(userconfigstorage, Path.Combine(perfilAcesso.GetUserConfigPath(), fileName), "syndra15OP");
MessageBox.Show("Dados salvos com sucesso!");
}
catch (Exception exception)
{
errorlog.SetError(exception.ToString());
SerializeError(perfilAcesso.GetUserErrorLogPath());
MessageBox.Show("Houve um erro ao salvar as configurações!\nPor favor, contate o desenvolvedor.\n\nEID: 002");
}
}
public UserConfigStorage DeserializeUserConfig(string fileName)
{
return Decrypt(Path.Combine(perfilAcesso.GetUserConfigPath(), fileName), "syndra15OP");
}
public void Encrypt(UserConfigStorage input, string filePath, string strHash)
{
using (TripleDESCryptoServiceProvider tdc = new TripleDESCryptoServiceProvider())
{
using (FileStream outStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
{
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
tdc.Key = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
md5.Clear();
}
tdc.Mode = CipherMode.ECB;
using (CryptoStream cryStream = new CryptoStream(outStream, tdc.CreateEncryptor(), CryptoStreamMode.Write))
{
BinaryFormatter binForm = new BinaryFormatter();
binForm.Serialize(cryStream, input);
}
}
}
}
public UserConfigStorage Decrypt(string filePath, string strHash)
{
UserConfigStorage output;
using (TripleDESCryptoServiceProvider tdc = new TripleDESCryptoServiceProvider())
{
using (FileStream outStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
tdc.Key = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
md5.Clear();
}
tdc.Mode = CipherMode.ECB;
using (CryptoStream cryStream = new CryptoStream(outStream, tdc.CreateDecryptor(), CryptoStreamMode.Read))
{
BinaryFormatter binForm = new BinaryFormatter();
output = binForm.Deserialize(cryStream) as UserConfigStorage;
}
}
}
return output;
}
问候
- 1 回答
- 0 关注
- 172 浏览
添加回答
举报