Posted By

kyrathaba on 01/24/11


Tagged

text encrypt obfuscate


Versions (?)

Text Obfuscation - Part II of II


 / Published in: C#
 

  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Windows.Forms;
  6.  
  7. namespace Kyrathasoft.Obfuscate {
  8.  
  9. public static class serTextManip {
  10.  
  11. private enum Operation{Serializing,Deserializing};
  12.  
  13. public static string DeserializeSerText(string filePath){
  14. serText sert;
  15. FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
  16. try {
  17. BinaryFormatter bFormatter = new BinaryFormatter();
  18. sert = (serText)bFormatter.Deserialize(fStream);
  19. filePath = sert.TheText;
  20. if (sert.Shift) {
  21. filePath = shifted(filePath,Operation.Deserializing);
  22. }
  23. if (sert.Reverse) {
  24. filePath = stringReversed(filePath);
  25. }
  26. if ((sert.StartIndex > 0) || (sert.TheLength < sert.TheText.Length)) {
  27. filePath = filePath.Substring(sert.StartIndex, sert.TheLength);
  28. }
  29. } catch (Exception exception) {
  30. MessageBox.Show(exception.Message);
  31. } finally {
  32. if (fStream != null) {
  33. fStream.Close();
  34. }
  35. }
  36.  
  37.  
  38.  
  39. return filePath;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. public static void SerializeSerText(string filePath, serText stObject) {
  48. serText st = stObject;
  49. if(st.Reverse){ st.TheText = stringReversed(st.TheText);}
  50. if (st.Shift) { st.TheText = shifted(st.TheText, Operation.Serializing); }
  51.  
  52. FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
  53. try {
  54. BinaryFormatter bFormatter = new BinaryFormatter();
  55. bFormatter.Serialize(fs, st);
  56. }
  57. catch (Exception exception) {
  58. MessageBox.Show(exception.Message);
  59. }
  60. finally {
  61. if (fs != null) {
  62. fs.Close();
  63. }
  64. }
  65. //end SerializePlainText, next line
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. private static string stringReversed(string inputString) {
  74. if (inputString == null) return null;
  75.  
  76. // this was posted by petebob as well
  77. char[] array = inputString.ToCharArray();
  78. Array.Reverse(array);
  79. string s = string.Empty;
  80. foreach (char c in array) {
  81. s += c;
  82. }
  83. return s;
  84. }
  85.  
  86.  
  87. private static string shifted(string inputString, Operation op) {
  88. string s = string.Empty;
  89. char[] array = inputString.ToCharArray();
  90. int i;
  91. foreach (char c in array) {
  92. if(op == Operation.Serializing){
  93. i = Convert.ToInt32((int)c) + 1;
  94. }else{
  95. i = Convert.ToInt32((int)c)-1;
  96. }
  97. s += (char)i;
  98. }
  99. return s;
  100. }
  101.  
  102.  
  103. //end class next line
  104. }
  105.  
  106. }

Report this snippet  

You need to login to post a comment.