· 5 years ago · Jul 12, 2020, 07:24 PM
1using System;
2
3namespace CSHARP_app
4{
5 public class cipher
6 {
7 static char xorKey;
8 public static String encryptDecrypt(String inputString)
9 {
10 char[] keys = {'K', 'P'};
11 Random rand = new Random();
12 int index = rand.Next(keys.Length);
13 xorKey = keys[index];
14 String outputString = "";
15 int len = inputString.Length;
16 for (int i = 0; i < len; i++)
17 {
18 outputString = outputString + char.ToString((char) (inputString[i] ^ xorKey));
19 }
20 Console.WriteLine(outputString);
21 return outputString;
22 }
23 public void XOR()
24 {
25 Console.WriteLine("enter string to encrypt");
26 String sampleString = Console.ReadLine();
27 Console.WriteLine("Encrypted String:");
28 String encryptedString = encryptDecrypt(sampleString);
29 Console.WriteLine("----------\nDecrypted String:");
30 encryptDecrypt(encryptedString);
31 Console.WriteLine("----------\nKey used:\n{0}", xorKey);
32 }
33 public static string XORCipher(string data, string key)
34 {
35 int dataLen = data.Length;
36 int keyLen = key.Length;
37 char[] output = new char[dataLen];
38 for (int i = 0; i < dataLen; ++i)
39 {
40 output[i] = (char)(data[i] ^ key[i % keyLen]);
41 }
42 return new string(output);
43 }
44 public void XOR2()
45 {
46 Console.WriteLine("enter data");
47 string data = Console.ReadLine();
48 XORCipher(data, "secretkey");
49 }
50 }
51}