加密

 

调用

DES md = new  DES();

System.Web.HttpContext.Current.Response.Cookies[
" CompanyID " ].Value = System.Web.HttpContext.Current.Server.UrlEncode(md.DESEncrypt(myCompanyID));
System.Web.HttpContext.Current.Response.Cookies[
" UserID " ].Value = System.Web.HttpContext.Current.Server.UrlEncode(md.DESEncrypt(us));

string  myCompanyID = System.Web.HttpContext.Current.Request.Cookies[ " CompanyID " ].Value;
myCompanyID
= md.DESDecrypt(System.Web.HttpContext.Current.Server.UrlDecode(myCompanyID)).Replace( "

DES.cs文件

using  System;
using  System.IO;
using  System.Text;
using  System.Security;
using  System.Security.Cryptography;
using  System.Collections;

namespace  CM_class
{
    
/// <summary>
    
/// DES 的摘要说明。
    
/// DES加密、解密
    
/// </summary>

    public class DES
    
{
        
public static byte[] DESKey = new byte[] {0x820xBC0xA10x6A0xF50x870x3B0xE60x590x6A0x320x640x7F0x3A0x2A0xBB0x2B0x680xE20x5F0x060xFB0xB80x2D0x670xB30x550x190x4E0xB80xBF0xDD };
        
public DES()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// DES加密
        
/// </summary>
        
/// <param name="strSource">待加密字串</param>
        
/// <param name="key">32位Key值</param>
        
/// <returns>加密后的字符串</returns>

        public string DESEncrypt(string strSource) 
        
{
            
return DESEncrypt(strSource, DESKey);
        }

        
public string DESEncrypt(string strSource,byte[] key)
        
{
            SymmetricAlgorithm sa 
= Rijndael.Create();
            sa.Key 
= key;
            sa.Mode
= CipherMode.ECB;
            sa.Padding 
= PaddingMode.Zeros;
            MemoryStream ms 
= new MemoryStream();
            CryptoStream cs 
= new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
            
byte[] byt = Encoding.Unicode.GetBytes(strSource);
            cs.Write(byt, 
0, byt.Length);
            cs.FlushFinalBlock();
            cs.Close();
            
return Convert.ToBase64String(ms.ToArray());
        }

        
/// <summary>
        
/// DES解密
        
/// </summary>
        
/// <param name="strSource">待解密的字串</param>
        
/// <param name="key">32位Key值</param>
        
/// <returns>解密后的字符串</returns>

        public string DESDecrypt(string strSource) 
        
{
            
return DESDecrypt(strSource, DESKey);
        }

        
public string DESDecrypt(string strSource,byte[] key)
        
{
            SymmetricAlgorithm sa 
= Rijndael.Create();
            sa.Key 
= key;
            sa.Mode 
= CipherMode.ECB;
            sa.Padding 
= PaddingMode.Zeros;
            ICryptoTransform ct 
= sa.CreateDecryptor();
            
byte[] byt = Convert.FromBase64String(strSource);
            MemoryStream ms 
= new MemoryStream(byt);
            CryptoStream cs 
= new CryptoStream(ms, ct, CryptoStreamMode.Read);
            StreamReader sr 
= new StreamReader(cs, Encoding.Unicode);
            
return sr.ReadToEnd();
        }

    }

}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值