我正在为办公室开发插件。在我的应用程序中,我需要存储用户提供的一些数据。
现在这些数据将在办公室程序(word,excel,power-point)之间共享,所以我需要将它们存储在一些文件中,我可以在任何办公程序下从我的应用程序访问。
我的问题是我如何序列化我的数据对象(我得到的例子),但我想要加密密码等数据。我试图将数据对象存储为二进制格式,但我仍然可以读取数据(只需在记事本++中打开它)。任何想法?
更新
我不需要知道如何加密数据,我想序列化我的数据对象,同时加密重要或秘密数据(加密序列化)
答案 0 :(得分:0)
答:如果你想要懒惰,只需GZip它 - 它会压缩数据,使你只能通过首先取消GZiping来读取它。
B:Encrypt数据。这需要您有密码才能阅读。这也很难实现。
请随时询问有关如何使用这些选项的说明。
答案 1 :(得分:0)
在意识到你需要的东西之后,使用正确属性修饰的下列方法应该有效...你只需要在序列化期间和之后运行一些自定义代码,然后在反序列化之后...
// Save your password so you can reset it after the object has been serialized.
[NonSerialized()]
private string SavedPassword;
// This saves the value of Password and Encrpts it so it will be stored Encrypted.
// I am leaving out the Encrypt code to make it cleaner here.
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
SavedPassword = Password;
Password = Encrypt(Password);
}
// reset the Password after serialization so you can continue to use your object.
[OnSerialized()]
internal void OnSerializedMethod(StreamingContext context)
{
Password = SavedPassword;
}
// On deserialize you need to Decrypt your Password.
[OnDeserialized()]
internal void OnDeserializedMethod(StreamingContext context)
{
Password = Decrypt(Password);
}
属性和方法的说明......
[NonSerialized()] - 告诉序列化程序不要在序列化对象中包含此字段/属性。
[OnSerializing()] - 告诉序列化程序在序列化对象之前调用此方法。我们的加密代码在这里,因为我们希望序列化密码的加密值。
[OnSerialized()] - 告诉序列化程序在序列化对象后调用此方法。我们需要在此处将密码重置为未加密状态。 (而不是保存未加密的密码,你可以在这里轻松解密)
[OnDeserialized()] - 告诉序列化程序在反序列化对象后调用此方法。这是我们解密的地方,因为在我们解密密码之前,对象还没有准备好使用。
使用这些属性和方法,Password属性将在序列化期间自动加密,并在反序列化期间解密。