Guest User

Untitled

a guest
Dec 9th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. public class Rot13
  9. {
  10. private String tekst;
  11. public Rot13(String inTekst)
  12. {
  13. this.tekst = inTekst;
  14. this.tekst = this.tekst.ToLower();
  15. }
  16. public void szyfr()
  17. {
  18. char[] tmp = tekst.ToCharArray();
  19. int tmpi;
  20. int tmpa;
  21. for(int i=0;i<this.tekst.Length;i++)
  22. {
  23. if (Convert.ToInt32(tmp[i]) > 96 && Convert.ToInt32(tmp[i]) < 124)
  24. {
  25. tmpi = Convert.ToInt32(tmp[i]);
  26. tmpi = tmpi + 13;
  27. if (tmpi > 122)
  28. {
  29. tmpa = tmpi - 123;
  30. tmpi = 97 + tmpa;
  31. }
  32. tmp[i] =Convert.ToChar(tmpi);
  33. }
  34. }
  35. Console.Out.Write("Zaszyfrowany tekst to ");
  36. for (int i = 0; i < tmp.Length; i++) Console.Out.Write(tmp[i]);
  37. Console.Out.Write("\n");
  38. }
  39. public void deszyfr()
  40. {
  41. char[] tmp = tekst.ToCharArray();
  42. int tmpi;
  43. int tmpa;
  44. for (int i = 0; i < this.tekst.Length; i++)
  45. {
  46. if (Convert.ToInt32(tmp[i]) > 96 && Convert.ToInt32(tmp[i]) < 124)
  47. {
  48. tmpi = Convert.ToInt32(tmp[i]);
  49. tmpi = tmpi - 13;
  50. if (tmpi < 97)
  51. {
  52. tmpa = 97 - tmpi;
  53. tmpi = 123 - tmpa;
  54. }
  55. tmp[i] = Convert.ToChar(tmpi);
  56. }
  57. }
  58. Console.Out.Write("Odszyfrowany tekst to ");
  59. for (int i = 0; i < tmp.Length; i++) Console.Out.Write(tmp[i]);
  60. Console.Out.Write("\n");
  61. }
  62. }
  63. public class Cezar
  64. {
  65. private String tekst;
  66. public Cezar(String inTekst)
  67. {
  68. this.tekst = inTekst;
  69. this.tekst = this.tekst.ToLower();
  70. }
  71. public void szyfr()
  72. {
  73. char[] tmp = tekst.ToCharArray();
  74. int tmpi;
  75. int tmpa;
  76. for (int i = 0; i < this.tekst.Length; i++)
  77. {
  78. if (Convert.ToInt32(tmp[i]) > 96 && Convert.ToInt32(tmp[i]) < 124)
  79. {
  80. tmpi = Convert.ToInt32(tmp[i]);
  81. tmpi = tmpi + 3;
  82. if (tmpi > 122)
  83. {
  84. tmpa = tmpi - 123;
  85. tmpi = 97 + tmpa;
  86. }
  87. tmp[i] = Convert.ToChar(tmpi);
  88. }
  89. }
  90. Console.Out.Write("Zaszyfrowany tekst to ");
  91. for (int i = 0; i < tmp.Length; i++) Console.Out.Write(tmp[i]);
  92. Console.Out.Write("\n");
  93. }
  94. public void deszyfr()
  95. {
  96. char[] tmp = tekst.ToCharArray();
  97. int tmpi;
  98. int tmpa;
  99. for (int i = 0; i < this.tekst.Length; i++)
  100. {
  101. if (Convert.ToInt32(tmp[i]) > 96 && Convert.ToInt32(tmp[i]) < 124)
  102. {
  103. tmpi = Convert.ToInt32(tmp[i]);
  104. tmpi = tmpi - 3;
  105. if (tmpi < 97)
  106. {
  107. tmpa = 97 - tmpi;
  108. tmpi = 123 - tmpa;
  109. }
  110. tmp[i] = Convert.ToChar(tmpi);
  111. }
  112. }
  113. Console.Out.Write("Odszyfrowany tekst to ");
  114. for (int i = 0; i < tmp.Length; i++) Console.Out.Write(tmp[i]);
  115. Console.Out.Write("\n");
  116. }
  117. }
  118.  
  119. class Program
  120. {
  121. static void Main(string[] args)
  122. {
  123. Console.Out.WriteLine("Podaj sciezke pliku");
  124. String patch = Console.In.ReadLine();
  125. StreamReader plik;
  126. try
  127. {
  128. plik = new StreamReader(patch);
  129. }
  130. catch (FileNotFoundException)
  131. {
  132. Console.Out.WriteLine("Nie znaleziono pliku");
  133. Console.In.Read();
  134. return;
  135. }
  136. String tekst = "";
  137. tekst = plik.ReadToEnd();
  138.  
  139. Console.Out.WriteLine("Podaj metode szyfrowania (1. ROT13, 2. Szyfr Cezara");
  140. String c="";
  141. //String c2="";
  142. while (c != "q")
  143. {
  144. c = Console.In.ReadLine();
  145. if (c == "1")
  146. {
  147. Console.Out.WriteLine("Podaj operacje (1. Szyfrowanie, 2. Deszyfrowanie");
  148. c = Console.In.ReadLine();
  149. if (c == "1")
  150. {
  151. new Rot13(tekst).szyfr();
  152.  
  153. }
  154. if (c == "2")
  155. {
  156. new Rot13(tekst).deszyfr();
  157.  
  158. }
  159. }
  160. if (c == "2")
  161. {
  162. Console.Out.WriteLine("Podaj operacje (1. Szyfrowanie, 2. Deszyfrowanie");
  163. c = Console.In.ReadLine();
  164. if (c == "1")
  165. {
  166. new Cezar(tekst).szyfr();
  167.  
  168. }
  169. if (c == "2")
  170. {
  171. new Cezar(tekst).deszyfr();
  172.  
  173. }
  174. }
  175. Console.Out.WriteLine("Wpisz q aby zakonczyc...");
  176. }
  177.  
  178.  
  179.  
  180. }
  181. }
  182. }
Add Comment
Please, Sign In to add comment