Return to Snippet

Revision: 39985
at January 24, 2011 12:53 by kyrathaba


Initial Code
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace Kyrathasoft.Obfuscate {

    public static class serTextManip {

        private enum Operation{Serializing,Deserializing};

        public static string DeserializeSerText(string filePath){
            serText sert;
            FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
            try {
                BinaryFormatter bFormatter = new BinaryFormatter();
                sert = (serText)bFormatter.Deserialize(fStream);
                filePath = sert.TheText;
                if (sert.Shift) {
                    filePath = shifted(filePath,Operation.Deserializing);
                }
                if (sert.Reverse) {
                    filePath = stringReversed(filePath);
                }
                if ((sert.StartIndex > 0) || (sert.TheLength < sert.TheText.Length)) {
                    filePath = filePath.Substring(sert.StartIndex, sert.TheLength);
                }
            } catch (Exception exception) {
                MessageBox.Show(exception.Message);                    
            } finally {
                if (fStream != null) {
                    fStream.Close();
                }
            }


           
            return filePath;
        }






        public static void SerializeSerText(string filePath, serText stObject) {            
            serText st = stObject;
            if(st.Reverse){ st.TheText = stringReversed(st.TheText);}
            if (st.Shift) { st.TheText = shifted(st.TheText, Operation.Serializing); }
            
            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            try {
                BinaryFormatter bFormatter = new BinaryFormatter();
                bFormatter.Serialize(fs, st);
            }
            catch (Exception exception) {
                MessageBox.Show(exception.Message);
            }
            finally {
                if (fs != null) {
                    fs.Close();
                }
            }
            //end SerializePlainText, next line
            
        }
            




        private static string stringReversed(string inputString) {
            if (inputString == null) return null;

            // this was posted by petebob as well 
            char[] array = inputString.ToCharArray();
            Array.Reverse(array);
            string s = string.Empty;
            foreach (char c in array) {
                s += c;
            }
            return s;
        }


        private static string shifted(string inputString, Operation op) {
            string s = string.Empty;
            char[] array = inputString.ToCharArray();
            int i;
            foreach (char c in array) {
                if(op == Operation.Serializing){
                    i = Convert.ToInt32((int)c) + 1;
                }else{
                    i = Convert.ToInt32((int)c)-1;
                }
                s += (char)i;
            }
            return s;
        }


        //end class next line
    }

}

Initial URL

                                

Initial Description

                                

Initial Title
Text Obfuscation - Part II of II

Initial Tags
text

Initial Language
C#