对C#加密类的使用实例
复制代码代码如下所示:使用系统;
使用System.IO;
使用系统文本;
使用system.security.cryptography;
使用系统;
命名空间encryption.app_code
{
X
只需添加一个密码类
X
加密的公共课
{
X
公钥加密
X
X
X
字符串(字符串desencrypt InputString)
{
返回desencrypt(InputString,关键);
}
X
公开解密
X
X
X
字符串(字符串desdecrypt InputString)
{
返回DesDecrypt(InputString,关键);
}
X
公共密钥
X
专用静态字符串密钥
{
得到
{
返回hongye10 ;
}
}
X
公共字符串加密
注意:必须是8键
X
公共字符串
公共密钥
只返回加密字符串
字符串(字符串的字符串desencrypt InputString,encryptkey)
{
{ }重点= null字节;
{ } = { {四字节0x12,0x34、0x56,0x78,0x90,0xab,0xcd,0xef };
尝试
{
重点= system.text.encoding.utf8.getbytes(encryptkey.substring(0, 8));
descryptoserviceprovider DES =新descryptoserviceprovider();
字节{ } inputbytearray = encoding.utf8.getbytes(InputString);
凯迪网络最新MemoryStream();
cryptostream CS =新cryptostream(MS,des.createencryptor(重点,IV),CryptoStreamMode。写);
CS。写(inputbytearray,0,inputbytearray。长度);
Cs.FlushFinalBlock();
返回convert.tobase64string(ms.toarray());
}
抓住(system.exception错误)
{
返回错误。消息;
返回null;
}
}
X
公共字符串解密
X
只需添加加密字符串
公共密钥
总是在解密后返回一个字符串。
字符串(字符串的字符串desdecrypt InputString,decryptkey)
{
{ }重点= null字节;
{ } = { {四字节0x12,0x34、0x56,0x78,0x90,0xab,0xcd,0xef };
字节{ } inputbytearray =新的字节长度} { InputString;
尝试
{
重点= system.text.encoding.utf8.getbytes(decryptkey.substring(0, 8));
descryptoserviceprovider DES =新descryptoserviceprovider();
inputbytearray = convert.frombase64string(InputString);
凯迪网络最新MemoryStream();
cryptostream CS =新cryptostream(MS,des.createdecryptor(重点,IV),CryptoStreamMode。写);
CS。写(inputbytearray,0,inputbytearray。长度);
Cs.FlushFinalBlock();
system.text.encoding编码=新system.text.utf8encoding();
返回encoding.getstring(ms.toarray());
}
抓住(system.exception错误)
{
返回错误。消息;
返回null;
}
}
}
}