Commit 0665cf1f by adbaga

yes

parent a82d7a83
......@@ -18,7 +18,6 @@
</e>
</e>
</e>
<e p="RSA.cs" t="Include" />
<e p="RSAhw2.cs" t="Include" />
<e p="Vigenere.cs" t="Include" />
</e>
......
using System;
namespace CryptoAlgo
{
public static class RSA
{
public static string RunRSA()
{
Console.WriteLine("Welcome to RSA");
string command = "";
do
{
Console.WriteLine("E - Encrypt Message");
Console.WriteLine("D - Decrypt Message");
Console.WriteLine("X - Exit");
Console.WriteLine("-----------");
Console.WriteLine(">");
command = Console.ReadLine().ToUpper();
switch (command)
{
case "E":
RunRSAEncrypt();
break;
case "D":
RunRSADecrypt();
break;
}
} while (command != "X");
static void RunRSAEncrypt()
{
Console.WriteLine("Text to encrypt");
Console.Write(">");
var plainText = Console.ReadLine()?.Trim().ToUpper() ?? "";
Console.WriteLine("Enter p (Prime) value:");
Console.Write(">");
string pValue = Console.ReadLine();
Console.WriteLine("Enter q (Prime) value:");
Console.Write(">");
string qValue = Console.ReadLine();
double q;
if (!double.TryParse(qValue, out q))
{
Console.WriteLine("Error: " + qValue + " is not a number");
}
int shiftAmount = 0;
double p;
if (!double.TryParse(pValue, out p))
{
Console.WriteLine("Error: " + pValue + " is not a number");
}
else
{
double n = p * q;
}
}
static void RunRSADecrypt()
{
Console.WriteLine("Text to decrypt");
Console.Write(">");
var encryptedText = Console.ReadLine()?.Trim().ToUpper() ?? "";
Console.WriteLine("Amount to shift");
Console.Write(">");
string shiftAmountText = Console.ReadLine();
int shiftAmount = 0;
if (int.TryParse(shiftAmountText, out shiftAmount))
{
string plainText = RSADecrypt(encryptedText, shiftAmount);
Console.WriteLine("----------------");
Console.WriteLine("original:" + encryptedText);
Console.WriteLine("shift:" + shiftAmount);
Console.WriteLine("----------------");
Console.WriteLine("decrypted:" + plainText);
}
else
{
Console.WriteLine(shiftAmountText + "is not a number");
}
}
return "";
}
private static char[] _alphabet = new char[]
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z', ' '
};
static string RSAEncrypt(string plainText, int shiftAmount)
{
Console.WriteLine("Alphabet size: " + _alphabet.Length);
string result = "";
for (int i = 0; i < plainText.Length; i++)
{
var letter = plainText[i];
var indexInAlphabet = Array.IndexOf(_alphabet, letter);
indexInAlphabet = (indexInAlphabet + shiftAmount) % _alphabet.Length;
result = result + _alphabet[indexInAlphabet];
}
return result;
}
static string RSADecrypt(string encryptedText, int shiftAmount)
{
Console.WriteLine("Alphabet size: " + _alphabet.Length);
string result = "";
for (int i = 0; i < encryptedText.Length; i++)
{
var letter = encryptedText[i];
var indexInAlphabet = Array.IndexOf(_alphabet, letter);
indexInAlphabet = (indexInAlphabet - shiftAmount) % _alphabet.Length;
result = result + _alphabet[indexInAlphabet];
}
return result;
}
}
}
/*string encryptedText = RSAEncrypt(plainText, shiftAmount);
Console.WriteLine("----------------");
Console.WriteLine("original:" + plainText);
Console.WriteLine("shift:" + shiftAmount);
Console.WriteLine("----------------");
Console.WriteLine("encrypted:" + encryptedText);
*/
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment