private void btnSend_Click(object sender, EventArgs e) { DeActivateGetSignatureComponents(); if ((tbP.Text.Length == 0) || (tbQ.Text.Length == 0) || (tbD.Text.Length == 0)) { MessageBox.Show(NOT_DETERMINED_FIELD); } else { switch (cbHashAlgorithm.SelectedIndex) { case 0: { bool PQ_Correct; int p, q; if ((tbP.Text.Length < 10) && (tbQ.Text.Length < 10) && (tbD.Text.Length < 10)) { p = Int32.Parse(tbP.Text); q = Int32.Parse(tbQ.Text); PQ_Correct = Check_PQ(p, q); if (PQ_Correct) { int r = GetDispalyR(p, q); int EulerFunctionMeaning = FindEulerFunction(p, q); int d = Int32.Parse(tbD.Text); if ((d <= 1) || (d >= EulerFunctionMeaning) || (gcd(d, EulerFunctionMeaning) != 1)) { MessageBox.Show(D_REQUIREMENTS); } else { if (OpenFiledlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { int E = FindE(d, EulerFunctionMeaning); tbE.Text = Convert.ToString(E); var fi = new FileInfo(OpenFiledlg.FileName); if (fi.Length != 0) { rtbPlainText.Text = ""; byte[] data = File.ReadAllBytes(OpenFiledlg.FileName); int MessageHash = CalculateHash_Erud(data, r); DisplayMessageHash(MessageHash); int Sign = FindSignature(MessageHash, d, r); DisplaySignature(Sign); DisplayPlaintextOnScreen(data); byte[] SignArr = BitConverter.GetBytes(Sign); SignLength = SignArr.Length; // write sign representes as byte array to the file after source data and spacing byte[] ResArr = new byte[data.Length + 1 + SignArr.Length]; for (int i = 0; i < data.Length; i++) { ResArr[i] = data[i]; } ResArr[data.Length] = (byte)32; int j = 0; for (int i = data.Length + 1; i < ResArr.Length; i++) { ResArr[i] = SignArr[j]; j++; } File.WriteAllBytes(OpenFiledlg.FileName, ResArr); DiactivateComponents(); } else { MessageBox.Show(EMPTY_FILE_MSG); } IsEncrypted = true; } else { MessageBox.Show(FILE_NOT_CHOOSED); } } } else { MessageBox.Show(PQ_REQUIREMENTS); } } else { MessageBox.Show(LONGPQ); } break; } case 1: { BigInteger p, q; p = BigInteger.Parse(tbP.Text); q = BigInteger.Parse(tbQ.Text); if (CheckPrimeBigInteger(p, q)) { BigInteger r = p * q; tbR.Text = r.ToString(); BigInteger EulerFuncBigInteger = (p - 1) * (q - 1); BigInteger d = BigInteger.Parse(tbD.Text); if ((d <= 1) || (d >= EulerFuncBigInteger) || (gcd(d, EulerFuncBigInteger) != 1)) { MessageBox.Show(D_REQUIREMENTS); } else { if (OpenFiledlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { BigInteger E = BigIntegerFindE(d, EulerFuncBigInteger); tbE.Text = Convert.ToString(E); rtbPlainText.Text = ""; byte[] data = File.ReadAllBytes(OpenFiledlg.FileName); byte[] Initializedrray = InitializeBlocks(data); BigInteger MessageHash = Calculate_SHA1(Initializedrray); BigIntegerDisplayMessageHash(MessageHash); BigInteger Sign = BigIntegerFindSignature(MessageHash, d, r); DisplaySignature(Sign); DisplayPlaintextOnScreen(data); // write sign representes as byte array to the file after source data and spacing byte[] ArrSignBig = Sign.ToByteArray(); SignLength = ArrSignBig.Length; byte[] ResArr = new byte[data.Length + 1 + ArrSignBig.Length]; for (int i = 0; i < data.Length; i++) { ResArr[i] = data[i]; } ResArr[data.Length] = (byte)32; int j = 0; for (int i = data.Length + 1; i < ResArr.Length; i++) { ResArr[i] = ArrSignBig[j]; j++; } File.WriteAllBytes(Signature_File, ResArr); IsEncrypted = true; DiactivateComponents(); } else { MessageBox.Show(FILE_NOT_CHOOSED); } } } else { MessageBox.Show(PQ_BIG); } break; } } } }
private void btnCheck_Click(object sender, EventArgs e) { if (IsEncrypted) { ActivateGetSignatureComponents(); if ((tbE.Text.Length == 0) || (tbR.Text.Length == 0)) { MessageBox.Show(DeterminedER); } else { switch (cbHashAlgorithm.SelectedIndex) { case 0: { if ((tbR.Text.Length < 10) && (tbE.Text.Length < 10)) { int r = Int32.Parse(tbR.Text); int E = Int32.Parse(tbE.Text); if ((OpenFiledlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) && (IsEncrypted)) { IsEncrypted = false; var fi = new FileInfo(OpenFiledlg.FileName); if (fi.Length != 0) { rtbPlainText.Text = ""; int LastWordIndex = 0; byte[] dataAndSign = File.ReadAllBytes(OpenFiledlg.FileName); byte[] FileSign = NewGetSign(ref LastWordIndex, dataAndSign); byte[] data = new byte[LastWordIndex]; for (int i = 0; i < LastWordIndex; i++) { data[i] = dataAndSign[i]; } DisplayPlaintextOnScreen(data); int MessageHash = CalculateHash_Erud(data, r); DiplayNewMessageHashes(MessageHash); int GettingSign = BitConverter.ToInt32(FileSign, 0); int GettingHash = FindSignature(GettingSign, E, r); DisplayComparison(GettingHash, MessageHash); } else { MessageBox.Show(EMPTY_FILE_MSG); } } else { MessageBox.Show(FILE_NOT_CHOOSED); } } else { MessageBox.Show(LONGER); } break; } case 1: { BigInteger R = BigInteger.Parse(tbR.Text); BigInteger E = BigInteger.Parse(tbE.Text); if (CheckRE(R, E)) { if ((OpenFiledlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) && (IsEncrypted)) { IsEncrypted = false; rtbPlainText.Text = ""; int LastWordIndex = 0; byte[] dataAndSign = File.ReadAllBytes(OpenFiledlg.FileName); byte[] FileSign = NewGetSign(ref LastWordIndex, dataAndSign); BigInteger GettingSign = new BigInteger(FileSign); byte[] data = new byte[LastWordIndex]; for (int i = 0; i < LastWordIndex; i++) { data[i] = dataAndSign[i]; } DisplayPlaintextOnScreen(data); byte[] Initializedrray = InitializeBlocks(data); BigInteger MessageHash = Calculate_SHA1(Initializedrray); BigIntegerDisplayNewMessageHashes(MessageHash); // BigInteger GetSign = BigInteger.Parse(tbDigitalSignature.Text); BigInteger GettingHash = BigIntegerFindSignature(GettingSign, E, R); DisplayComparisonBigInteger(GettingHash, MessageHash); } else { MessageBox.Show(FILE_NOT_CHOOSED); } } else { MessageBox.Show(ErrorRE); } break; } } } } else { MessageBox.Show(SignFirst); } }