using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace FaibClass.Encrypting
{
public class BaseEncrypt
{
/**///// <summary>
/// 加密
/// </summary>
/// <param name="s">需加密的字串</param>
/// <param name="k">密钥</param>
/// <returns></returns>
public static string EncryptString(string s,string k)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
if(k.Length>8)
byKey = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
else
byKey = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(s);
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(System.Exception error)
{
throw new Exception(error.Message);
}
}
/**///// <summary>
/// 解密
/// </summary>
/// <param name="s">需解密的字串</param>
/// <param name="k">密钥</param>
/// <returns></returns>
public static string DecryptString(string s,string k)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[s.Length];
try
{
if(k.Length>8)
byKey = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
else
byKey = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(s);
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 = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
throw new Exception(error.Message);
}
}
}
}
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace FaibClass.Encrypting
{
public class BaseEncrypt
{
/**///// <summary>
/// 加密
/// </summary>
/// <param name="s">需加密的字串</param>
/// <param name="k">密钥</param>
/// <returns></returns>
public static string EncryptString(string s,string k)
{
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
if(k.Length>8)
byKey = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
else
byKey = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(s);
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(System.Exception error)
{
throw new Exception(error.Message);
}
}
/**///// <summary>
/// 解密
/// </summary>
/// <param name="s">需解密的字串</param>
/// <param name="k">密钥</param>
/// <returns></returns>
public static string DecryptString(string s,string k)
{
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[s.Length];
try
{
if(k.Length>8)
byKey = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
else
byKey = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(s);
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 = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch(System.Exception error)
{
throw new Exception(error.Message);
}
}
}
}