Commit a62b0c6f by adbaga

input validation check for dh

parent 0665cf1f
...@@ -9,12 +9,62 @@ namespace CryptoAlgo ...@@ -9,12 +9,62 @@ namespace CryptoAlgo
public static string RunDH() public static string RunDH()
{ {
Console.WriteLine("Enter the generator"); 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"); 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"); 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 mod = 601457;
long Anna = diff_hell(generator, annaSecret, mod); long Anna = diff_hell(generator, annaSecret, mod);
...@@ -34,6 +84,8 @@ namespace CryptoAlgo ...@@ -34,6 +84,8 @@ namespace CryptoAlgo
} }
public static long diff_hell(long a, long b, long c) public static long diff_hell(long a, long b, long c)
{ {
......
...@@ -5,9 +5,7 @@ namespace CryptoAlgo ...@@ -5,9 +5,7 @@ namespace CryptoAlgo
{ {
public class RSAhw2 public class RSAhw2
{ {
//static void Main(string[] args)
public static string runRSA() public static string runRSA()
{ {
...@@ -18,14 +16,14 @@ namespace CryptoAlgo ...@@ -18,14 +16,14 @@ namespace CryptoAlgo
do do
{ {
p = g.Next(100, 500); p = g.Next(100, 700);
} while (!PrimeChecker(p)); } while (!PrimeChecker(p));
var h = new Random(); var h = new Random();
int q; long q;
do do
{ {
q = h.Next(100, 500); q = h.Next(100, 700);
} while (!PrimeChecker(q)); } while (!PrimeChecker(q));
Console.WriteLine($"P value: {p}"); Console.WriteLine($"P value: {p}");
...@@ -49,7 +47,7 @@ namespace CryptoAlgo ...@@ -49,7 +47,7 @@ namespace CryptoAlgo
Console.WriteLine($"Co-prime to the ф(n): {m} is e:{e} "); Console.WriteLine($"Co-prime to the ф(n): {m} is e:{e} ");
int k = 0; long k = 0;
do do
{ {
if ((1 + k * m) % e == 0) break; if ((1 + k * m) % e == 0) break;
...@@ -64,16 +62,16 @@ namespace CryptoAlgo ...@@ -64,16 +62,16 @@ namespace CryptoAlgo
Console.WriteLine($"Private key: p*q = {p}*{q} ={n} d:{d}"); Console.WriteLine($"Private key: p*q = {p}*{q} ={n} d:{d}");
Console.WriteLine("Plaintext char:"); 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; 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; table[0, i] = plainText;
var encrypted = calcMod(plainText, e, n); var encrypted = calcMod(plainText, e, n);
...@@ -85,19 +83,19 @@ namespace CryptoAlgo ...@@ -85,19 +83,19 @@ namespace CryptoAlgo
Console.Write("Char code: "); 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($" {table[0,j]}");
} }
Console.Write("\nEncrypted: "); Console.Write("\nEncrypted: ");
for (var j = 0; j < plainTextString.Length; j++) for (var j = 0; j < plainTextStr.Length; j++)
{ {
Console.Write($" {table[1, j]}"); Console.Write($" {table[1, j]}");
} }
Console.Write("\nDecrypted: "); Console.Write("\nDecrypted: ");
for (var j = 0; j < plainTextString.Length; j++) for (var j = 0; j < plainTextStr.Length; j++)
{ {
Console.Write($" {table[2,j]}"); Console.Write($" {table[2,j]}");
} }
...@@ -108,16 +106,16 @@ namespace CryptoAlgo ...@@ -108,16 +106,16 @@ namespace CryptoAlgo
static void CheckRSA(long n, long e) static void CheckRSA(long n, long e)
{ {
long x = 1; long x = 100;
long y = 1; long y = 1;
do do
{ {
x++; x++;
y = Divide(n,x); y = findPrivKey(n,x);
} while (!PrimeChecker(x) || !PrimeChecker(y) || y==1); } 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 ...@@ -139,7 +137,7 @@ namespace CryptoAlgo
static long Divide(long n, long x) static long findPrivKey( long n, long x)
{ {
if (n % x == 0) if (n % x == 0)
return n / x; return n / x;
...@@ -148,28 +146,28 @@ namespace CryptoAlgo ...@@ -148,28 +146,28 @@ namespace CryptoAlgo
static long Gcd(long a, long b) static long Gcd(long a, long b)
{ {
//Case Divide 0
if (a == 0) if (a == 0)
return b; return b;
if (b == 0) if (b == 0)
return a; return a;
// base case
if (a == b) if (a == b)
return b; return b;
// a is greater
if (a > b) if (a > b)
return Gcd(a-b, b); return Gcd(a-b, b);
return Gcd(a, b-a); 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; 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; 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