欢迎光临
我们一直在努力

一个可以用来加密/解密的类-.NET教程,安全和优化

建站超值云服务器,限时71元/月

可以用来加/解密数据库用户、密码等

using system;

using system.io;

using system.text;

using system.security.cryptography;

namespace common

{

/// <summary>

/// securityservice 的摘要说明。

/// </summary>

public class securityservice

{

static protected byte[] bytekey = {125, 14, 45, 67, 112, 79, 77, 99, 37, 104, 13, 9, 118, 51, 87, 108};

static protected byte[] byteiv = {86, 19, 79, 15, 72, 58, 117, 45};

static public string symmetricencrypt(string splaintext)

{

byte[] byteplaintext;

memorystream encryptedstream;

icryptotransform encryptor;

cryptostream thecryptostream;

if(splaintext=="") return "";

byteplaintext = encoding.ascii.getbytes(splaintext);

encryptedstream = new memorystream(splaintext.length);

encryptor = getencryptor();

thecryptostream = new cryptostream(encryptedstream, encryptor, cryptostreammode.write);

thecryptostream.write(byteplaintext, 0, byteplaintext.length);

thecryptostream.flushfinalblock();

thecryptostream.close();

return convert.tobase64string(encryptedstream.toarray());

}//end function

static public string symmetricdecrypt(string sencryptedtext)

{

byte[] byteencrypted;

memorystream plaintextstream;

icryptotransform decryptor;

cryptostream thecryptostream;

if (sencryptedtext == "") return "";

byteencrypted = convert.frombase64string(sencryptedtext.trim());

plaintextstream = new memorystream(sencryptedtext.length);

decryptor = getdecryptor();

thecryptostream = new cryptostream(plaintextstream, decryptor, cryptostreammode.write);

thecryptostream.write(byteencrypted, 0, byteencrypted.length);

thecryptostream.flushfinalblock();

thecryptostream.close();

return encoding.ascii.getstring(plaintextstream.toarray());

}//end function

static private icryptotransform getencryptor()

{

rc2cryptoserviceprovider cryptoprovider = new rc2cryptoserviceprovider();

cryptoprovider.mode = ciphermode.cbc;

return cryptoprovider.createencryptor(bytekey, byteiv);

}//end function

static private icryptotransform getdecryptor()

{

rc2cryptoserviceprovider cryptoprovider = new rc2cryptoserviceprovider();

cryptoprovider.mode = ciphermode.cbc;

return cryptoprovider.createdecryptor(bytekey, byteiv);

}//end function

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一个可以用来加密/解密的类-.NET教程,安全和优化
分享到: 更多 (0)