今天小编就为大家带来一篇C#中的常见加密解密类的文章。小编觉得挺不错的,为此分享给大家做个参考。一起跟随小编过来看看吧。

常见的加密方式分为可逆和不可逆两种方式

可逆:RSA,AES,DES等

不可逆:常见的MD5,SHAD等

常见的加密方式封装到一个Password类中

publicclassPassword{///<summary>///此代码示例通过创建哈希字符串适用于任何MD5哈希函数(在任何平台)上创建32个字符的十六进制格式哈希字符串///官网案例改编///</summary>///<paramname="source"></param>///<returns></returns>publicstaticstringGet32MD5One(stringsource){using(System.Security.Cryptography.MD5md5Hash=System.Security.Cryptography.MD5.Create()){byte[]data=md5Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source));System.Text.StringBuildersBuilder=newSystem.Text.StringBuilder();for(inti=0;i<data.Length;i++){sBuilder.Append(data[i].ToString("x2"));}stringhash=sBuilder.ToString();returnhash.ToUpper();}}///<summary>///获取16位md5加密///</summary>///<paramname="source"></param>///<returns></returns>publicstaticstringGet16MD5One(stringsource){using(System.Security.Cryptography.MD5md5Hash=System.Security.Cryptography.MD5.Create()){byte[]data=md5Hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(source));//转换成字符串,并取9到25位stringsBuilder=BitConverter.ToString(data,4,8);//BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉sBuilder=sBuilder.Replace("-","");returnsBuilder.ToString().ToUpper();}}////<summary>///</summary>///<paramname="strSource">需要加密的明文</param>///<returns>返回32位加密结果,该结果取32位加密结果的第9位到25位</returns>publicstaticstringGet32MD5Two(stringsource){System.Security.Cryptography.MD5md5=newSystem.Security.Cryptography.MD5CryptoServiceProvider();//获取密文字节数组byte[]bytResult=md5.ComputeHash(System.Text.Encoding.Default.GetBytes(source));//转换成字符串,32位stringstrResult=BitConverter.ToString(bytResult);//BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉strResult=strResult.Replace("-","");returnstrResult.ToUpper();}////<summary>///</summary>///<paramname="strSource">需要加密的明文</param>///<returns>返回16位加密结果,该结果取32位加密结果的第9位到25位</returns>publicstaticstringGet16MD5Two(stringsource){System.Security.Cryptography.MD5md5=newSystem.Security.Cryptography.MD5CryptoServiceProvider();//获取密文字节数组byte[]bytResult=md5.ComputeHash(System.Text.Encoding.Default.GetBytes(source));//转换成字符串,并取9到25位stringstrResult=BitConverter.ToString(bytResult,4,8);//BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉strResult=strResult.Replace("-","");returnstrResult.ToUpper();}//SHA为不可逆加密方式publicstaticstringSHA1Encrypt(stringnormalTxt){varbytes=System.Text.Encoding.Default.GetBytes(normalTxt);varSHA=newSystem.Security.Cryptography.SHA1CryptoServiceProvider();varencryptbytes=SHA.ComputeHash(bytes);returnConvert.ToBase64String(encryptbytes);}publicstaticstringSHA256Encrypt(stringnormalTxt){varbytes=System.Text.Encoding.Default.GetBytes(normalTxt);varSHA256=newSystem.Security.Cryptography.SHA256CryptoServiceProvider();varencryptbytes=SHA256.ComputeHash(bytes);returnConvert.ToBase64String(encryptbytes);}publicstaticstringSHA384Encrypt(stringnormalTxt){varbytes=System.Text.Encoding.Default.GetBytes(normalTxt);varSHA384=newSystem.Security.Cryptography.SHA384CryptoServiceProvider();varencryptbytes=SHA384.ComputeHash(bytes);returnConvert.ToBase64String(encryptbytes);}publicstringSHA512Encrypt(stringnormalTxt){varbytes=System.Text.Encoding.Default.GetBytes(normalTxt);varSHA512=newSystem.Security.Cryptography.SHA512CryptoServiceProvider();varencryptbytes=SHA512.ComputeHash(bytes);returnConvert.ToBase64String(encryptbytes);}///<summary>///将base64格式,转换utf8///</summary>///<paramname="content">解密内容</param>///<returns></returns>publicstaticstringBase64Decode(stringcontent){byte[]bytes=Convert.FromBase64String(content);returnSystem.Text.Encoding.UTF8.GetString(bytes);}///<summary>///DES加密数据///</summary>///<paramname="Text"></param>///<paramname="sKey"></param>///<returns></returns>publicstaticstringDESEncryption(stringText,stringsKey=null){sKey=sKey??"zhiqiang";try{System.Security.Cryptography.DESCryptoServiceProviderdes=newSystem.Security.Cryptography.DESCryptoServiceProvider();byte[]inputByteArray;inputByteArray=System.Text.Encoding.Default.GetBytes(Text);stringmd5SKey=Get32MD5One(sKey).Substring(0,8);des.Key=System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey);des.IV=System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey);System.IO.MemoryStreamms=newSystem.IO.MemoryStream();System.Security.Cryptography.CryptoStreamcs=newSystem.Security.Cryptography.CryptoStream(ms,des.CreateEncryptor(),System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(inputByteArray,0,inputByteArray.Length);cs.FlushFinalBlock();System.Text.StringBuilderret=newSystem.Text.StringBuilder();foreach(bytebinms.ToArray()){ret.AppendFormat("{0:X2}",b);}returnret.ToString();}catch{return"error";}}///<summary>///DES解密数据///</summary>///<paramname="Text"></param>///<paramname="sKey"></param>///<returns></returns>publicstaticstringDESDecrypt(stringText,stringsKey=null){sKey=sKey??"zhiqiang";try{System.Security.Cryptography.DESCryptoServiceProviderdes=newSystem.Security.Cryptography.DESCryptoServiceProvider();intlen;len=Text.Length/2;byte[]inputByteArray=newbyte[len];intx,i;for(x=0;x<len;x++){i=Convert.ToInt32(Text.Substring(x*2,2),16);inputByteArray[x]=(byte)i;}stringmd5SKey=Get32MD5One(sKey).Substring(0,8);des.Key=System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey);des.IV=System.Text.ASCIIEncoding.ASCII.GetBytes(md5SKey);System.IO.MemoryStreamms=newSystem.IO.MemoryStream();System.Security.Cryptography.CryptoStreamcs=newSystem.Security.Cryptography.CryptoStream(ms,des.CreateDecryptor(),System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(inputByteArray,0,inputByteArray.Length);cs.FlushFinalBlock();returnSystem.Text.Encoding.Default.GetString(ms.ToArray());}catch{return"error";}}///<summary>///RSA加密数据///</summary>///<paramname="express"></param>///<paramname="sKey"></param>///<returns></returns>publicstaticstringRSAEncryption(stringexpress,stringKeyContainerName=null){System.Security.Cryptography.CspParametersparam=newSystem.Security.Cryptography.CspParameters();param.KeyContainerName=KeyContainerName??"zhiqiang";//密匙容器的名称,保持加密解密一致才能解密成功using(System.Security.Cryptography.RSACryptoServiceProviderrsa=newSystem.Security.Cryptography.RSACryptoServiceProvider(param)){byte[]plaindata=System.Text.Encoding.Default.GetBytes(express);//将要加密的字符串转换为字节数组byte[]encryptdata=rsa.Encrypt(plaindata,false);//将加密后的字节数据转换为新的加密字节数组returnConvert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串}}///<summary>///RSA解密数据///</summary>///<paramname="express"></param>///<paramname="sKey"></param>///<returns></returns>publicstaticstringRSADecrypt(stringciphertext,stringKeyContainerName=null){System.Security.Cryptography.CspParametersparam=newSystem.Security.Cryptography.CspParameters();param.KeyContainerName=KeyContainerName??"zhiqiang";using(System.Security.Cryptography.RSACryptoServiceProviderrsa=newSystem.Security.Cryptography.RSACryptoServiceProvider(param)){byte[]encryptdata=Convert.FromBase64String(ciphertext);byte[]decryptdata=rsa.Decrypt(encryptdata,false);returnSystem.Text.Encoding.Default.GetString(decryptdata);}}}

以上就是C#中的常见加密解密类的具体内容,代码详细清楚,如果在日常工作遇到这个问题,希望你能通过这篇文章解决问题。如果想了解更多相关内容,欢迎关注亿速云行业资讯频道!