加密和解密函数

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// SecureAttribute 的摘要说明。
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  SecureAttribute
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static byte[] keys = new byte[]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
13398721431716218783715179199247
ExpandedSubBlockEnd.gif        }
;
InBlock.gif        
private static byte[] vectors = new byte[]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
913923177213639012953122187190321013513892
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
private static byte[] keys = new byte[]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
13398721431716218783715179199247
ExpandedSubBlockEnd.gif        }
;
InBlock.gif        
private static byte[] vectors = new byte[]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
913923177213639012953122187190321013513892
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        
// 加密串
InBlock.gif
        public static string EncryptString(string Source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ASCIIEncoding textConverter 
= new ASCIIEncoding();
InBlock.gif            RC2CryptoServiceProvider rc2CSP 
= new RC2CryptoServiceProvider();
InBlock.gif            
byte[] encrypted;
InBlock.gif            
byte[] toEncrypt;
InBlock.gif            rc2CSP.EffectiveKeySize 
= 128;
InBlock.gif            
// 生成加密器.
InBlock.gif
            ICryptoTransform encryptor = rc2CSP.CreateEncryptor(keys, vectors);
InBlock.gif            
InBlock.gif            
// 加密数据
InBlock.gif
            MemoryStream msEncrypt = new MemoryStream();
InBlock.gif            CryptoStream csEncrypt 
= new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
InBlock.gif
InBlock.gif            
// 转换数据到字节表
InBlock.gif
            toEncrypt = textConverter.GetBytes(Source);
InBlock.gif
InBlock.gif            
// 执行加密
InBlock.gif
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
InBlock.gif            csEncrypt.FlushFinalBlock();
InBlock.gif
InBlock.gif            
// 取加密过的字节流
InBlock.gif
            encrypted = msEncrypt.ToArray();
InBlock.gif
InBlock.gif            
// 返回结果
InBlock.gif
            return Convert.ToBase64String(encrypted);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 解密串
InBlock.gif
        public static string DecryptString(string Source)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] fromEncrypt;
InBlock.gif            
byte[] encrypted;
InBlock.gif
InBlock.gif            ASCIIEncoding textConverter 
= new ASCIIEncoding();
InBlock.gif            RC2CryptoServiceProvider rc2CSP 
= new RC2CryptoServiceProvider();
InBlock.gif            rc2CSP.EffectiveKeySize 
= 128;
InBlock.gif
InBlock.gif            
// 生成解密器
InBlock.gif
            ICryptoTransform decryptor = rc2CSP.CreateDecryptor(keys, vectors);
InBlock.gif
InBlock.gif            
// 取得源加密字节流
InBlock.gif
            encrypted    = System.Convert.FromBase64String(Source);
InBlock.gif
InBlock.gif            
// 解密字节流
InBlock.gif
            MemoryStream msDecrypt = new MemoryStream(encrypted);
InBlock.gif            CryptoStream csDecrypt 
= new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
InBlock.gif
InBlock.gif            fromEncrypt 
= new byte[encrypted.Length];
InBlock.gif            csDecrypt.Read(fromEncrypt, 
0, fromEncrypt.Length);
InBlock.gif            
int n = 0;
InBlock.gif            
foreach (byte b in fromEncrypt)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (b != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    n
++;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
byte[] bs = new byte[n];
InBlock.gif            
for (int i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bs[i] 
= fromEncrypt[i];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string s = textConverter.GetString(bs);
InBlock.gif
InBlock.gif            
// 返回解密串
InBlock.gif
            return s;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

转载于:https://www.cnblogs.com/gjahead/archive/2006/06/23/433917.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值