意識到你需要什麼樣的飾有正確的屬性應該工作在以下方法後...你只需要一些自定義代碼時,系列化後,然後反序列化後運行...
// 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);
}
的說明的屬性和方法...
[非序列化()] - 告訴串行到不包括在所述序列化對象這個字段/屬性。
[OnSerializing()] - 告訴串行序列化對象之前,調用此方法。我們的加密代碼在這裏,因爲我們希望密碼的加密值被序列化。
[OnSerialized()] - 告訴串行對象已被串行化後,調用此方法。我們需要在這裏將密碼重置爲未加密狀態。 (而不是保存未加密的密碼,你可以在這裏簡單地解密)
[OnDeserialized()] - 告訴序列化程序在對象被反序列化後調用此方法。這是我們解密的地方,因爲在解密密碼之前,對象還沒有準備好使用。
通過這些屬性和方法,Password屬性將在序列化期間自動加密並在反序列化期間解密。
你可能想看看http://stackoverflow.com/questions/4501289/c-sharp-byte-encryption –
可以用於序列化我的數據對象加密一些字段? –
加密整個批次,然後在反序列化之前將其解密。或者不要使用純文本密碼(不管怎樣) –