Question: this is a c# code use to encrypt plain text. explain the basic steps of this code and how this code works(basically every detail on
this is a c# code use to encrypt plain text. explain the basic steps of this code and how this code works(basically every detail on the code).
using System;
using System.Text;
class Cipher
{
private static int key = 20;
private static int a = 20;
private static int b = 20;
public string generateKey(string str, string key)
{
int x = str.Length;
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.Length == str.Length)
break;
key+=(key[i]);
}
return key;
}
public string encryptWithVegenere(string str, string key)
{
string cipher_text=\"\";
for (int i = 0; i
{
int x = (str[i] + key[i]) %26;
x += 'A';
cipher_text+=(char)(x);
}
return cipher_text;
}
public StringBuilder encryptWithCaesar(string text)
{
StringBuilder result= new StringBuilder();
for (int i=0; i;>
{
if (char.IsUpper(text[i]))
{
char ch = (char)(((int)text[i] + key - 65) % 26 + 65);
result.Append(ch);
}
else
{
char ch = (char)(((int)text[i] + key - 97) % 26 + 97);
result.Append(ch);
}
}
return result;
}
public string encryptWithAffine(char[] msg)
{
string cipher = \"\";
for (int i = 0; i
{
mod m
{here x is msg[i] and m is 26}
and added 'A' to bring it in range of ascii alphabet[ 65-90 | A-Z ]
if (msg[i] != ' ')
{
cipher = cipher + (char) ((((a * (msg[i] - 'A')) + b) % 26) + 'A');
}
else
{
cipher += msg[i];
}
}
return cipher;
}
}
public class Program
{
public static void Main(string []args)
{
Console.WriteLine(\"YOU CN NOW ENCRYPT OR DECRYPTYOUR MESSAGE\");
Console.WriteLine(\"Caesar cipher (1),\");
Console.WriteLine(\"Affine cipher (2),\");
Console.WriteLine(\"Vigenre cipher (3),\");
Console.Write(\"ENTER YOUR PREFERED CHOICE \");
int choice = Convert.ToInt32(Console.ReadLine());
Console.Write(\" INSERT TEXT: \");
string plainText = Console.ReadLine();
Cipher c = new Cipher();
if(choice == 1)
{
Console.WriteLine(\"CIPHER TEXT IS:\");
Console.WriteLine(c.encryptWithCaesar(plainText));
}
else if(choice == 2)
{
Console.WriteLine(\"AFFINE TEXT IS:\");
Console.WriteLine(c.encryptWithAffine(plainText.ToCharArray()));
}
else if(choice == 3)
{
string keyword = \"AMIT\";
string key = c.generateKey(plainText, keyword);
Console.WriteLine(\"PLAIN TEXT IS:\");
Console.WriteLine(c.encryptWithVegenere(plainText, key));
}
else
{
Console.WriteLine(\"Invalid Choice!\");
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
