zoukankan      html  css  js  c++  java
  • Visual Studio.net 2008开发ActiveX过程与总结

    1 : 前言

    最近因为公司需要,让我研究一下ActiveX控件,主要实现触摸屏的银行帐号,密码输入,效果如下图所示

        

    2: 开发过程注意事项

    2.1 简介

    整个方案包括三个项目,分别是  

    1.Avantouch.Platform.ActiveX(WindowsFormsControlLibrary)

    2. Avantouch.Platform.ActiveX.Clent(Web Application)

    3. Avantouch.Platform.ActiveX.Setup(安装项目)

         其中ActiveX项目是主要程序,所有ActiveX实现程序都在这里。Clent项目是一个Web应用程序,用于测试ActiveXSetup是一个安装项目,用于安装ActiveX

          

     

          2.2  关键设置

            1  ActiveX项目的 Assembly.cs中的特殊设置:
     [assembly: ComVisible(true)]

            2  如下图所示 ActiveXa项目的属性设置里找到Build 勾选Regester for COM Interop,

             

     

            3   Setup项目的项目输出是ActiveX项目

            4   Client项目页面可以通过这样的方式来使用ActiveX控件

     

    <object classid="clsid:3CFCF47C-D783-4e7c-8E18-A839D2E45271" codebase="setup.exe#version=1.0.0.0" width="700" height="262" id="Avantouch.ActiveX"></object>

          其中Setup.exe就是安装项目生成的exe文件,如上面所示,只需将生成的Setup.exe文件放到网页文件同一目录当中即可

          在页面中,可以用Javascript来调用ActiveX控件的方法,

             

    代码
     function AlertDesString() {
                
    var objKey = document.getElementById("txt_PublicKey");
                
    var EnString = document.getElementById("Avantouch.ActiveX").ReturnEncryptString(objKey.value);
                document.getElementById(
    "txt_EnString").value = EnString;
                window.location.href 
    = "TestActiveX.aspx?Enstring=" + URLencode(EnString) + "&PrivateKey=" + document.getElementById("txt_PrivateKey").value + "";
            }
     

     其中 document.getElementById("Avantouch.ActiveX").ReturnEncryptString(objKey.value);就是调用ActiveX提供的方法ReturnEncryptString

     

    完整的页面源文件是

     

    代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Avantouch.Platform.ActiveX.Clent._Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title></title>
           
    <script type="text/javascript">  
           
            
    function AlertDesString() {
                
    var objKey = document.getElementById("txt_PublicKey");
                
    var EnString = document.getElementById("Avantouch.ActiveX").ReturnEncryptString(objKey.value);
                document.getElementById(
    "txt_EnString").value = EnString;
                window.location.href 
    = "TestActiveX.aspx?Enstring=" + URLencode(EnString) + "&PrivateKey=" + document.getElementById("txt_PrivateKey").value + "";
            }

            
    function URLencode(sStr) {
                
    return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g, '%22').replace(/\'/g, '%27').replace(/\//g, '%2F');
            }

        
    </script> 
    </head>
    <body background="Image/信用卡效果图.jpg">
         
         
    <form name="frm" id="frm" runat="server" 
         style
    ="background-image: url('Image/确认.png')"> 
        
             
    <table width = "100%" style="position:absolute;top:199px;left:4px">
         
         
              
    <tr >
              
    <td colspan="5">
              
              
    </td>
           
    </tr>
       
           
    <tr>
              
    <td width="26%"></td>
              
    <td colspan="4">
               
    <object classid="clsid:3CFCF47C-D783-4e7c-8E18-A839D2E45271" codebase="setup.exe#version=1.0.0.0" width="700" height="262" id="Avantouch.ActiveX"></object>
              
    </td>
           
    </tr>
           
              
    <tr>
              
    <td >
              
    </td>
              
    <td colspan="4" align="right">
                
    <br />
                
    <br />
                 
    <input type="button" value=""    onclick="AlertDesString()" 
                      
                      style
    ="background-position: center center; background-image: url('Image/确认.png'); 123px; height:60px" />&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;        </td>
           
    </tr>
              
    <tr >
               
    <td>
               
              
    </td>
              
    <td colspan="4">
               
              
    </td>
           
    </tr>
        

        
    </table>

        
    <table  style="display:none" >
               
        
    <tr>
        
         
    <td> <input type="button" value="产生公钥,私钥" onclick="setFocus()" /></td>
         
    <td>  公钥 </td>
         
    <td>   <asp:TextBox ID="txt_PublicKey" runat="server"></asp:TextBox> </td>
         
    <td> 私钥  </td>
          
    <td>   <asp:TextBox ID="txt_PrivateKey" runat="server"></asp:TextBox></td>
        
    </tr>
         
    <tr>
        
    <td>  <input type="button" value="产生公钥加密后的字符串" onclick="AlertDesString()" /></td>
       
    <td>  </td>
        
    <td>   <asp:TextBox ID="txt_EnString" runat="server"></asp:TextBox> </td>
         
    <td> </td>
          
    <td>  </td>
        
    </tr>
         
    <tr>
        
    <td> &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                Text
    ="用私钥解密后的字符串" />
             
    </td>
        
    <td>  </td>
        
    <td>   <asp:TextBox ID="txt_Password" runat="server"></asp:TextBox> </td>
         
    <td> </td>
          
    <td>  
              
    <asp:TextBox ID="txt_NoPassword" runat="server"></asp:TextBox>
             
    </td>
        
    </tr>
         
    <tr>
        
    <td> </td>
        
    <td></td>
        
    </tr>
         
    <tr>
        
    <td> </td>
        
    <td></td>
        
    </tr>
        
        
    </table>
        
        
          
    </form>
    </body>
    </html>

     

     

    3 RSA加密的使用

     

     
    代码
      private void GenerateKey()
            {
                
    try
                {
                    
    string publicKey = "";
                    
    string privateKey = "";
                    CspParameters RSAParams 
    = new CspParameters();
                    RSAParams.Flags 
    = CspProviderFlags.UseMachineKeyStore;
                    RSACryptoServiceProvider provider 
    = new RSACryptoServiceProvider(1024, RSAParams);
                    
    //privateKey = provider.ToXmlString(true);   //产生私月
                    
    //publicKey = provider.ToXmlString(false);  //产生公钥

                    privateKey 
    = "<RSAKeyValue><Modulus>uaFIi4mmm5M/jUZPIXtFtMRMVwmk1/7RSD2YTdf5KlbnOcZgkZXmgS2nI79zF+F+59Z3OcrOh3gA/mzw5laGt+N+sFe787tGKZf0iOptbd5x7KJhUHrCytjVigtmkTnRcz70M7O4iNfQwCSzq7s7uNbuxhcXJ/wxJE3VX0KYSB0=</Modulus><Exponent>AQAB</Exponent><P>4l++gQOt6tt8vdaGlB+XoGKEwtZvCSxWGnUl3bBiiLbYUKrVxq9GVKqfAvk/Z+v9+txj//s1U+3BbSz1QAOc/Q==</P><Q>0ex8HaSk54ZaV/4Knxz49uZjV3NTYe5ti3SSpT/1rZnZmZ3L+5rGhLel9rrsshQ4npSI/HOxiXi0kabuXkjRoQ==</Q><DP>10MUdLp694of5KXK86/XD7yfYmdmzuJKPn6Hs3e3OyokKRFKl9S0nG5jJwC9OqU+rv5EWXcYDudXt+m4jVbiQQ==</DP><DQ>iVF+pw1qaaDzPBgL7qVPHoL6fNwNsUP8GHs66in6lYLMWrZkFsPaFDrlr437A0pC90kGis1LVzfENnrR9o7/wQ==</DQ><InverseQ>K7fJEUZwk7DvDLLY433MS+1aSYhIYbwRwYXR73PfOh+PfdVDFO2ajYD87NlTQh6OmHvLPecVTJx4z5zqghgTog==</InverseQ><D>QPtix+Yx+rg7QRthzBi5OCSXjMiFek9rt9xIGsgGI3o0i5cUayKMhKAUqID2q19263W6WvLSBb5OqsI8OoYDtncjNO7LHkMo2nFO+4qXgn7MrxdtF4AHlC2RO9KHgEHnqsYXIYng4WsaBefxdR5eFKOuEHgyeklrLkXeAQhsvQE=</D></RSAKeyValue>";   //产生私月
                    publicKey = "<RSAKeyValue><Modulus>uaFIi4mmm5M/jUZPIXtFtMRMVwmk1/7RSD2YTdf5KlbnOcZgkZXmgS2nI79zF+F+59Z3OcrOh3gA/mzw5laGt+N+sFe787tGKZf0iOptbd5x7KJhUHrCytjVigtmkTnRcz70M7O4iNfQwCSzq7s7uNbuxhcXJ/wxJE3VX0KYSB0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";  //产生公钥

                    
    //私钥  64位编码
                    byte[] bytes = Encoding.Default.GetBytes(privateKey);
                    
    string strPrivateKey = Convert.ToBase64String(bytes);
                    txt_PrivateKey.Text 
    = strPrivateKey;
                    
                    
    //公钥  64位编码
                    byte[] bytes2 = Encoding.Default.GetBytes(publicKey);
                    
    string strPublicKey = Convert.ToBase64String(bytes2);
                    txt_PublicKey.Text 
    = strPublicKey;
                 
                }

                
    catch (Exception exception)
                {
                    
    throw exception;
                }
            }

            
    private void GenerationKey()
            {
                RSACryptoServiceProvider provider 
    = new RSACryptoServiceProvider();
                
    string privateKey = provider.ToXmlString(true);
                
    string publicKey = provider.ToXmlString(false);
                
    //    string encodeString = Encrypt("1234567");
                
    //   lb1.Text = encodeString;

                
    //     string decode = Decrypt(encodeString);

                
    //   lb2.Text = decode;

            }

            
    /// <summary>
            
    /// 解密
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            protected void Button1_Click(object sender, EventArgs e)
            {
                
    byte[] bytes = Encoding.Default.GetBytes(txt_PrivateKey.Text);
                bytes 
    = Convert.FromBase64String(txt_PrivateKey.Text);
                
    string strKey = Encoding.Default.GetString(bytes);
           
                
                
    byte[] bytes2 = Encoding.Default.GetBytes(txt_EnString.Text);
                bytes2 
    = Convert.FromBase64String(txt_PrivateKey.Text);
                
    string strKey2 = Encoding.Default.GetString(bytes2);
            
                
                txt_Password.Text 
    = Decrypt(txt_EnString.Text, strKey);
            }

            
    /// <summary>
            
    /// 
            
    /// </summary>
            
    /// <param name="base64code">待加密字符串</param>
            
    /// <param name="publicKey">公钥</param>
            
    /// <returns></returns>
            public string Decrypt(string base64code, string publicKey)
            {
                
    try
                {

                    
    //Create a UnicodeEncoder to convert between byte array and string.
                    UnicodeEncoding ByteConverter = new UnicodeEncoding();
                    CspParameters RSAParams 
    = new CspParameters();

                   
                    
                    RSAParams.Flags 
    = CspProviderFlags.UseMachineKeyStore;
                 
    //   RSAParams.Flags = Cspp

                    
    //Create a new instance of RSACryptoServiceProvider to generate
                    
    //public and private key data.
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, RSAParams);
                  
    //  RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    RSA.FromXmlString(publicKey);
                    
    byte[] encryptedData; ;
                    
    byte[] decryptedData;
                  
    //  Response.Write("<script> alert('" + base64code + "');  </script>");
                    encryptedData = Convert.FromBase64String(base64code);
                    
    //Pass the data to DECRYPT, the private key information 
                    
    //(using RSACryptoServiceProvider.ExportParameters(true),

                 
    //   Response.Write("<script> alert('" + encryptedData.Count().ToString() + "');  </script>");
                    
    //and a boolean flag specifying no OAEP padding.

                   
    // Response.Write("<script> alert('" + ByteConverter.GetString(encryptedData) + "');  </script>");

                    decryptedData 
    = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
                 
    //   Response.Write("<script> alert('0');  </script>");
                    
    //Display the decrypted plaintext to the console. 
                  
                    
    if(decryptedData != null)
                    {
                        
    return ByteConverter.GetString(decryptedData);
                    }


                    
    return "Password Error";
                }
                
    catch (Exception exc)
                {
                    
    //Exceptions.LogException(exc);
                    
    //  Console.WriteLine(exc.Message);
                    return exc.Message;
                }
            }
            
    /// <summary>
            
    /// 
            
    /// </summary>
            
    /// <param name="toEncryptString">待解密字符串</param>
            
    /// <param name="PrivateKey">私钥</param>
            
    /// <returns></returns>
            public string Encrypt(string toEncryptString, string PrivateKey)
            {
                
    try
                {
                    
    //Create a UnicodeEncoder to convert between byte array and string.
                    UnicodeEncoding ByteConverter = new UnicodeEncoding();
                    
    //Create byte arrays to hold original, encrypted, and decrypted data.
                    byte[] dataToEncrypt = ByteConverter.GetBytes(toEncryptString);
                    
    byte[] encryptedData;
                    
    byte[] decryptedData;
                    CspParameters RSAParams 
    = new CspParameters();
                    RSAParams.Flags 
    = CspProviderFlags.UseMachineKeyStore;
                    
    //Create a new instance of RSACryptoServiceProvider to generate
                    
    //public and private key data.
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024, RSAParams);
                    RSA.FromXmlString(PrivateKey);
                    
    //Pass the data to ENCRYPT, the public key information 
                    
    //(using RSACryptoServiceProvider.ExportParameters(false),
                    
    //and a boolean flag specifying no OAEP padding.
                    encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
                    
    string base64code = Convert.ToBase64String(encryptedData);
                    
    return base64code;
                }
                
    catch (Exception exc)
                {
                    
    //Catch this exception in case the encryption did
                    
    //not succeed.
                    
    //Exceptions.LogException(exc);
                    
    //  Console.WriteLine(exc.Message);
                    return "";
                }

            }
             
    private byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                
    try
                {
                    
    //Create a new instance of RSACryptoServiceProvider.
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    
    //Import the RSA Key information. This only needs
                    
    //toinclude the public key information.
                    RSA.ImportParameters(RSAKeyInfo);
                    
    //Encrypt the passed byte array and specify OAEP padding.  
                    
    //OAEP padding is only available on Microsoft Windows XP or
                    
    //later.  
                    return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
                }
                
    //Catch and display a CryptographicException  
                
    //to the console.
                catch (CryptographicException e)
                {
                    
    //Exceptions.LogException(e);
                    Console.WriteLine(e.Message);

                    
    return null;
                }

            }
            
    private byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
            {
                
    try
                {
                    
    //Create a new instance of RSACryptoServiceProvider.
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                    
    //Import the RSA Key information. This needs
                    
    //to include the private key information.
                    RSA.ImportParameters(RSAKeyInfo);

                    
    //Decrypt the passed byte array and specify OAEP padding.  
                    
    //OAEP padding is only available on Microsoft Windows XP or
                    
    //later.  
                    return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
                }
                
    //Catch and display a CryptographicException  
                
    //to the console.
                catch (CryptographicException e)
                {
                    
    //Exceptions.LogException(e);
                    
    //  Console.WriteLine(e.Message);
                    txt_NoPassword.Text = e.Message;
                 
    //   Response.Write("<script> alert('"+ e.Message  +"');  </script>");
                    return null;
                }

            }
  • 相关阅读:
    PHP array_intersect_uassoc
    PHP array_intersect_key
    PHP array_intersect_assoc
    PHP array_flip
    PHP array_filter
    PHP array_fill
    PHP array_fill_keys
    Android4.0-Fragment框架实现方式剖析
    Fragment 生命周期
    WebView
  • 原文地址:https://www.cnblogs.com/zycblog/p/1762818.html
Copyright © 2011-2022 走看看