public string Decrypt(string cipherText) { char[] cipherArray = cipherText.ToCharArray(); int i = 0; string decryptedCipher = ""; progressBar2.Maximum = cipherArray.Length; try { for (; i < cipherArray.Length; i++) { Application.DoEvents(); string cipher = ""; progressBar2.Value = i; int j; for (j = i; cipherArray[j] != '-'; j++) { cipher += cipherArray[j]; } i = j; ulong cipherValue = Convert.ToUInt64(cipher); decryptedCipher += ((char)RSAlgorithm.BigMod(cipherValue, d, n)).ToString(); } } catch (Exception) { } return(decryptedCipher); }
/// <summary> /// Encrypt the selected text file, which is encoded using the unicode charset /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnEncryptText_Click(object sender, EventArgs e) { n = RSAlgorithm.N(p, q); BtnSelectImage.Enabled = false; DisableUI(); //Get running time for encription Stopwatch TimerForEncription = new Stopwatch(); TimerForEncription.Start(); String encrypted = Encrypt(stringFile, progressBarEnc); TimerForEncription.Stop(); //End running time calculation File.WriteAllText(txtSaveCipherAtPath.Text, encrypted); MessageBox.Show("Encryption Done"); BtnSelectImage.Enabled = true; BtnLoadImage.Enabled = true; BtnSaveImageCipherAt.Enabled = true; BtnDecryptImage.Enabled = true; BtnSelectDecrytedImagePath.Enabled = true; BtnLoadImageCipher.Enabled = true; button10.Enabled = true; BtnEncryptImage.Enabled = true; GetRunningTime(TimerForEncription.ElapsedMilliseconds.ToString()); }
public string Encrypt(string image, ProgressBar progressBar) { string hexImage = image; char[] hexImageArray = hexImage.ToCharArray(); string cipher = ""; progressBar.Maximum = hexImageArray.Length; for (int i = 0; i < hexImageArray.Length; i++) { Application.DoEvents(); progressBar.Value = i; //lblPercentage.Text = ((double)i / (double)progressBar.Maximum * 100).ToString(); int percent = (int)(((double)progressBar.Value / (double)progressBar.Maximum) * 100); ///progressBar.Refresh(); progressBar.CreateGraphics().DrawString(percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(progressBar.Width / 2 - 10, progressBar.Height / 2 - 7)); if (cipher == "") { cipher += RSAlgorithm.BigMod(hexImageArray[i], e, n); } else { cipher += "-" + RSAlgorithm.BigMod(hexImageArray[i], e, n); } } return(cipher); }
private void BtnGetRSAElements_Click(object sender, EventArgs e) { //65537 var primes = RSACrypto.RSAHelper.GeneratePrimes(10000); var result = RSACrypto.RSAHelper.GetTwoPrimes(primes); string Prime1 = primes[(int)result[0]].ToString(); string Prime2 = primes[(int)result[1]].ToString(); var N = primes[(int)result[0]] * primes[(int)result[1]]; p = Convert.ToUInt64(primes[(int)result[0]]); q = Convert.ToUInt64(primes[(int)result[1]]); textBox2.Text = Prime1; textBox3.Text = Prime2; textBox8.Text = N.ToString(); // //call the function for calculating e here var phi = RSAlgorithm.Phi(p, q); EDForm.e = RSAlgorithm.Find_E(phi); bool flag = false; for (ulong j = 1; !flag; j++) { if ((EDForm.e * j) % phi == 1) { d = j; flag = true; break; } } //Set Public and Private Key textBox4.Text = EDForm.e.ToString(); textBox9.Text = d.ToString(); if (MakerCheckerMessageMemoEdit.Text.Trim() == "") { MakerCheckerMessageMemoEdit.Text = "P=" + Prime1 + ", Q=" + Prime2 + ", N=" + N + System.Environment.NewLine + "Public Key=[" + EDForm.e.ToString() + "," + N + "]" + System.Environment.NewLine + "Private Key=[" + d.ToString() + ", " + N + "]"; } else { MakerCheckerMessageMemoEdit.Text = MakerCheckerMessageMemoEdit.Text + System.Environment.NewLine + "_______________________________________________" + System.Environment.NewLine + "P=" + Prime1 + ", Q=" + Prime2 + ", N=" + N + System.Environment.NewLine + "Public Key=[" + EDForm.e.ToString() + "," + N + "]" + System.Environment.NewLine + "Private Key=[" + d.ToString() + ", " + N + "]"; } //button3.Text = "Reset Details"; Application.DoEvents(); }
public string Encrypt(string image) { string hexImage = image; char[] hexImageArray = hexImage.ToCharArray(); string cipher = ""; progressBar1.Maximum = hexImageArray.Length; for (int i = 0; i < hexImageArray.Length; i++) { Application.DoEvents(); progressBar1.Value = i; if (cipher == "") { cipher += RSAlgorithm.BigMod(hexImageArray[i], e, n); } else { cipher += "-" + RSAlgorithm.BigMod(hexImageArray[i], e, n); } } return(cipher); }
public static object[] PopulateKeys(ulong e, ulong d, ulong p, ulong q, params TextBox[] textBoxes) { var primes = GeneratePrimes(10000); var result = GetTwoPrimes(primes); string Prime1 = primes[(int)result[0]].ToString(); string Prime2 = primes[(int)result[1]].ToString(); var N = RSAlgorithm .N((ulong)primes[result[0]], (ulong)primes[result[1]]); p = Convert.ToUInt64(primes[result[0]]); q = Convert.ToUInt64(primes[result[1]]); textBoxes[0].Text = Prime1; textBoxes[1].Text = Prime2; textBoxes[2].Text = N.ToString(); var phi = RSAlgorithm.Phi(p, q); e = RSAlgorithm.Find_E(phi); bool flag = false; for (ulong j = 1; !flag; j++) { if ((e * j) % phi == 1) { d = j; flag = true; } } textBoxes[3].Text = e.ToString(); textBoxes[4].Text = d.ToString();; var returnValues = new object[] { p, q, N, e, d }; return(returnValues); }