using System;
using SystemIO;
using SystemSecurityCryptography;
namespace Vavic
{
/// <summary>
/// Security 的摘要说明
/// </summary>
public class Security
{
const string KEY_ = VavicApp;
const string IV_ = VavicApp; //注意了是个字符位
public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string Encode(string data)
{
byte[] byKey = SystemTextASCIIEncodingASCIIGetBytes(KEY_);
byte[] byIV = SystemTextASCIIEncodingASCIIGetBytes(IV_);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProviderKeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(mscryptoProviderCreateEncryptor(byKeybyIV)CryptoStreamModeWrite);
StreamWriter sw = new StreamWriter(cst);
swWrite(data);
swFlush();
cstFlushFinalBlock();
swFlush();
return ConvertToBaseString(msGetBuffer()(int)msLength);
}
public static string Decode(string data)
{
byte[] byKey = SystemTextASCIIEncodingASCIIGetBytes(KEY_);
byte[] byIV = SystemTextASCIIEncodingASCIIGetBytes(IV_);
byte[] byEnc;
try
{
byEnc = ConvertFromBaseString(data);
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(mscryptoProviderCreateDecryptor(byKeybyIV)CryptoStreamModeRead);
StreamReader sr = new StreamReader(cst);
return srReadToEnd();
}
}
}