Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace ConsoleApp1
  5. {
  6. class Program
  7. {
  8. public static string Encrypt1(int rail, string plainText)
  9. {
  10. List<string> railFence = new List<string>();
  11. for (int i = 0; i < rail; i++)
  12. {
  13. railFence.Add("");
  14. }
  15.  
  16. int number = 0;
  17. int increment = 1;
  18. foreach (char c in plainText)
  19. {
  20. if (number + increment == rail)
  21. {
  22. increment = -1;
  23. }
  24. else if (number + increment == -1)
  25. {
  26. increment = 1;
  27. }
  28. railFence[number] += c;
  29. number += increment;
  30. }
  31.  
  32. string buffer = "";
  33. foreach (string s in railFence)
  34. {
  35. buffer += s;
  36. }
  37. return buffer;
  38. }
  39.  
  40. public static void Encript2b(string key, string text)
  41. {
  42. int keyLength = key.Length;
  43. double textLength = text.Length;
  44.  
  45. int rows = (int)Math.Ceiling(textLength / keyLength);
  46.  
  47. string[] combinedArray = new string[keyLength];
  48. for (int i = 0; i < keyLength; i++)
  49. {
  50. combinedArray[i] = key[i].ToString();
  51. }
  52.  
  53. //Console.WriteLine();
  54. //char[,] array = new char[rows, keyLength];
  55. //int index = 0;
  56. //for (int i=0; i<rows; i++)
  57. //{
  58. // for(int j=0; j< keyLength && index < textLength ; j++)
  59. // {
  60. // if (text[index] != ' ')
  61. // {
  62. // array[i, j] = text[index];
  63. // Console.Write(array[i, j]);
  64. // }
  65. // else j--;
  66. // index++;
  67. // }
  68. // Console.WriteLine();
  69. //}
  70.  
  71. int index = 0;
  72. for (int i=0; i<textLength && index < textLength; i++)
  73. {
  74. if (text[index] != ' ')
  75. {
  76. combinedArray[i % (keyLength)] += text[index];
  77. }
  78. else i--;
  79. index++;
  80. }
  81.  
  82. sortArray(combinedArray);
  83.  
  84. foreach (string s in combinedArray)
  85. {
  86. Console.Write("{0}", s.Substring(1, s.Length-1));
  87. }
  88. }
  89.  
  90. public static string[] sortArray(string[] arrayToSort)
  91. {
  92. int listSize = arrayToSort.Length;
  93. string temp = "";
  94.  
  95. for (int write = 0; write < arrayToSort.Length; write++)
  96. {
  97. for (int sort = 0; sort < arrayToSort.Length - 1; sort++)
  98. {
  99. if (arrayToSort[sort][0] > arrayToSort[sort + 1][0])
  100. {
  101. temp = arrayToSort[sort + 1];
  102. arrayToSort[sort + 1] = arrayToSort[sort];
  103. arrayToSort[sort] = temp;
  104. }
  105. }
  106. }
  107.  
  108. return arrayToSort;
  109. }
  110.  
  111. private static int Mod(int a, int b)
  112. {
  113. return (a % b + b) % b;
  114. }
  115.  
  116. private static string Cipher(string input, string key, bool encipher)
  117. {
  118. for (int i = 0; i < key.Length; ++i)
  119. if (!char.IsLetter(key[i]))
  120. return null; // Error
  121.  
  122. string output = string.Empty;
  123. int nonAlphaCharCount = 0;
  124.  
  125. for (int i = 0; i < input.Length; ++i)
  126. {
  127. if (char.IsLetter(input[i]))
  128. {
  129. bool cIsUpper = char.IsUpper(input[i]);
  130. char offset = cIsUpper ? 'A' : 'a';
  131. int keyIndex = (i - nonAlphaCharCount) % key.Length;
  132. int k = (cIsUpper ? char.ToUpper(key[keyIndex]) : char.ToLower(key[keyIndex])) - offset;
  133. k = encipher ? k : -k;
  134. char ch = (char)((Mod(((input[i] + k) - offset), 26)) + offset);
  135. output += ch;
  136. }
  137. else
  138. {
  139. output += input[i];
  140. ++nonAlphaCharCount;
  141. }
  142. }
  143.  
  144. return output;
  145. }
  146.  
  147. public static string Encipher(string input, string key)
  148. {
  149. return Cipher(input, key, true);
  150. }
  151.  
  152. public static string Decipher(string input, string key)
  153. {
  154. return Cipher(input, key, false);
  155. }
  156.  
  157.  
  158.  
  159. static void Main(string[] args)
  160. {
  161. //string plainText = "HERE IS A SECRET MESSAGE ENCIPHERED BY TRANSPOSITION";
  162. //string key = "CONVENIENCE";
  163. //Encript2b(key, plainText);
  164. //string cipherText = Encrypt(key, plainText);
  165.  
  166.  
  167. //string cipherText = Encipher("cryptography", "break");
  168. //string plainText = Decipher(cipherText, "cipher");
  169.  
  170. //Console.WriteLine("{0}", cipherText);
  171.  
  172. Console.ReadLine();
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement