14-加密

作者: D丝学编程 | 来源:发表于2024-02-26 10:54 被阅读0次

    一、MD5加密

    0090.PNG

    MD5特点:单向加密,只能加密,不能解密。

    MD5加密案例:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h3>单向加密,只能加密,不能解密</h3>
        <div>
            <asp:TextBox ID="txt" runat="server"></asp:TextBox>
            <asp:Button ID="btMD5" runat="server" Text="MD5加密" onclick="btMD5_Click" />
            <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
        </div>
        </form>
    </body>
    </html>
    
    public partial class Demo01_MD5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void btMD5_Click(object sender, EventArgs e)
        {
            //MD5加密方式一:
            //string str = FormsAuthentication.HashPasswordForStoringInConfigFile(this.TextBox1.Text, "MD5");
            //this.Label1.Text = str;
    
            //MD5加密方式二:
            byte[] arrStart = Encoding.Default.GetBytes(this.txt.Text);   //字符串变字节数组
            MD5 md5 = new MD5CryptoServiceProvider();  //定义MD5加密类
            byte[] arrEnd = md5.ComputeHash(arrStart);  //利用md5算法对字节数组进行加密
            //加密16位
            //this.lblResult.Text = BitConverter.ToString(arrEnd,4,8).Replace("-", "");  //将加密后的字节数组转换成字符串
            //加密32位
            this.lblResult.Text = BitConverter.ToString(arrEnd).Replace("-", "");  //将加密后的字节数组转换成字符串
        }
    }
    

    二、DES加密

    0091.PNG

    DES特点:对称加密(加密解密钥匙相同)。

    DES加密案例:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h3>双向对称加密(加密解密钥匙相同-DES实现对称加密)</h3>
        <div>
            <asp:TextBox ID="txtEncryption" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btEncryption" runat="server" Text="加密" 
                onclick="btEncryption_Click"  />
            <asp:Label ID="lblEncryption" runat="server" Text="加密结果"></asp:Label>
        </div>
        <br />
        <br />
        <div>
            <asp:TextBox ID="txtDecrypt" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btDecrypt" runat="server" Text="解密" 
                onclick="btDecrypt_Click"  />
            <asp:Label ID="lblDecrypt" runat="server" Text="解密结果"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    
    public partial class Demo02_DES : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string key = "";
                DESCryptoServiceProvider des = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
                key = ASCIIEncoding.ASCII.GetString(des.Key);
                ViewState["key"] = key;
            }
        }
    
        #region 加密
        protected void btEncryption_Click(object sender, EventArgs e)
        {
            byte[] data = Encoding.Default.GetBytes(this.txtEncryption.Text);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //此处可以使用固定的Key和IV
            DES.Key = Encoding.Default.GetBytes("gaojun11");  //长度8
            DES.IV = Encoding.Default.GetBytes("gaojun22");  //长度8
            //此处的Key,IV也可以使用Page_Load中生成的ViewState["key"]代替
            //DES.Key = Encoding.Default.GetBytes(ViewState["key"].ToString());  //长度8
            //DES.IV = Encoding.Default.GetBytes(ViewState["key"].ToString());  //长度8
            ICryptoTransform desEncrypt = DES.CreateEncryptor();
            byte[] result = desEncrypt.TransformFinalBlock(data, 0, data.Length);
            this.lblEncryption.Text = Convert.ToBase64String(result);
        }
        #endregion
    
        #region 解密
        protected void btDecrypt_Click(object sender, EventArgs e)
        {
            byte[] data = Convert.FromBase64String(this.txtDecrypt.Text);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //此处可以使用固定的Key和IV
            DES.Key = Encoding.Default.GetBytes("gaojun11"); //长度8
            DES.IV = Encoding.Default.GetBytes("gaojun22"); //长度8
            //此处的Key,IV也可以使用Page_Load中生成的ViewState["key"]代替
            //DES.Key = Encoding.Default.GetBytes(ViewState["key"].ToString());  //长度8
            //DES.IV = Encoding.Default.GetBytes(ViewState["key"].ToString());  //长度8
            ICryptoTransform desencrypt = DES.CreateDecryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            this.lblDecrypt.Text = Encoding.Default.GetString(result);
            //return Encoding.UTF8.GetString(result);
        }
        #endregion
    }
    

    三、RSA加密

    RSA特点:主要实现非对称加密,但这里也可以实现对称。

    (1)RSA实现对称加密

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h3>双向对称加密(加密解密钥匙相同-RSA主要实现非对称加密,但这里也可以实现对称)</h3>
        <div>
            <asp:TextBox ID="txtEncryption" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btEncryption" runat="server" Text="加密" 
                onclick="btEncryption_Click" />
            <asp:Label ID="lblEncryption" runat="server" Text="加密结果"></asp:Label>
        </div>
        <br />
        <br />
        <div>
            <asp:TextBox ID="txtDecrypt" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btDecrypt" runat="server" Text="解密" onclick="btDecrypt_Click" />
            <asp:Label ID="lblDecrypt" runat="server" Text="解密结果"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    
    public partial class Demo02_RSA1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        //加密
        protected void btEncryption_Click(object sender, EventArgs e)
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gaojun"; //密匙容器的名称,保持加密解密一致才能解密成功
            
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param);
            byte[] arrStart = Encoding.Default.GetBytes(this.txtEncryption.Text); //将要加密的字符串转换为字节数组
            byte[] arrEnd = rsa.Encrypt(arrStart, false);//将加密后的字节数据转换为新的加密字节数组
            this.lblEncryption.Text = Convert.ToBase64String(arrEnd);//将加密后的字节数组转换为字符串       
        }
    
        //解密
        protected void btDecrypt_Click(object sender, EventArgs e)
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gaojun";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param);
            byte[] arrStart = Convert.FromBase64String(this.txtDecrypt.Text);
            byte[] arrEnd = rsa.Decrypt(arrStart, false);
            this.lblDecrypt.Text = Encoding.Default.GetString(arrEnd);
        }
    }
    

    (2)RSA实现非对称加密

    RSA特点:主要实现非对称加密。

    生成钥匙:

    0092.PNG
    加密解密:
    0093.PNG
    xml文件保存私钥和公钥:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btComputeKey" runat="server" Text="计算公钥秘钥" 
                onclick="btComputeKey_Click" />
            <asp:Label ID="lblKey" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    
    public partial class Demo03_MakeKey : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void btComputeKey_Click(object sender, EventArgs e)
        {
            string privateKeyPath = Server.MapPath("PrivateKey.xml"); //私钥保存位置
            string publicKeyPath = Server.MapPath("PublicKey.xml");   //公钥保存位置
            //创建RSA对象
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string privateKey = rsa.ToXmlString(true);
            string publicKey = rsa.ToXmlString(false);
            File.WriteAllText(privateKeyPath, privateKey);
            File.WriteAllText(publicKeyPath, publicKey);
            this.lblKey.Text = "公钥-私钥生成成功,保存在PublicKey.xml和PrivateKey.xml中";
        }
    }
    

    加密解密实现:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h3>双向不对称加密(加密解密钥匙不相同-RSA主要实现非对称加密,但这里也可以实现对称)</h3>
        <div>
            <asp:TextBox ID="txtEncryption" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btEncryption" runat="server" Text="使用PublicKey.xml文件内容为公钥进行加密" 
                onclick="btEncryption_Click" />
            <asp:Label ID="lblEncryption" runat="server" Text="加密结果"></asp:Label>
        </div>
        <br />
        <br />
        <div>
            <asp:TextBox ID="txtDecrypt" runat="server" Width="287px"></asp:TextBox>
            <asp:Button ID="btDecrypt" runat="server" Text="使用PrivateKey.xml文件内容为私钥解密" onclick="btDecrypt_Click" />
            <asp:Label ID="lblDecrypt" runat="server" Text="解密结果"></asp:Label>
        </div>
        </form>
    </body>
    </html>
    
    public partial class Demo02_RSA2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        
        //加密
        protected void btEncryption_Click(object sender, EventArgs e)
        {
            string publicKeyPath = Server.MapPath("PublicKey.xml");   //公钥保存位置
            string publicKey = File.ReadAllText(publicKeyPath);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicKey);
            byte[] arrStart = Encoding.Default.GetBytes(this.txtEncryption.Text); //将要加密的字符串转换为字节数组
            byte[] arrEnd = rsa.Encrypt(arrStart, false);//将加密后的字节数据转换为新的加密字节数组
            this.lblEncryption.Text = Convert.ToBase64String(arrEnd);//将加密后的字节数组转换为字符串
        }
    
        //解密
        protected void btDecrypt_Click(object sender, EventArgs e)
        {
            string privateKeyPath = Server.MapPath("PrivateKey.xml"); //私钥保存位置
            string privateKey = File.ReadAllText(privateKeyPath);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(privateKey);
            byte[] arrStart = Convert.FromBase64String(this.txtDecrypt.Text);
            byte[] arrEnd = rsa.Decrypt(arrStart, false);
            this.lblDecrypt.Text = Encoding.Default.GetString(arrEnd);
        }
    }
    

    相关文章

      网友评论

          本文标题:14-加密

          本文链接:https://www.haomeiwen.com/subject/fjszadtx.html