日期:2014-05-18  浏览次数:20831 次

C#基础知识整理:基础知识(10) 静态

    如果想访问某个类的方法或属性,一定要先实例化该类,然后用该类的对象加.号访问。比如:
有一个用户类和一个处理密码(加密和解密)的类。没生成一个用户实例后,处理密码类要对密码进行加密和解密。

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

namespace YYS.CSharpStudy.MainConsole.Static
{
    /// <summary>
    /// 用户类
    /// </summary>
    public class User
    {
        //加密解密用到的Key
        private string key = "20120719";
        //加密解密用到的向量
        private string ivalue = "12345678";

        private string userName;

        private string userEncryptPassword;

        private string userDecryptPassword;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get
            {
                return userName;
            }
        }
        /// <summary>
        /// 用户密码,加密后的密码
        /// </summary>
        public string UserEncryptPassword
        {
            get
            {
                return userEncryptPassword;
            }
        }
        /// <summary>
        /// 用户密码,解密后的密码
        /// </summary>
        public string UserDecryptPassword
        {
            get
            {
                DES des = new DES();

                this.userDecryptPassword = des.Decrypt(userEncryptPassword, key, ivalue);

                return userDecryptPassword;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        public User(string userName, string userPassword)
        {
            this.userName = userName;

            DES des = new DES();

            this.userEncryptPassword = des.Encrypt(userPassword, key, ivalue);
        }
    }

    /// <summary>
    /// 处理密码的类
    /// </summary>
    public class DES
    {
        /// <summary>
        /// 加密字符串
        /// </summary>
        public string Encrypt(string sourceString, string key, string iv)
        {
            try
            {
                byte[] btKey = Encoding.UTF8.GetBytes(key);

                byte[] btIV = Encoding.UTF8.GetBytes(iv);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                    try
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);

                            cs.FlushFinalBlock();
                        }

                        return Convert.ToBase64String(ms.ToArray());
                    }
                    catch
                    {
                        return sourceString;
                    }
                }
            }
            catch { }

            return sourceString;
        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        public string Decrypt(string encryptedString, string key, string iv)
        {
            byte[] btKey = Encoding.UTF8.GetBytes(key);

            byte[] btIV = Encoding.UTF8.GetBytes(iv);

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] inData = Convert.FromBase64String(encryptedString);
                try
                {
                    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                    {
                        cs.Write(inData, 0, inData.Length);