public void subtract_fun() { FileStream fs = new FileStream("SubtractTestCases.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); FileStream ss = new FileStream("thesuboutput.txt", FileMode.OpenOrCreate); StreamWriter sw = new StreamWriter(ss); strcount = sr.ReadLine(); str = sr.ReadLine(); str2 = "0"; while (sr.Peek() != -1) { str = sr.ReadLine(); if (str == "") { break; } str2 = sr.ReadLine(); a = new Bigint(str); b = new Bigint(str2); a.subtract_in_strings_in_bigint(b); sw.WriteLine(a.items_string); sw.WriteLine(); strcount = sr.ReadLine(); } sr.Close(); fs.Close(); sw.Close(); ss.Close(); }
public void multiplication_function() { FileStream mes = new FileStream("MultiplyTestCases.txt", FileMode.Open); StreamReader mer = new StreamReader(mes); FileStream msse = new FileStream("themuloutput.txt", FileMode.OpenOrCreate); StreamWriter mswe = new StreamWriter(msse); strcount = mer.ReadLine(); str = mer.ReadLine(); str2 = "0"; while (mer.Peek() != -1) { str = mer.ReadLine(); if (str == "") { break; } str2 = mer.ReadLine(); a = new Bigint(str); b = new Bigint(str2); a.Karatsuba_in_bigint(b); mswe.WriteLine(a.items_string); mswe.WriteLine(); strcount = mer.ReadLine(); } mer.Close(); mes.Close(); mswe.Close(); msse.Close(); }
public void add_fun() { FileStream es = new FileStream("AddTestCases.txt", FileMode.Open); StreamReader er = new StreamReader(es); FileStream sse = new FileStream("theaddoutput.txt", FileMode.OpenOrCreate); StreamWriter swe = new StreamWriter(sse); strcount = er.ReadLine(); str = er.ReadLine(); str2 = "0"; while (er.Peek() != -1) { str = er.ReadLine(); if (str == "") { break; } str2 = er.ReadLine(); a = new Bigint(str); b = new Bigint(str2); a.add_in_String_in_bigint(b); swe.WriteLine(a.items_string); swe.WriteLine(); strcount = er.ReadLine(); } er.Close(); es.Close(); swe.Close(); sse.Close(); }
public void Rsafinishcomplete() { FileStream es = new FileStream("TestRSA2.txt", FileMode.Open); StreamReader er = new StreamReader(es); FileStream sse = new FileStream("ourProjectTestRSA.txt", FileMode.OpenOrCreate); StreamWriter swe = new StreamWriter(sse); String e, M, n, zero_one; strcount = er.ReadLine(); while (er.Peek() != -1) { a = new Bigint(""); n = er.ReadLine(); e = er.ReadLine(); M = er.ReadLine(); zero_one = er.ReadLine(); if (zero_one == "0") { swe.WriteLine(a.Encrypt(n, e, M)); } else { swe.WriteLine(a.Decrypt(n, e, M)); } } er.Close(); es.Close(); swe.Close(); sse.Close(); }
public void add_in_String_in_bigint(Bigint Y) { MakeEqualstring(ref items_string, ref Y.items_string); String Res = ""; int carry = 0; for (int i = items_string.Length - 1; i >= 0; i--) { long num1 = Convert.ToInt64(items_string[i].ToString()); long num2 = Convert.ToInt64(Y.items_string[i].ToString()); long result = num1 + num2 + carry; if (result > 9) { Res = Res.Insert(0, (result % 10).ToString()); carry = 1; } else { Res = Res.Insert(0, (result).ToString()); carry = 0; } } if (carry == 1) { Res = Res.PadLeft(items_string.Length + 1, '1'); } items_string = Res; }
public void subtract_in_strings_in_bigint(Bigint Y) { MakeEqualstring(ref items_string, ref Y.items_string); int borrow = 0; String RES = ""; for (int i = items_string.Length - 1; i >= 0; i--) { long num1 = Convert.ToInt64(items_string[i].ToString()); long num2 = Convert.ToInt64(Y.items_string[i].ToString()); if (num1 - borrow < num2) { num1 += 10; num1 -= num2; num1 -= borrow; borrow = 1; } else { num1 -= num2; num1 -= borrow; borrow = 0; } RES = RES.Insert(0, num1.ToString()); } RES = RES.TrimStart('0'); if (RES == "") { RES += "0"; } items_string = RES; }
public String Karatsuba_in_bigint(Bigint y) { int n = MakeEqualstring(ref items_string, ref y.items_string); if (n == 1) { long X_Result = Convert.ToInt64(items_string); long Y_Result = Convert.ToInt64(y.items_string); long result = X_Result * Y_Result; return(result.ToString()); } if (n % 2 != 0) { items_string = "0" + items_string; y.items_string = "0" + y.items_string; n++; } int half = n / 2; string A = items_string.Substring(0, half); string B = items_string.Substring(half, half); string C = y.items_string.Substring(0, half); string D = y.items_string.Substring(half, half); String AC = Karatsuba(A, C); String BD = Karatsuba(B, D); String aplusb = add_in_String(A, B); String cplusd = add_in_String(C, D); String Res1 = Karatsuba(aplusb, cplusd); String Z1 = subtract_in_strings(Res1, AC); String Z = subtract_in_strings(Z1, BD); Z = Z.TrimStart(new char[] { '0' }); AC = AC.PadRight(AC.Length + (B.Length + D.Length), '0'); Z = Z.PadRight(Z.Length + (B.Length + D.Length) / 2, '0'); String r = add_in_String(add_in_String(Z, AC), BD); r = r.TrimStart(new char[] { '0' }); items_string = r; return(r); }