Guest User

Untitled

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