Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace Lab2
- {
- class Program
- {
- const int size = 5;
- static void Main(string[] args)
- {
- int m, t;
- int[] key = new int[size], enkey = new int[size];
- string iobuff;
- string[] arrkey = new string[size];
- m = 63; t = 29;
- key = new int[size] {2, 3, 6, 13, 27};
- //input key information
- /*
- Console.Write("Input m: ");
- m = int.Parse(Console.ReadLine());
- Console.Write("Input t: ");
- t = int.Parse(Console.ReadLine());
- Console.Write("Input key: ");
- iobuff = Console.ReadLine();
- arrkey = iobuff.Split(' ');
- for (int i = 0; i < arrkey.Length; i++)
- {
- key[i] = int.Parse(arrkey[i]);
- }*/
- //generate key for encryption
- enkey = genKey(m, t, key);
- Console.Write("Secret key: ");
- foreach (int n in enkey)
- {
- Console.Write("{0} ", n);
- }
- Console.WriteLine();
- //encrypt
- Console.WriteLine("Input string: ");
- iobuff = Console.ReadLine();
- Console.WriteLine("Encrypted string: {0}", genMt(m, t));
- int[] cryp = encrypt(iobuff, enkey);
- //gen decrypt key and decrypt
- enkey = genKey(m, genMt(m, t), enkey);
- foreach (int n in enkey)
- {
- Console.Write("{0} ", n);
- }
- Console.WriteLine();
- //Console.WriteLine("Input string: ");
- //iobuff = Console.ReadLine();
- Console.WriteLine("Encrypted string: {0}", decrypt(cryp, enkey, genMt(m, t), m));
- Console.ReadKey();
- }
- static int[] encrypt(string s, int[] key)
- {
- StringBuilder ctext = new StringBuilder(s);
- int[] crypted = new int[ctext.Length];
- for (int i = 0; i < ctext.Length; i++)
- {
- if (char.IsLetter(ctext[i]))
- {
- int nc = 0;
- bool upper = char.IsUpper(ctext[i]);
- if (upper) ctext[i] = (char)(ctext[i] + 32);
- string bin = numToBin(ctext[i] - 96);
- for (int j = 0; j < bin.Length; j++)
- {
- nc += key[j] * (bin[j] - 48);
- }
- crypted[i] = nc;
- if (upper) ctext[i] = (char)(ctext[i] - 32);
- }
- else if (ctext[i] == ' ') crypted[i] = 0;
- }
- return crypted;
- }
- static string decrypt(int[] crypted, int[] key, int mt, int m)
- {
- StringBuilder text = new StringBuilder(crypted.Length);
- for (int i = 0; i < crypted.Length; i++)
- {
- if (crypted[i] == 0)
- {
- text.Append(' ');
- continue;
- }
- int nc = 0;
- char[] bin = new char[size];
- crypted[i] = (crypted[i]*mt) % m;
- for (int j = key.Length-1; j >= 0; j--)
- {
- if (crypted[i] - key[j] >= 0)
- {
- crypted[i] -= key[j];
- bin[j] = '1';
- }
- else { bin[j] = '0'; }
- }
- for (int k = 0; k < bin.Length; k++)
- nc += (bin[k] - 48) * (int)Math.Pow(2, bin.Length - k -1);
- text.Append((char)(nc % 27 + 96));
- }
- return text.ToString();
- }
- static int[] genKey(int m, int t, int[] key)
- {
- int[] outKey = new int[key.Length];
- for (int i = 0; i < key.Length; i++)
- {
- outKey[i] = (key[i]*t)%m;
- }
- return outKey;
- }
- static int genMt(int m, int t)
- {
- for (int i = 0; i < m; i++)
- {
- if ((t*i)%m == 1)
- {
- return i;
- }
- }
- return -1;
- }
- static string numToBin(int num)
- {
- char[] bin = new char[size];
- int testNumber = 1;
- for (int i = size-1; i >= 0; i--)
- {
- bin[i] += (num & testNumber) > 0 ? '1' : '0';
- testNumber = testNumber << 1;
- }
- return new string(bin);
- }
- /*static string encrypt(string s , int[] key)
- {
- StringBuilder ctext = new StringBuilder(s);
- for (int i = 0; i < ctext.Length; i++)
- {
- if (char.IsLetter(ctext[i]))
- {
- int nc = 0;
- bool upper = char.IsUpper(ctext[i]);
- if (upper) ctext[i] = (char)(ctext[i] + 32);
- string bin = numToBin(ctext[i] - 96);
- for (int j = 0; j < bin.Length; j++)
- {
- nc += key[j] * (bin[j] - 48);
- }
- ctext[i] = (char)(nc % 27 + 96);
- if (upper) ctext[i] = (char)(ctext[i] - 32);
- }
- }
- return ctext.ToString();
- }*/
- /*
- static string decrypt(string s, int[] key, int mt)
- {
- StringBuilder text = new StringBuilder(s);
- for (int i = 0; i < text.Length; i++)
- {
- if (char.IsLetter(text[i]))
- {
- int nc = 0;
- bool upper = char.IsUpper(text[i]);
- if (upper) text[i] = (char)(text[i] + 32);
- string bin = numToBin((text[i] - 96) * mt);
- for (int j = 0; j < bin.Length; j++)
- {
- nc += key[j] * (bin[j] - 48);
- }
- text[i] = (char)(nc % 27 + 96);
- if (upper) text[i] = (char)(text[i] - 32);
- }
- }
- return text.ToString();
- }*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment