Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to encode string in serialization

how to encode string in serialization

Scheduled Pinned Locked Moved C#
jsontutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Green Fuze
    wrote on last edited by
    #1

    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 ?

    S S 2 Replies Last reply
    0
    • G Green Fuze

      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 ?

      S Offline
      S Offline
      Stefan Prodan
      wrote on last edited by
      #2

      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; }

      1 Reply Last reply
      0
      • G Green Fuze

        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 ?

        S Offline
        S Offline
        StealthyMark
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups