有一个用户类和一个处置惩罚暗码(加密和解密)的类。没生成一个用户实例后,处置惩罚暗码类要对暗码举行加密和解密。
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); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } catch { return encryptedString; } } } } }
挪用:
class Program { static void Main(string[] args) { User user = new User("yangyoushan", "000000"); Console.WriteLine(string.Format("用户名:{0}", user.UserName)); Console.WriteLine(string.Format("加密后的暗码:{0}", user.UserEncryptPassword)); Console.WriteLine(string.Format("明文的暗码:{0}", user.UserDecryptPassword)); Console.ReadKey(); } }
效果:
这两个类完成的代码内里,有两个题目。
1、关于每实例化一个user,都要运转
DES des = new DES(); this.userEncryptPassword = des.Encrypt(userPassword, key, ivalue);
也就是每次都要实例化一个DES实例。如许是不好的,实例化DES只是为了挪用它的要领罢了,然则每次挪用要领都有要实例化倒是不太轻易,而且也增加了内存的斲丧。
2、关于
//加密解密用到的Key private string key = "20120719"; //加密解密用到的向量 private string ivalue = "12345678";
这两个变量是每一个user实例都要用到的,而且不会变化。然则每实例化一个user都要分派空间,一样也是对内存有斲丧的,而且就面向对象头脑来讲,也不大合理。
既然如许,那末最好是将DES的两个要领公用出来,而且不必经由过程实例化就能够直接挪用就好。比方Math的一切要领(Math.Abs(1);)。再一个就是将User里的key和ivalue变量也设置为公用,而且也能够直接接见,而且只分派一次内存空间,而实例化user时不必再别的分派了。
这就要用到静态,即static关键字。所谓静态就是,成员被一个类所同享。也就是说被声明为静态的成员不属于一个特定的类的对象,属于这个类一切对象。一切类的成员都能够声明为静态,能够声明静态字段、静态属性或静态要领。不过这里要区分一下const和static,const是指常量在顺序运转中是不能转变值的,static则能够在运转中转变值,而且一个处所转变,别的处所接见到的都是转变后的值。
如许就能够够应用静态,来优化上述的代码,以下:
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 static string key = "20120719"; //加密解密用到的向量 private static 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 { //运用静态要领和静态字段 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; this.userEncryptPassword = DES.Encrypt(userPassword, key, ivalue); } } /// <summary> /// 处置惩罚暗码的类 /// </summary> public class DES { /// <summary> /// 加密字符串 /// </summary> public static 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 static 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); cs.FlushFinalBlock(); } return Encoding.UTF8.GetString(ms.ToArray()); } catch { return encryptedString; } } } } }
运转效果:
不过要注意一个题目,平常要领能够接见要领外的静态属性或静态要领。然则假如是静态要领中要接见要领外的属性或要领,那末被接见的属性和要领也必需是静态的。由于平常的属性或要领只要在实例化后才分派空间,才能够运用,而静态中则是直接在编译的时刻就分派好了内存空间,因而静态要领中挪用别的的属性或要领是不能够的,只能挪用同时静态的才能够。
以上就是C#基础知识整顿:基础知识(10) 静态的内容,更多相关内容请关注ki4网(www.ki4.cn)!