Commit a62b0c6f by adbaga

input validation check for dh

parent 0665cf1f
......@@ -9,12 +9,62 @@ namespace CryptoAlgo
public static string RunDH()
{
Console.WriteLine("Enter the generator");
long generator = Convert.ToInt64(Console.ReadLine()?.Trim());
string generatorInput = Console.ReadLine();
int generator = 0;
if (int.TryParse(generatorInput, out generator))
{
generator = (int) Convert.ToInt64(generatorInput);
}
else
{
Console.WriteLine(generatorInput + "is not a number");
}
Console.WriteLine("Enter Anna's' secret");
long annaSecret = Convert.ToInt64(Console.ReadLine()?.Trim());
string annaInput = Console.ReadLine();
int annaSecret = 0;
if (int.TryParse(annaInput, out annaSecret))
{
annaSecret = (int) Convert.ToInt64(annaInput);
}
else
{
Console.WriteLine(annaInput + "is not a number");
}
Console.WriteLine("Enter Boris's secret");
long borisSecret = Convert.ToInt64(Console.ReadLine()?.Trim());
string borisInput = Console.ReadLine();
int borisSecret = 0;
if (int.TryParse(borisInput, out borisSecret))
{
borisSecret = (int) Convert.ToInt64(borisInput);
}
else
{
Console.WriteLine("borrisInput + is not a number");
}
long mod = 601457;
long Anna = diff_hell(generator, annaSecret, mod);
......@@ -34,6 +84,8 @@ namespace CryptoAlgo
}
public static long diff_hell(long a, long b, long c)
{
......
......@@ -5,9 +5,7 @@ namespace CryptoAlgo
{
public class RSAhw2
{
//static void Main(string[] args)
public static string runRSA()
{
......@@ -18,14 +16,14 @@ namespace CryptoAlgo
do
{
p = g.Next(100, 500);
p = g.Next(100, 700);
} while (!PrimeChecker(p));
var h = new Random();
int q;
long q;
do
{
q = h.Next(100, 500);
q = h.Next(100, 700);
} while (!PrimeChecker(q));
Console.WriteLine($"P value: {p}");
......@@ -49,7 +47,7 @@ namespace CryptoAlgo
Console.WriteLine($"Co-prime to the ф(n): {m} is e:{e} ");
int k = 0;
long k = 0;
do
{
if ((1 + k * m) % e == 0) break;
......@@ -64,16 +62,16 @@ namespace CryptoAlgo
Console.WriteLine($"Private key: p*q = {p}*{q} ={n} d:{d}");
Console.WriteLine("Plaintext char:");
var plainTextString = Console.ReadLine();
var plainTextStr = Console.ReadLine();
if (plainTextString.Length >= 1)
if (plainTextStr.Length >= 1)
{
var table = new Int64[4, plainTextString.Length];
var table = new Int64[4, plainTextStr.Length];
var i = 0;
while (i < plainTextString.Length)
while (i < plainTextStr.Length)
{
var plainText = (long) plainTextString[i];
var plainText = (long) plainTextStr[i];
table[0, i] = plainText;
var encrypted = calcMod(plainText, e, n);
......@@ -85,19 +83,19 @@ namespace CryptoAlgo
Console.Write("Char code: ");
for (var j = 0; j < plainTextString.Length; j++)
for (var j = 0; j < plainTextStr.Length; j++)
{
Console.Write($" {table[0,j]}");
}
Console.Write("\nEncrypted: ");
for (var j = 0; j < plainTextString.Length; j++)
Console.Write("\nEncrypted: ");
for (var j = 0; j < plainTextStr.Length; j++)
{
Console.Write($" {table[1, j]}");
}
Console.Write("\nDecrypted: ");
for (var j = 0; j < plainTextString.Length; j++)
for (var j = 0; j < plainTextStr.Length; j++)
{
Console.Write($" {table[2,j]}");
}
......@@ -108,16 +106,16 @@ namespace CryptoAlgo
static void CheckRSA(long n, long e)
{
long x = 1;
long x = 100;
long y = 1;
do
{
x++;
y = Divide(n,x);
y = findPrivKey(n,x);
} while (!PrimeChecker(x) || !PrimeChecker(y) || y==1);
Console.WriteLine("Private keys are: X: {0}, Y: {1}", x,y);
Console.WriteLine($"Private keys are: X: {x}, Y: {y} \n");
}
......@@ -139,7 +137,7 @@ namespace CryptoAlgo
static long Divide(long n, long x)
static long findPrivKey( long n, long x)
{
if (n % x == 0)
return n / x;
......@@ -148,28 +146,28 @@ namespace CryptoAlgo
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)
static long calcMod(long Base, long Secret, long Modulus)
{
long returnMod = 1;
for (var i = 1; i <= inputSecret; ++i)
for (var i = 1; i <= Secret; ++i)
{
returnMod = (returnMod * inputBase) % inputModulus;
returnMod = (returnMod * Base) % Modulus;
}
return returnMod;
}
......
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