none
有關數位簽章的問題 RRS feed

  • 一般討論

  • 各位大大你們好

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Security.Cryptography;
    using Microsoft.VisualBasic;

    namespace DigitalSignatureDemo
    {
        public partial class Form3 : Form
        {
            private Alice alice = new Alice();
            private Bob bob = new Bob();
           
            public Form3()
            {
                InitializeComponent();
            }
            /*
            AOIDb.Form1 ai = new AOIDb.Form1();
            public string g_send
            {
                get { return ai.txt_DigitalSignature.Text; }
            }
            public string s_send
            {
                set { ai.txt_DigitalSignature.Text = value; }
            }
            */
            private void btnSign_Click(object sender, EventArgs e)
            {
               
                byte[] orgData = Encoding.ASCII.GetDecoder(this.txtOrgData.Text);
                byte[] signature = alice.HashAnsSign(orgData);


                txtSignature.Text = Convert.ToByte(signature);

                /*
                ai.txt_DigitalSignature.Text = txtSignature.Text;
                ai.txt_DigitalSignature.Text = Convert.ToBase64String(signature);
                ai.ShowDialog();
                */
               
                lblSignedLength.Text = "簽章大小: " + signature.Length.ToString() + " bytes";
            }

           

            private void btnVerifySignedData_Click(object sender, EventArgs e)
            {
               
                byte[] orgData = Encoding.ASCII.GetDecoder(txtOrgData.Text);
                byte[] signedData = Convert.ToByte(txtSignature.Text);
               

                bool ok = bob.VerifySignedData(orgData, signedData, alice.PublicKey);
                if (ok)
                {
                    MessageBox.Show("驗證成功!");
                  
                }
                else
                {
                    MessageBox.Show("簽章無效!");
                }
            }

            private void txtSignature_TextChanged(object sender, EventArgs e)
            {

            }
        }

        public class Alice
        {
            RSACryptoServiceProvider m_Rsa;
           
            public Alice()
            {
                m_Rsa = new RSACryptoServiceProvider(); // 建立 RSA 加密器,並自動建立一組金鑰對。
            }
           
            public byte[] HashAnsSign(byte[] dataToSign)
            {
                // 使用 SHA1 演算法產生雜湊值,然後產生簽章。
                return m_Rsa.SignData(dataToSign, new SHA1CryptoServiceProvider());
          
            }

            public string PublicKey
            {
                get
                {
                    return m_Rsa.ToXmlString(false);
                }
            }
        }
       
        public class Bob
        {
            public bool VerifySignedData(byte[] data, byte[] signature, string publicKey)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(publicKey);
                return rsa.VerifyData(data, new SHA1CryptoServiceProvider(), signature);
            }
        }
    }

     

    這是我的程式碼

     

    問題是要如何改成顯示ASCII 16進位的方式

     

    我在其中的程式碼改成以下方式卻顯示各行的程式錯誤是否可以請各位大大不吝指教

     

     byte[] orgData = Encoding.ASCII.GetDecoder(this.txtOrgData.Text);

    Error 1 No overload for method 'GetDecoder' takes '1' arguments 

     

    txtSignature.Text = Convert.ToByte(signature);

    Error 2 Cannot implicitly convert type 'byte' to 'string' 

     

    byte[] orgData = Encoding.ASCII.GetDecoder(txtOrgData.Text);

    Error 3 No overload for method 'GetDecoder' takes '1' arguments 

     

    byte[] signedData = Convert.ToByte(txtSignature.Text);

    Error 4 Cannot implicitly convert type 'byte' to 'byte[]' 

    2008年7月31日 上午 02:19

所有回覆