using System;
using System.Collections.Generic;
using System.Text;

namespace isn
{
    public interface IDataProtector
    {
        string Protect(string data);
        string UnProtect(string data);
    }

    public class DefaultDataProtector : IDataProtector
    {
        private byte delta = 145;

        public DefaultDataProtector()
        {

        }
        
        public string Protect(string data)
        {
            List<Byte> protd = new List<byte>();
            StringBuilder sb = new StringBuilder();
            foreach (byte c in Encoding.UTF8.GetBytes(data))
            {
                 protd.Add((byte) (c ^ delta));
            }
            return System.Convert.ToBase64String(protd.ToArray());
        }

        public string UnProtect(string data)
        {
            if (data==null) return null;
            StringBuilder sb = new StringBuilder();
            List<byte> unps = new List<byte>();

            foreach (byte c in System.Convert.FromBase64CharArray(data.ToCharArray(),0,data.Length))
            {
                unps.Add((byte) (c ^ delta));
            }
            return Encoding.UTF8.GetString(unps.ToArray());
        }
    }

}