how to encode string in serialization
-
hey everybody! I'm serializing some strings in an application, but one of the string is a password. the thing is that when I go to the file that I'm serializing to, I can see with notepad the password clearly. is there anyway to encode and decode this string before serializing it ?
-
hey everybody! I'm serializing some strings in an application, but one of the string is a password. the thing is that when I go to the file that I'm serializing to, I can see with notepad the password clearly. is there anyway to encode and decode this string before serializing it ?
Well here it is a function that i use to encode strings in XML files:
/// /// Convert To Base64 /// /// clean text /// 1(Unicode) 2(ASCII) 3(UTF7) 4(UTF8) /// Base64 string public static string ToB64(string text, Int16 type) { string rez = null; switch (type) { case 1: //Unicode rez = Convert.ToBase64String(Encoding.Unicode.GetBytes(text)); break; case 2: //ASCII rez = Convert.ToBase64String(Encoding.ASCII.GetBytes(text)); break; ase 3: //UTF7 rez = Convert.ToBase64String(Encoding.UTF7.GetBytes(text)); break; case 4: //UTF8 rez = Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); break; } eturn rez; }
and to decode:/// /// Convert From Base64 /// /// encrypted text /// 1(Unicode) 2(ASCII) 3(UTF7) 4(UTF8) /// clean text public static string FromB64(string text, Int16 type) { string rez = null; switch (type) { case 1: //Unicode rez = Encoding.Unicode.GetString(Convert.FromBase64String(text)); break; case 2: //ASCII rez = Encoding.ASCII.GetString(Convert.FromBase64String(text)); break; case 3: //UTF7 rez = Encoding.UTF7.GetString(Convert.FromBase64String(text)); break; case 4: //UTF8 rez = Encoding.UTF8.GetString(Convert.FromBase64String(text)); break; } return rez; }
-
hey everybody! I'm serializing some strings in an application, but one of the string is a password. the thing is that when I go to the file that I'm serializing to, I can see with notepad the password clearly. is there anyway to encode and decode this string before serializing it ?
I'm assuming you want to serialize your class with the XmlSerializer. The simplest trick is to mark your cleartext Password field/property with XMLIgnore and add another property providing an encrypted password.
using System;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Security.Cryptography;[Serializable]
public class UserData
{
[XmlIgnore]
public string Password;\[XmlElement("Password", DataType = "base64Binary")\] \[EditorBrowsable(EditorBrowsableState.Never)\] public byte\[\] EncryptedPassword { get { // use the classes in the System.Cryptography // namespace to return the encrypted password return YourEncryptMethod(Password); } set { // use the classes in the System.Cryptography // namespace to decrypt the encrypted password Password = YourDecryptMethod(value); } }
}
Another possibility is to implement the IXmlSerializable interface and serialize your class manually. HTH, Mark