public string Decryption() { Console.WriteLine("Alice recieves the numbers and starting to work with it."); Console.WriteLine("Decrypted Word's Codes:"); List <BigInt> wordNumbers = new List <BigInt>(); BigInt one = new BigInt("1"); for (int i = 0; i < word.Length; i++) { //wordNumbers.Add(BigInt.powByMod(wordNumbersEncrypted[i], d, mod)); wordNumbers.Add(ChineseTheorem(BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(p, one))), p), BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(q, one))), q), p, q)); //Console.Write("Chinese: " + ChineseTheorem(BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(p, one))), p), BigInt.mod(BigInt.pow(wordNumbersEncrypted[i], BigInt.mod(d, BigInt.subtract(q, one))), q), p, q).toString()); Console.Write(wordNumbers[i].toString() + " "); } Console.WriteLine(); string decryptedWord = ""; for (int i = 0; i < word.Length; i++) { decryptedWord += (char)Int32.Parse(wordNumbers[i].toString()); } Console.WriteLine("Decrypted Word: " + decryptedWord); Console.WriteLine("Sign: " + BigInt.powByMod(SignEncrypted, eSign, modSign).toString()); Console.WriteLine("Hash: " + BigInt.mod(Hash(word, modSign), modSign).toString()); Console.WriteLine("------------------------------------------------"); return(decryptedWord); }
public void Encryption(string tempWord) { Console.WriteLine("------------------------------------------------"); Console.WriteLine("Bob recieves the word " + tempWord + " and starting to work with it."); word = tempWord; List <BigInt> wordNumbers = new List <BigInt>(); Console.WriteLine("Word's Codes:"); for (int i = 0; i < word.Length; i++) { wordNumbers.Add(new BigInt(((int)word[i]).ToString())); Console.Write(wordNumbers[i].toString() + " "); } Console.WriteLine(); wordNumbersEncrypted = new List <BigInt>(); Console.WriteLine("Encrypted Word's Codes:"); for (int i = 0; i < wordNumbers.Count; i++) { wordNumbersEncrypted.Add(BigInt.powByMod(wordNumbers[i], e, mod)); Console.Write(wordNumbersEncrypted[i].toString() + " "); } SignEncrypted = BigInt.powByMod(Hash(word, modSign), dSign, modSign); Console.WriteLine(); Console.WriteLine("Bob is sending numbers to Alice."); Console.WriteLine("------------------------------------------------"); }
public static BigInt powByMod(BigInt x, BigInt y, BigInt N) { if (y.isZero()) { return(new BigInt("1")); } BigInt z = BigInt.powByMod(x, divide(y, new BigInt("2")), N); if (mod(y, valueOf(2)).isZero()) { return(mod(multiply(z, z), N)); } else { return(mod(multiply(multiply(z, z), x), N)); } }
static void calculate() { BigInt a, b, m; int n; switch (choice) { case 1: Console.WriteLine("1 - +"); a = getNumber("First "); // BigInt.Billie(a); // Console.WriteLine((BigInt.Billie(a).toString())); b = getNumber("Second "); Console.WriteLine(a.toString() + "+" + b.toString() + "=" + BigInt.add(a, b).toString()); break; case 2: Console.WriteLine("2 - -"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "-" + b.toString() + "=" + BigInt.subtract(a, b).toString()); break; case 3: Console.WriteLine("3 - *"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "*" + b.toString() + "=" + BigInt.multiply(a, b).toString()); break; case 4: Console.WriteLine("4 - /"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "/" + b.toString() + "=" + BigInt.divide(a, b).toString()); Console.WriteLine("Ostacha: " + BigInt.subtract(a, BigInt.multiply(b, BigInt.divide(a, b))).toString()); break; case 5: Console.WriteLine("5 - ^"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "^" + b.toString() + "=" + BigInt.pow(a, b).toString()); break; case 6: Console.WriteLine("6 - [sqrt(x)]"); a = getNumber("Number >0 "); Console.WriteLine("sqrt =" + a.sqrt().toString()); break; case 7: Console.WriteLine("7 - <"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "<" + b.toString() + "=" + BigInt.less(a, b)); break; case 8: Console.WriteLine("8 - >"); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + ">" + b.toString() + "=" + BigInt.greater(a, b)); break; case 9: Console.WriteLine("9 - =="); a = getNumber("First "); b = getNumber("Second "); Console.WriteLine(a.toString() + "==" + b.toString() + "=" + BigInt.equal(a, b)); break; case 10: Console.WriteLine("10 - + (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "+" + b.toString() + "mod" + m.toString() + "=" + BigInt.addMod(a, b, m).toString()); break; case 11: Console.WriteLine("11 - - (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "-" + b.toString() + "mod" + m.toString() + "=" + BigInt.subtractMod(a, b, m).toString()); break; case 12: Console.WriteLine("12 - * (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "*" + b.toString() + "mod" + m.toString() + "=" + BigInt.multiplyByMod(a, b, m).toString()); break; case 13: Console.WriteLine("13 - /(mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "/" + b.toString() + "mod" + m.toString() + "=" + BigInt.divideMod(a, b, m).toString()); Console.WriteLine("Ostacha: " + BigInt.subtractMod(a, BigInt.multiply(b, BigInt.divide(a, b)), m).toString()); break; case 14: Console.WriteLine("14 - ^ (mod)"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByMod(a, b, m).toString()); break; case 15: Console.WriteLine("15 - aiX=bi(mod pi)"); n = getUInt("Count of equations"); BigInt[] A = new BigInt[n]; BigInt[] B = new BigInt[n]; BigInt[] M = new BigInt[n]; for (int i = 0; i < n; i++) { A[i] = getNumber("A[" + i + "]"); B[i] = getNumber("B[" + i + "]"); M[i] = getNumber("P[" + i + "]"); } for (int i = 0; i < n; i++) { Console.WriteLine(A[i].toString() + "x =" + B[i].toString() + "mod" + M[i].toString()); } (BigInt, BigInt)res = BigInt.solve(A, B, M, n); Console.WriteLine("x=" + res.Item1.toString() + "mod" + res.Item2.toString()); break; case 16: Console.WriteLine("16 - ^ (mod) fast"); a = getNumber("First "); b = getNumber("Second "); m = getNumber("mod "); Console.WriteLine(a.toString() + "^" + b.toString() + "mod" + m.toString() + "=" + BigInt.powByModFast(a, b, m).toString()); break; case 17: Console.WriteLine("17 - Miller Rabin"); a = getNumber("Number "); Console.WriteLine(BigInt.MillerRabin(a)); break; case 18: //Console.WriteLine("18 - Billie"); //Console.WriteLine("Enter the word"); //string word = Console.ReadLine(); //RSA test = new RSA(8, 1, 6); //test.Encryption(word); //Console.WriteLine(test.Decryption()); break; case 19: Console.WriteLine("Enter the word"); string word = Console.ReadLine(); long ellapledTicks = DateTime.Now.Ticks; RSA test = new RSA(8, 32, 64); test.Encryption(word); ellapledTicks = DateTime.Now.Ticks - ellapledTicks; TimeSpan elapsedSpan = new TimeSpan(ellapledTicks); Console.WriteLine(test.Decryption()); Console.WriteLine("Time: " + elapsedSpan.TotalMilliseconds); break; case 20: Console.WriteLine("19 - exit"); end = true; break; } }