using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;

namespace cry
{
    
/// <summary>
    
/// Cryop 的摘要说明。
    
/// </summary>

    public class Cryop
    
{
        
//Keys与IV的长度相等
        private static Byte[] Keys = System.Text.Encoding.Unicode.GetBytes("天天向上");
        
private static Byte[] IV = new Byte[]{0x120x340x560x780x900xAB0xCD0xEF};
        

        
/// <summary>
        
/// 加密
        
/// </summary>

        public static Byte[] Enc(String test)
        
{            
            System.Security.Cryptography.RC2CryptoServiceProvider rc2 
= new RC2CryptoServiceProvider();
            MemoryStream msStream 
= new MemoryStream();    
            
byte[] toEncrypt;    
            
try
            
{
                ICryptoTransform encrypt 
= rc2.CreateEncryptor(Keys,IV);
                
                CryptoStream csStream 
= new CryptoStream(msStream,encrypt,CryptoStreamMode.Write);

                toEncrypt 
= System.Text.Encoding.Unicode.GetBytes(test);
                csStream.Write(toEncrypt,
0,toEncrypt.Length);
                csStream.FlushFinalBlock();
            }

            
catch(Exception ex)
            
{
                
throw new Exception(ex.Message,ex);
            }

            
return msStream.ToArray();

        }
 

        
/// <summary>
        
/// 解密
        
/// </summary>

        public static String Dec(Byte[] enc)
        
{
            System.Security.Cryptography.RC2CryptoServiceProvider rc2 
= new RC2CryptoServiceProvider();
            
try
            
{
                ICryptoTransform decrypt 
= rc2.CreateDecryptor(Keys,IV);
                MemoryStream msDec 
= new MemoryStream(enc);
                CryptoStream csDec 
= new CryptoStream(msDec,decrypt,CryptoStreamMode.Read);
                
byte[] fromEncpty = new Byte[enc.Length];
                csDec.Read(fromEncpty,
0,fromEncpty.Length);
                
return System.Text.Encoding.Unicode.GetString(fromEncpty);
            }

            
catch(Exception ex)
            
{
                
throw new Exception(ex.Message,ex);
            }

            
        }

    }

}

posted on 2005-03-28 12:34  王员外  阅读(937)  评论(0编辑  收藏  举报