c#

位置:IT落伍者 >> c# >> 浏览文章

小技巧:用C#实现Des加密和解密


发布日期:2021年01月11日
 
小技巧:用C#实现Des加密和解密

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();

}

}

}               

上一篇:C#给RichTextBox添加查找画面

下一篇:C# webservice调用方法总结