C#使用DES和AES实现加密解密功能示例
程序员文章站
2023-11-24 13:05:04
本文实例讲述了c#使用des和aes实现加密解密功能。分享给大家供大家参考,具体如下:
using system;
using system.text;
us...
本文实例讲述了c#使用des和aes实现加密解密功能。分享给大家供大家参考,具体如下:
using system; using system.text; using system.security.cryptography; using system.io; namespace mycryptography { /// <summary> /// des加密解密 /// </summary> public class des { /// <summary> /// 获取密钥 /// </summary> private static string key { get { return @"p@+#wg+z"; } } /// <summary> /// 获取向量 /// </summary> private static string iv { get { return @"l%n67}g\mk@k%:~y"; } } /// <summary> /// des加密 /// </summary> /// <param name="plainstr">明文字符串</param> /// <returns>密文</returns> public static string desencrypt(string plainstr) { byte[] bkey = encoding.utf8.getbytes(key); byte[] biv = encoding.utf8.getbytes(iv); byte[] bytearray = encoding.utf8.getbytes(plainstr); string encrypt = null; descryptoserviceprovider des = new descryptoserviceprovider(); try { using (memorystream mstream = new memorystream()) { using (cryptostream cstream = new cryptostream(mstream, des.createencryptor(bkey, biv), cryptostreammode.write)) { cstream.write(bytearray, 0, bytearray.length); cstream.flushfinalblock(); encrypt = convert.tobase64string(mstream.toarray()); } } } catch { } des.clear(); return encrypt; } /// <summary> /// des解密 /// </summary> /// <param name="encryptstr">密文字符串</param> /// <returns>明文</returns> public static string desdecrypt(string encryptstr) { byte[] bkey = encoding.utf8.getbytes(key); byte[] biv = encoding.utf8.getbytes(iv); byte[] bytearray = convert.frombase64string(encryptstr); string decrypt = null; descryptoserviceprovider des = new descryptoserviceprovider(); try { using (memorystream mstream = new memorystream()) { using (cryptostream cstream = new cryptostream(mstream, des.createdecryptor(bkey, biv), cryptostreammode.write)) { cstream.write(bytearray, 0, bytearray.length); cstream.flushfinalblock(); decrypt = encoding.utf8.getstring(mstream.toarray()); } } } catch { } des.clear(); return decrypt; } } /// <summary> /// aes加密解密 /// </summary> public class aes { /// <summary> /// 获取密钥 /// </summary> private static string key { get { return @")o[nb]6,yf}+efcaj{+oesb9d8>z'e9m"; } } /// <summary> /// 获取向量 /// </summary> private static string iv { get { return @"l+\~f4,ir)b$=pkf"; } } /// <summary> /// aes加密 /// </summary> /// <param name="plainstr">明文字符串</param> /// <returns>密文</returns> public static string aesencrypt(string plainstr) { byte[] bkey = encoding.utf8.getbytes(key); byte[] biv = encoding.utf8.getbytes(iv); byte[] bytearray = encoding.utf8.getbytes(plainstr); string encrypt = null; rijndael aes = rijndael.create(); try { using (memorystream mstream = new memorystream()) { using (cryptostream cstream = new cryptostream(mstream, aes.createencryptor(bkey, biv), cryptostreammode.write)) { cstream.write(bytearray, 0, bytearray.length); cstream.flushfinalblock(); encrypt = convert.tobase64string(mstream.toarray()); } } } catch { } aes.clear(); return encrypt; } /// <summary> /// aes加密 /// </summary> /// <param name="plainstr">明文字符串</param> /// <param name="returnnull">加密失败时是否返回 null,false 返回 string.empty</param> /// <returns>密文</returns> public static string aesencrypt(string plainstr, bool returnnull) { string encrypt = aesencrypt(plainstr); return returnnull ? encrypt : (encrypt == null ? string.empty : encrypt); } /// <summary> /// aes解密 /// </summary> /// <param name="encryptstr">密文字符串</param> /// <returns>明文</returns> public static string aesdecrypt(string encryptstr) { byte[] bkey = encoding.utf8.getbytes(key); byte[] biv = encoding.utf8.getbytes(iv); byte[] bytearray = convert.frombase64string(encryptstr); string decrypt = null; rijndael aes = rijndael.create(); try { using (memorystream mstream = new memorystream()) { using (cryptostream cstream = new cryptostream(mstream, aes.createdecryptor(bkey, biv), cryptostreammode.write)) { cstream.write(bytearray, 0, bytearray.length); cstream.flushfinalblock(); decrypt = encoding.utf8.getstring(mstream.toarray()); } } } catch { } aes.clear(); return decrypt; } /// <summary> /// aes解密 /// </summary> /// <param name="encryptstr">密文字符串</param> /// <param name="returnnull">解密失败时是否返回 null,false 返回 string.empty</param> /// <returns>明文</returns> public static string aesdecrypt(string encryptstr, bool returnnull) { string decrypt = aesdecrypt(encryptstr); return returnnull ? decrypt : (decrypt == null ? string.empty : decrypt); } } }
ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含aes、des、rc4等):
md5在线加密工具:
http://tools.jb51.net/password/createmd5password
在线散列/哈希算法加密工具:
在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
在线sha1/sha224/sha256/sha384/sha512加密工具:
更多关于c#相关内容还可查看本站专题:《c#加密与解密算法与技巧总结》、《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程》
希望本文所述对大家c#程序设计有所帮助。