usingSystem;
usingSystem.Security.Cryptography;
usingSystem.Text;
usingSystem.IO;
namespaceCommon
...{
/**////<summary>
///DESEncrypt加密解密算法。
///</summary>
publicsealedclassDESEncrypt
...{
privateDESEncrypt()
...{
//
//TODO:在此处添加构造函数逻辑
//
}
privatestaticstringkey="zhoufoxcn";

/**////<summary>
///对称加密解密的密钥
///</summary>
publicstaticstringKey
...{
get
...{
returnkey;
}
set
...{
key=value;
}
}

/**////<summary>
///DES加密
///</summary>
///<paramname="encryptString"></param>
///<returns></returns>
publicstaticstringDesEncrypt(stringencryptString)
...{
byte[]keyBytes=Encoding.UTF8.GetBytes(key.Substring(0,8));
byte[]keyIV=keyBytes;
byte[]inputByteArray=Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProviderprovider=newDESCryptoServiceProvider();
MemoryStreammStream=newMemoryStream();
CryptoStreamcStream=newCryptoStream(mStream,provider.CreateEncryptor(keyBytes,keyIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
returnConvert.ToBase64String(mStream.ToArray());
}

/**////<summary>
///DES解密
///</summary>
///<paramname="decryptString"></param>
///<returns></returns>
publicstaticstringDesDecrypt(stringdecryptString)
...{
byte[]keyBytes=Encoding.UTF8.GetBytes(key.Substring(0,8));
byte[]keyIV=keyBytes;
byte[]inputByteArray=Convert.FromBase64String(decryptString);
DESCryptoServiceProviderprovider=newDESCryptoServiceProvider();
MemoryStreammStream=newMemoryStream();
CryptoStreamcStream=newCryptoStream(mStream,provider.CreateDecryptor(keyBytes,keyIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
returnEncoding.UTF8.GetString(mStream.ToArray());
}
}
}
本文介绍了一个使用 C# 实现的 DES 对称加密解密算法,包括加密和解密的具体实现过程。该算法利用了 .NET Framework 中提供的 System.Security.Cryptography 命名空间来完成数据加密和解密操作。
1006

被折叠的 条评论
为什么被折叠?



