کد کردن و دی کد کردن رشته در C#
mrchsoft.com
Source
/// <summary>
/// کد کردن رشته ها
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Encrypt(this string str)
{
string strEncrKey = "?pws#m";
byte[] byKey;
byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// دی کد کردن رشته ها
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Decrypt(this string str)
{
string sDecrKey = "?pws#m";
byte[] byKey;
byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
byte[] inputByteArray;
// inputByteArray.Length = strText.Length;
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(str);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), 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;
}
}
Example
Encrypt:uN3ZxZKd2a4=
Decryp2:123