Commit 8237fd9c by adbaga

yes

parent 25516cf8
......@@ -18,6 +18,8 @@
</e>
</e>
</e>
<e p="RSA.cs" t="Include" />
<e p="RSAhw2.cs" t="Include" />
<e p="Vigenere.cs" t="Include" />
</e>
<e p="MenuSystem" t="IncludeRecursive">
......
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
using System;
using System.Threading;
namespace CryptoAlgo
{
public class RSAhw2
{
//static void Main(string[] args)
public static string runRSA()
{
Console.WriteLine("RSA");
var g = new Random();
long p;
do
{
p = g.Next(512, 10000);
} while (!PrimeChecker(p));
var h = new Random();
int q;
do
{
q = h.Next(512, 10000);
} while (!PrimeChecker(q));
Console.WriteLine($"P value: {p}");
Console.WriteLine($"Q value: {q}");
long n = p * q;
long m = (p - 1) * (q - 1);
Console.WriteLine($"n = p*q: {n}");
Console.WriteLine($"ф(n) = (p-1)*(q-1): {m}");
long e = 1;
long gcd = 0;
do
{
e++;
gcd = Gcd(m, e);
} while (gcd != 1);
Console.WriteLine($"Co-prime to the ф(n): {m} is e:{e} ");
int k = 0;
do
{
if ((1 + k * m) % e == 0) break;
k++;
} while (true);
Console.WriteLine($"K: {k}");
var d = (1 + k * m) / e;
Console.WriteLine($"d: {d}");
Console.WriteLine($"Public key \n n: {n} e: {e}");
Console.WriteLine($"Private key: p*q = {p}*{q} = {n} d:{d}");
Console.WriteLine("Plaintext char:");
var plainTextString = Console.ReadLine();
if (plainTextString.Length >= 1)
{
var table = new Int64[4, plainTextString.Length];
var i = 0;
while (i < plainTextString.Length)
{
var plainText = (int) plainTextString[i];
table[0, i] = plainText;
var cypher = calcMod(plainText, e, n);
table[1, i] = cypher;
table[2, i] = calcMod(cypher, d, n);
i++;
}
Console.Write("Char code: ");
for (var j = 0; j < plainTextString.Length; j++)
{
Console.Write($" {table[0,j]}");
}
Console.Write("\nCypher: ");
for (var j = 0; j < plainTextString.Length; j++)
{
Console.Write($" {table[1, j]}");
}
Console.Write("\nDecrypted: ");
for (var j = 0; j < plainTextString.Length; j++)
{
Console.Write($" {table[2,j]}");
}
Console.WriteLine();
}
CheckRSA(p*q,e);
static void CheckRSA(long n, long e)
{
long x = 1;
long y = 1;
do
{
x++;
y = Divide(n,x);
} while (!PrimeChecker(x) || !PrimeChecker(y) || y==1);
Console.WriteLine("2 Private keys are: X: {0}, Y: {1}", x,y);
}
static int calculateMod(int inputBase, int inputSecret, int inputModulus)
{
var returnMod = 1;
for (var i = 1; i <= inputSecret; ++i)
{
returnMod = (returnMod * inputBase) % inputModulus;
}
return returnMod;
}
return "";
}
static bool PrimeChecker(long x)
{
for (var i = 2; i * i < x; ++i)
{
if (x % i == 0)
{
return false;
}
}
return true;
}
static long Divide(long n, long x)
{
if (n % x == 0)
return n / x;
return 1;
}
static long Gcd(long a, long b)
{
//Case Divide 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return b;
// a is greater
if (a > b)
return Gcd(a-b, b);
return Gcd(a, b-a);
}
static long calcMod(long inputBase, long inputSecret, long inputModulus)
{
long returnMod = 1;
for (var i = 1; i <= inputSecret; ++i)
{
returnMod = (returnMod * inputBase) % inputModulus;
}
return returnMod;
}
}
}
\ No newline at end of file
......@@ -35,7 +35,16 @@ namespace pleaseWork
Command = "3",
Title = "Diffie-Hellman Key Exchange",
CommandToExecute = DH.RunDH
},
new MenuItem()
{
Command = "4",
Title = "RSA",
CommandToExecute = RSAhw2.runRSA
}
}
};
......
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