C#实现用私钥的对称加密

在网络的数据交互中,特别是在从一个网站到另一个网站的数据验证上,通常我们需要更安全的加密方法,在ASP方面,实现起来会相对难点,而且代量也很大,但是在asp.net中有一系列的加密与解密的类与方法,比如对称加密。

需要用到的命名空间:

System

System.Text

System.Security.Cryptography

需要用到的类:

System.Convert

System.Text.Encoding

System.Security.Cryptography.TripleDESCryptoServiceProvider

核心代码:

       

         private   static   bool  Encrypt( byte [] KEY,  byte [] IV,  byte [] TobeEncrypted,  out   byte [] Encrypted)
        
{
            Encrypted 
= null;
            
try
            
{
                
byte[] buffer1 = new byte[] 01234567 };
                
for (int num1 = 0; num1 < 8; num1++)
                
{
                    buffer1[num1] 
= IV[num1];
                }

                
byte[] buffer2 = new byte[] 
                  
0123456701234567
                  
01234567
             }
;
                
for (int num2 = 0; num2 < 24; num2++)
                
{
                    buffer2[num2] 
= KEY[num2];
                }

                Encrypted 
= des.CreateEncryptor(buffer2, buffer1).TransformFinalBlock(TobeEncrypted, 0, TobeEncrypted.Length);
                des.Clear();
            }

            
catch
            
{
                
return false;
            }

            
return true;
        }


        
private   static   bool  Decrypt( byte [] KEY,  byte [] IV,  byte [] TobeDecrypted,  out   byte [] Decrypted)
        
{
            Decrypted 
= null;
            
try
            
{
                
byte[] buffer1 = new byte[] 01234567 };
                
for (int num1 = 0; num1 < 8; num1++)
                
{
                    buffer1[num1] 
= IV[num1];
                }

                
byte[] buffer2 = new byte[] 
                  
0123456701234567
                  
01234567
             }
;
                
for (int num2 = 0; num2 < 24; num2++)
                
{
                    buffer2[num2] 
= KEY[num2];
                }

                Decrypted 
= des.CreateDecryptor(buffer2, buffer1).TransformFinalBlock(TobeDecrypted, 0, TobeDecrypted.Length);
                des.Clear();
            }

            
catch
            
{
                
return false;
            }

            
return true;
        }


        
public   static   string  Encode( string  InputStr)
        
{
            
try
            
{
                
byte[] buffer1 = HexStringToByteArray(PrivateKey);
                
byte[] buffer2 = HexStringToByteArray(PrivateIV);
                
byte[] buffer3;
                
if (!Encrypt(buffer1, buffer2, Encoding.GetEncoding("utf-8").GetBytes(InputStr), out buffer3))
                
{
                    
return "";
                }

                
else
                
{
                    
return Convert.ToBase64String(buffer3);
                }

            }

            
catch
            
{
                
return "";
            }

        }


        
public   static   string  Decode( string  InputStr)
        
{
            
try
            
{
                
byte[] buffer1 = HexStringToByteArray(PrivateKey);
                
byte[] buffer2 = HexStringToByteArray(PrivateIV);
                
byte[] buffer3;
                
if (!Decrypt(buffer1, buffer2, Convert.FromBase64String(InputStr), out buffer3))
                
{
                    
return "";
                }

                
else
                
{
                    
return Encoding.GetEncoding("utf-8").GetString(buffer3);
                }

            }

            
catch
            
{
                
return "";
            }


        }

打包好的DLL下载:点击下载此文件

调用方法:

提示:PrivateKey  必须为48位 PrivateIV必需为16位 

using  System;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  SecurityCode;
public   partial   class  _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Code.PrivateKey 
= "Ad64cC32A8804C7AA8EA62589252231FFD6802E6028658A1";
        Code.PrivateIV 
= "1122030405060708";
        
string text1 = Code.Encode("test");
        
string test2 = Code.Decode(text1);
        Response.Write(text1 
+ "<br/>" + test2);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值