public byte[] Create(byte[] text, RSA RSA) { SHA_1 SHA1 = new SHA_1(); byte[] SHAHash = new byte[20]; SHAHash = SHA1.GetHash(text).Value; BigInteger BI_Hash = new BigInteger(SHAHash); byte[] signature = RSA.EncryptHash(BI_Hash).ToByteArray(); return(signature); }
private void btnVerify_Click(object sender, RoutedEventArgs e) { if ((FileName == null) || (FileName == "")) { MessageBox.Show("Error: Choose file."); } else if ((Signature == null) || (Signature == "")) { MessageBox.Show("Error: Choose file with signature."); } else { BigInteger eps, r; // public key if (IsPublicKeyValid(out eps, out r)) { RSA RSA = new RSA(eps, r); SHA_1 SHA1 = new SHA_1(); byte[] realHash = new byte[20]; realHash = SHA1.GetHash(Message).Value; // BigInteger values are represented in little endian Array.Reverse(realHash); // sure that a positive value is not incorrectly instantiated as a negative value // by adding a byte whose value is zero to the end of the array byte[] temp = new byte[realHash.Length]; Array.Copy(realHash, temp, realHash.Length); realHash = new byte[temp.Length + 1]; Array.Copy(temp, realHash, temp.Length); // decimal representation of hash tbHashDecimal.Text = new BigInteger(realHash).ToString(); //hexadecimal representation of hash string hexHash = new BigInteger(realHash).ToString("X"); if (hexHash[0] == '0') { hexHash = hexHash.Substring(1); } tbHashHex.Text = hexHash; // convert signature to BigInteger BigInteger BI_Hash = BigInteger.Parse(Signature); //encrypt hash from file using public key byte[] checkedHash = new byte[20]; RSA Rsa = new RSA(eps, r); checkedHash = Rsa.DecryptHash(BI_Hash).ToByteArray(); if (new BigInteger(realHash) > RSA.R) { MessageBox.Show("Error: Hash is greater than R."); return; } // output encrypted hash from file tbCheckedHash.Text = new BigInteger(checkedHash).ToString(); if (new BigInteger(checkedHash) == new BigInteger(realHash)) { MessageBox.Show("Digital signature is correct. File is authentic."); } else { MessageBox.Show("Digital signature isn't correct!"); } } } }
private void btnSign_Click(object sender, RoutedEventArgs e) { if ((FileName == null) || (FileName == "")) { MessageBox.Show("Error: choose file"); } else if ((Message == null) || (Message.Length == 0)) { MessageBox.Show("Error: empty file"); return; } else { BigInteger p, q, eps, d; // secret key if (IsSecretKeyValid(out p, out q, out eps, out d)) { // modulo BigInteger r; r = p * q; tbR.Text = r.ToString(); RSA RSA = new RSA(p, q, eps, d, r); SHA_1 SHA1 = new SHA_1(); byte[] SHAHash = new byte[20]; SHAHash = SHA1.GetHash(Message).Value; // BigInteger values are represented in little endian Array.Reverse(SHAHash); // sure that a positive value is not incorrectly instantiated as a negative value // by adding a byte whose value is zero to the end of the array byte[] temp = new byte[SHAHash.Length]; Array.Copy(SHAHash, temp, SHAHash.Length); SHAHash = new byte[temp.Length + 1]; Array.Copy(temp, SHAHash, temp.Length); // convert hash array to BigInteger BigInteger BI_Hash = new BigInteger(SHAHash); if (BI_Hash > r) { MessageBox.Show("Error: Hash is greater than R."); return; } // decimal representation of hash tbHashDecimal.Text = BI_Hash.ToString(); //hexadecimal representation of hash string hexHash = BI_Hash.ToString("X"); if (hexHash[0] == '0') { hexHash = hexHash.Substring(1); } tbHashHex.Text = hexHash; // encrypt hash string signature = RSA.EncryptHash(BI_Hash).ToString(); tbD.Text = RSA.D.ToString(); tbE.Text = RSA.E.ToString(); tbDigitalSignature.Text = signature; // save signature to file File.WriteAllText(FileName.Substring(0, FileName.IndexOf('.')) + "_Sign.txt", signature); } } }