Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- public static string Encrypt1(int rail, string plainText)
- {
- List<string> railFence = new List<string>();
- for (int i = 0; i < rail; i++)
- {
- railFence.Add("");
- }
- int number = 0;
- int increment = 1;
- foreach (char c in plainText)
- {
- if (number + increment == rail)
- {
- increment = -1;
- }
- else if (number + increment == -1)
- {
- increment = 1;
- }
- railFence[number] += c;
- number += increment;
- }
- string buffer = "";
- foreach (string s in railFence)
- {
- buffer += s;
- }
- return buffer;
- }
- public static void Encript2b(string key, string text)
- {
- int keyLength = key.Length;
- double textLength = text.Length;
- int rows = (int)Math.Ceiling(textLength / keyLength);
- string[] combinedArray = new string[keyLength];
- for (int i = 0; i < keyLength; i++)
- {
- combinedArray[i] = key[i].ToString();
- }
- //Console.WriteLine();
- //char[,] array = new char[rows, keyLength];
- //int index = 0;
- //for (int i=0; i<rows; i++)
- //{
- // for(int j=0; j< keyLength && index < textLength ; j++)
- // {
- // if (text[index] != ' ')
- // {
- // array[i, j] = text[index];
- // Console.Write(array[i, j]);
- // }
- // else j--;
- // index++;
- // }
- // Console.WriteLine();
- //}
- int index = 0;
- for (int i=0; i<textLength && index < textLength; i++)
- {
- if (text[index] != ' ')
- {
- combinedArray[i % (keyLength)] += text[index];
- }
- else i--;
- index++;
- }
- sortArray(combinedArray);
- foreach (string s in combinedArray)
- {
- Console.Write("{0}", s.Substring(1, s.Length-1));
- }
- }
- public static string[] sortArray(string[] arrayToSort)
- {
- int listSize = arrayToSort.Length;
- string temp = "";
- for (int write = 0; write < arrayToSort.Length; write++)
- {
- for (int sort = 0; sort < arrayToSort.Length - 1; sort++)
- {
- if (arrayToSort[sort][0] > arrayToSort[sort + 1][0])
- {
- temp = arrayToSort[sort + 1];
- arrayToSort[sort + 1] = arrayToSort[sort];
- arrayToSort[sort] = temp;
- }
- }
- }
- return arrayToSort;
- }
- private static int Mod(int a, int b)
- {
- return (a % b + b) % b;
- }
- private static string Cipher(string input, string key, bool encipher)
- {
- for (int i = 0; i < key.Length; ++i)
- if (!char.IsLetter(key[i]))
- return null; // Error
- string output = string.Empty;
- int nonAlphaCharCount = 0;
- for (int i = 0; i < input.Length; ++i)
- {
- if (char.IsLetter(input[i]))
- {
- bool cIsUpper = char.IsUpper(input[i]);
- char offset = cIsUpper ? 'A' : 'a';
- int keyIndex = (i - nonAlphaCharCount) % key.Length;
- int k = (cIsUpper ? char.ToUpper(key[keyIndex]) : char.ToLower(key[keyIndex])) - offset;
- k = encipher ? k : -k;
- char ch = (char)((Mod(((input[i] + k) - offset), 26)) + offset);
- output += ch;
- }
- else
- {
- output += input[i];
- ++nonAlphaCharCount;
- }
- }
- return output;
- }
- public static string Encipher(string input, string key)
- {
- return Cipher(input, key, true);
- }
- public static string Decipher(string input, string key)
- {
- return Cipher(input, key, false);
- }
- static void Main(string[] args)
- {
- //string plainText = "HERE IS A SECRET MESSAGE ENCIPHERED BY TRANSPOSITION";
- //string key = "CONVENIENCE";
- //Encript2b(key, plainText);
- //string cipherText = Encrypt(key, plainText);
- //string cipherText = Encipher("cryptography", "break");
- //string plainText = Decipher(cipherText, "cipher");
- //Console.WriteLine("{0}", cipherText);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement