C#的最实用的的字符串加密解密方法大全


using System ;
using
System . Data ;
using
System . Configuration ;
using
System . Web ;
using
System . Web . Security ;
using
System . Web . UI ;
using
System . Web . UI . WebControls ;
using
System . Web . UI . WebControls . WebParts ;
using
System . Web . UI . HtmlControls ;
using
System . IO ;
using
System . Security . Cryptography ;
using
System . Text ;

namespace sql_function
{

   
/// <summary>
   
/// 所有加密方法
   
/// 定义字符串加密接口 [MD5加密,SHA1加密,字符串加密]    前台只需定这样定义:
   
/// sql_function.InterFaceStringEncryptionDecryption QueryStringEncodeCodeAndDncodeCode =new sql_function.StringEncryptionDecryption();
   
/// 说明:其中sql_function代表命名空间名称 InterFaceStringEncryptionDecryption代表接口名称   StringEncryptionDecryption代表类名
   
/// </summary>
   
public interface InterFaceStringEncryptionDecryption
   
{

       
#region (1) QueryString加密与解密 开始

       
/// <summary>
       
/// QueryString加密
       
/// </summary>
       
/// <param name="StringSQL"></param>
       
/// <returns></returns>

       
string QueryStringEncodeCode ( string StringSQL );
       
       
/// <summary>
       
/// QueryString解密
       
/// </summary>
       
/// <param name="StringSQL"></param>
       
/// <returns></returns>
       
string QueryStringDncodeCode ( string StringSQL );

       
#endregion


       
#region (2) Rijndael算法

       
/// <summary>
       
/// 加密方法
       
/// </summary>
       
/// <param name="Source">待加密的串</param>
       
/// <returns>经过加密的串</returns>
       
string RijndaelEncrypt ( string Source );

       
/// <summary>
       
/// 解密方法
       
/// </summary>
       
/// <param name="Source">待解密的串</param>
       
/// <returns>经过解密的串</returns>
       
string RijndaelDecrypt ( string Source );

       
#endregion (2) Rijndael算法


       
#region ( 3 ) Base64与UTF8混用

       
/// <summary>
       
/// 字符串加密
       
/// </summary>
       
/// <param name="bb"></param>
       
/// <returns></returns>
       
string BUEncrypt ( string bb );

       
/// <summary>
       
/// 字符串解密
       
/// </summary>
       
/// <param name="aa"></param>
       
/// <returns></returns>
       
string BUDecrypt ( string aa );

       
#endregion

       
#region ( 4 )固定密钥算法

       
/// <summary>
       
/// 字符串加密
       
/// </summary>
       
/// <param name="strText"></param>
       
/// <returns></returns>
       
string SKeyEncrypt ( string strText );


       
/// <summary>
       
/// 字符串解密
       
/// </summary>
       
/// <param name="strText"></param>
       
/// <returns></returns>
       
string SKeyDecrypt ( string strText );

       
#endregion


       
#region ( 5 )DES算法

       
/// <summary>
       
/// DES加密
       
/// </summary>
       
/// <param name="strSource"></param>
       
/// <returns></returns>
       
string DESEncrypt ( string strSource );

       
/// <summary>
       
/// DES解密
       
/// </summary>
       
/// <param name="strSource"></param>
       
/// <returns></returns>
       
string DESDecrypt ( string strSource );


       
#endregion


       
#region ( 6 )   加密密码MD5T和SHA1

       
/// <summary>
       
/// 加密密码MD5T和SHA1
       
/// </summary>
       
/// <param name="strSource">字符串</param>
       
/// <param name="strFlag">加密类别</param>
       
/// <param name="substringlen">加密长度</param>
       
/// <returns></returns>
       
string encrypting ( string strSource , int strFlag , int substringlen );

       
#endregion
   
}
   
   
/// <summary>
   
/// 定义接口类 加密的类的方法
   
/// </summary>
   
public class StringEncryptionDecryption : InterFaceStringEncryptionDecryption
   
{

       
#region (1) QueryString加密与解密 开始

       
/// <summary>
       
/// QueryString加密
       
/// </summary>
       
/// <param name="code"></param>
       
/// <returns></returns>
       
public string QueryStringEncodeCode ( string code )
       
{
           
string result = "" ;
           
if ( code == null || code == "" )
           
{
                result
= "" ;
           
}
           
else
           
{
                result
= Convert . ToBase64String ( System . Text . Encoding . Default . GetBytes ( "" + code + "" )). Replace ( "+" , "%2B" );

           
}
           
return result ;
       
}

       
/// <summary>
       
/// QueryString解密
       
/// </summary>
       
/// <param name="code"></param>
       
/// <returns></returns>
       
public string QueryStringDncodeCode ( string code )
       
{
           
string result = "" ;

           
if ( code == null || code == "" )
           
{
                result
= "" ;
           
}
           
else
           
{
               
try
               
{
                    result
= System . Text . Encoding . Default . GetString ( Convert . FromBase64String ( code . Replace ( "%2B" , "+" )));
               
}
               
catch ( FormatException ex ) ///抛出异常 [错误信息“Base-64字符数组的无效长度”]
               
{
                    result
= "0" ;
               
}
           
}
           
return result ;
       
}


       
#endregion (1) QueryString加密与解密 结束


       
#region ( 2 ) Rijndael算法
       
private static SymmetricAlgorithm mobjCryptoService = new RijndaelManaged ();

       
private static string Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7" ;

       
/// <summary>
       
/// 获得密钥
       
/// </summary>
       
/// <returns>密钥</returns>
       
private byte [] GetLegalKey ()
       
{
           
string sTemp = Key ;
            mobjCryptoService
. GenerateKey ();
           
byte [] bytTemp = mobjCryptoService . Key ;
           
int KeyLength = bytTemp . Length ;
           
if ( sTemp . Length > KeyLength )
                sTemp
= sTemp . Substring ( 0 , KeyLength );
           
else if ( sTemp . Length < KeyLength )
                sTemp
= sTemp . PadRight ( KeyLength , ' ' );
           
return ASCIIEncoding . ASCII . GetBytes ( sTemp );
       
}
       
/// <summary>
       
/// 获得初始向量IV
       
/// </summary>
       
/// <returns>初试向量IV</returns>
       
private byte [] GetLegalIV ()
       
{
           
string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk" ;
            mobjCryptoService
. GenerateIV ();
           
byte [] bytTemp = mobjCryptoService . IV ;
           
int IVLength = bytTemp . Length ;
           
if ( sTemp . Length > IVLength )
                sTemp
= sTemp . Substring ( 0 , IVLength );
           
else if ( sTemp . Length < IVLength )
                sTemp
= sTemp . PadRight ( IVLength , ' ' );
           
return ASCIIEncoding . ASCII . GetBytes ( sTemp );
       
}
       
/// <summary>
       
/// 加密方法
       
/// </summary>
       
/// <param name="Source">待加密的串</param>
       
/// <returns>经过加密的串</returns>
       
public string RijndaelEncrypt ( string Source )
       
{
           
byte [] bytIn = UTF8Encoding . UTF8 . GetBytes ( Source );
           
MemoryStream ms = new MemoryStream ();
            mobjCryptoService
. Key = GetLegalKey ();
            mobjCryptoService
. IV = GetLegalIV ();
           
ICryptoTransform encrypto = mobjCryptoService . CreateEncryptor ();
           
CryptoStream cs = new CryptoStream ( ms , encrypto , CryptoStreamMode . Write );
            cs
. Write ( bytIn , 0 , bytIn . Length );
            cs
. FlushFinalBlock ();
            ms
. Close ();
           
byte [] bytOut = ms . ToArray ();
           
return Convert . ToBase64String ( bytOut );
       
}

       
/// <summary>
       
/// 解密方法
       
/// </summary>
       
/// <param name="Source">待解密的串</param>
       
/// <returns>经过解密的串</returns>
       
public string RijndaelDecrypt ( string Source )
       
{
           
try
           
{
               
byte [] bytIn = Convert . FromBase64String ( Source );
               
MemoryStream ms = new MemoryStream ( bytIn , 0 , bytIn . Length );
                mobjCryptoService
. Key = GetLegalKey ();
                mobjCryptoService
. IV = GetLegalIV ();
               
ICryptoTransform encrypto = mobjCryptoService . CreateDecryptor ();
               
CryptoStream cs = new CryptoStream ( ms , encrypto , CryptoStreamMode . Read );
               
StreamReader sr = new StreamReader ( cs );
               
return sr . ReadToEnd ();
           
}
           
catch ( Exception ex )
           
{
               
return ex . Message ;
           
}
       
}
       
#endregion


       
#region ( 3 )Base64与UTF8混用

       
//字符串加密
       
public string BUEncrypt ( string bb )
       
{
           
byte [] by = new byte [ bb . Length ];
           
by = System . Text . Encoding . UTF8 . GetBytes ( bb );

           
string r = Convert . ToBase64String ( by );
           
return r ;
       
}

       
//字符串解密
       
public string BUDecrypt ( string aa )
       
{
           
try
           
{
               
byte [] by = Convert . FromBase64String ( aa );

               
string r = Encoding . UTF8 . GetString ( by );

               
return r ;
           
}
           
catch ( Exception ex )
           
{
               
return ex . Message ;
           
}
       
}

       
#endregion


       
#region ( 4 )固定密钥算法

       
public static Byte [] Iv64 ={ 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 };

       
public static Byte [] byKey64 ={ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 };
       
//字符串加密
       
public string SKeyEncrypt ( string strText )
       
{
           
try
           
{
               
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
               
Byte [] inputByteArray = Encoding . UTF8 . GetBytes ( strText );
               
MemoryStream ms = new MemoryStream ();
               
CryptoStream cs = new CryptoStream ( ms , des . CreateEncryptor ( byKey64 , Iv64 ), CryptoStreamMode . Write );
                cs
. Write ( inputByteArray , 0 , inputByteArray . Length );
                cs
. FlushFinalBlock ();
               
return Convert . ToBase64String ( ms . ToArray ());
           
}
           
catch ( Exception ex )
           
{
               
return ex . Message ;
           
}
       
}

       
//字符串解密
       
public string SKeyDecrypt ( string strText )
       
{
           
Byte [] inputByteArray = new byte [ strText . Length ];
           
try
           
{
               
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
                inputByteArray
= Convert . FromBase64String ( strText );
               
MemoryStream ms = new MemoryStream ();
               
CryptoStream cs = new CryptoStream ( ms , des . CreateDecryptor ( byKey64 , Iv64 ), CryptoStreamMode . Write );
                cs
. Write ( inputByteArray , 0 , inputByteArray . Length );
                cs
. FlushFinalBlock ();
               
System . Text . Encoding encoding = System . Text . Encoding . UTF8 ;
               
return encoding . GetString ( ms . ToArray ());
           
}
           
catch ( Exception ex )
           
{
               
return ex . Message ;
           
}
       
}
       
#endregion


       
#region ( 5 )DES算法

       
public static byte [] DESKey = new byte [] { 0x82 , 0xBC , 0xA1 , 0x6A , 0xF5 , 0x87 , 0x3B , 0xE6 , 0x59 , 0x6A , 0x32 , 0x64 , 0x7F , 0x3A , 0x2A , 0xBB , 0x2B , 0x68 , 0xE2 , 0x5F , 0x06 , 0xFB , 0xB8 , 0x2D , 0x67 , 0xB3 , 0x55 , 0x19 , 0x4E , 0xB8 , 0xBF , 0xDD };
       
/// <summary>
       
/// DES加密
       
/// </summary>
       
/// <param name="strSource">待加密字串</param>
       
/// <param name="key">32位Key值</param>
       
/// <returns>加密后的字符串</returns>
       
public string DESEncrypt ( string strSource )
       
{
           
return DESEncryptF ( strSource , DESKey );
       
}

       
private string DESEncryptF ( string strSource , byte [] key )
       
{
           
SymmetricAlgorithm sa = Rijndael . Create ();
            sa
. Key = key ;
            sa
. Mode = CipherMode . ECB ;
            sa
. Padding = PaddingMode . Zeros ;
           
MemoryStream ms = new MemoryStream ();
           
CryptoStream cs = new CryptoStream ( ms , sa . CreateEncryptor (), CryptoStreamMode . Write );
           
byte [] byt = Encoding . Unicode . GetBytes ( strSource );
            cs
. Write ( byt , 0 , byt . Length );
            cs
. FlushFinalBlock ();
            cs
. Close ();
           
return Convert . ToBase64String ( ms . ToArray ());
       
}

       
/// <summary>
       
/// DES解密
       
/// </summary>
       
/// <param name="strSource">待解密的字串</param>
       
/// <param name="key">32位Key值</param>
       
/// <returns>解密后的字符串</returns>
       
public string DESDecrypt ( string strSource )
       
{
           
return DESDecryptF ( strSource , DESKey );
       
}

       
private string DESDecryptF ( string strSource , byte [] key )
       
{
           
try
           
{
               
SymmetricAlgorithm sa = Rijndael . Create ();
                sa
. Key = key ;
                sa
. Mode = CipherMode . ECB ;
                sa
. Padding = PaddingMode . Zeros ;
               
ICryptoTransform ct = sa . CreateDecryptor ();
               
byte [] byt = Convert . FromBase64String ( strSource );
               
MemoryStream ms = new MemoryStream ( byt );
               
CryptoStream cs = new CryptoStream ( ms , ct , CryptoStreamMode . Read );
               
StreamReader sr = new StreamReader ( cs , Encoding . Unicode );
               
return sr . ReadToEnd ();
           
}
           
catch ( Exception ex )
           
{
               
return ex . Message ;
           
}
       
}
       
#endregion


       
#region ( 6 )   加密密码MD5T和SHA1

       
/// <summary>
       
/// 加密密码MD5T和SHA1
       
/// </summary>
       
/// <param name="strSource">字符串</param>
       
/// <param name="strFlag">加密类别</param>
       
/// <param name="substringlen">加密长度</param>
       
/// <returns></returns>
       
///
       
public string encrypting ( string strSource , int strFlag , int substringlen )
       
{
           
string ss = "" ;
           
if ( strFlag == 1 ) ///MD5加密
           
{
               
if ( substringlen == 16 ) //16位MD5加密(取32位加密的9~25字符)
               
{
                    ss
= FormsAuthentication . HashPasswordForStoringInConfigFile ( strSource , "MD5" ). ToLower (). Substring ( 8 , 16 );
               
}
               
else if ( substringlen == 32 ) //32位加密
               
{
                    ss
= FormsAuthentication . HashPasswordForStoringInConfigFile ( strSource , "MD5" ). ToLower ();
               
}
           
}
           
else if ( strFlag == 2 ) ///SHA1加密
           
{
               
if ( substringlen == 16 ) //SHA1 16位加密
               
{
                    ss
= FormsAuthentication . HashPasswordForStoringInConfigFile ( strSource , "SHA1" ). ToLower (). Substring ( 8 , 16 );
               
}
               
else if ( substringlen == 32 ) //SHA1 40位加密
               
{
                    ss
= FormsAuthentication . HashPasswordForStoringInConfigFile ( strSource , "SHA1" ). ToLower ();
               
}
           
}
           
else
           
{
                ss
= "" ;
           
}
           
return ss ;
       
}

       
#endregion
   
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 中,可以使用许多不同的加密算法来加密和解密字符串。这里介绍两种常用的加密算法:AES 和 RSA。 AES 加密和解密字符串的代码示例: ```csharp using System; using System.IO; using System.Security.Cryptography; using System.Text; class AesExample { static void Main() { string original = "Hello World!"; byte[] key = new byte[32]; byte[] iv = new byte[16]; using (Aes aes = Aes.Create()) { aes.Key = key; aes.IV = iv; // Encrypt the string to an array of bytes. byte[] encrypted = EncryptStringToBytes_Aes(original, aes.Key, aes.IV); // Decrypt the bytes to a string. string roundtrip = DecryptStringFromBytes_Aes(encrypted, aes.Key, aes.IV); //Display the original data and the decrypted data. Console.WriteLine("Original: {0}", original); Console.WriteLine("Round Trip: {0}", roundtrip); } } static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV) { // Check arguments. if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); byte[] encrypted; // Create an Aes object // with the specified key and IV. using (Aes aes = Aes.Create()) { aes.Key = Key; aes.IV = IV; // Create an encryptor to perform the stream transform. ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); // Create the streams used for encryption. using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } // Return the encrypted bytes from the memory stream. return encrypted; } static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV) { // Check arguments. if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0) throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0) throw new ArgumentNullException("IV"); // Declare the string used to hold // the decrypted text. string plaintext = null; // Create an Aes object // with the specified key and IV. using (Aes aes = Aes.Create()) { aes.Key = Key; aes.IV = IV; // Create a decryptor to perform the stream transform. ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); // Create the streams used for decryption. using (MemoryStream msDecrypt = new MemoryStream(cipherText)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // Read the decrypted bytes from the decrypting stream // and place them in a string. plaintext = srDecrypt.ReadToEnd(); } } } } return plaintext; } } ``` RSA 加密和解密字符串的代码示例: ```csharp using System; using System.Security.Cryptography; using System.Text; class RSACSPSample { static void Main() { // Create a new instance of RSACryptoServiceProvider RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //Export the key information to an RSAParameters object. //Pass false to export the public key information or pass //true to export public and private key information. RSAParameters RSAParams = rsa.ExportParameters(false); //Create some data to sign and verify. byte[] data = Encoding.UTF8.GetBytes("Data to Sign"); try { //Sign the data using the private key. byte[] signature = SignData(data, RSAParams); //Verify the data using the public key. bool verified = VerifyData(data, signature, RSAParams); Console.WriteLine("Data verified: {0}", verified); } catch (CryptographicException e) { Console.WriteLine(e.Message); } } static byte[] SignData(byte[] data, RSAParameters RSAParams) { try { //Create a new instance of RSACryptoServiceProvider. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { //Import the RSA key information. rsa.ImportParameters(RSAParams); //Sign the data. return rsa.SignData(data, SHA256.Create()); } } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } } static bool VerifyData(byte[] data, byte[] signature, RSAParameters RSAParams) { try { //Create a new instance of RSACryptoServiceProvider. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { //Import the RSA key information. rsa.ImportParameters(RSAParams); //Verify the data. return rsa.VerifyData(data, SHA256.Create(), signature); } } catch (CryptographicException e) { Console.WriteLine(e.Message); return false; } } } ``` 请注意,这些示例代码只是演示如何使用 AES 和 RSA 加密和解密字符串,并不一定适用于所有情况。在实际应用中,需要根据具体需求选择适当的加密算法和实现方式,并且需要采取额外的安全措施来保护加密密钥等敏感信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值