c#加密类使用方法示例

c#加密类使用方法示例

using System;using System.IO;using System.Text;using System.Security.Cryptography;using System.Web;namespace Encryption.App_Code{    ///     /// 加密码类    ///     public class Encryption    {        ///         /// 加密        ///         ///         ///         public static string DesEncrypt(string inputString)        {            return DesEncrypt(inputString, Key);        }        ///         /// 解密        ///         ///         ///         public static string DesDecrypt(string inputString)        {            return DesDecrypt(inputString, Key);        }        ///         /// 密匙        ///         private static string Key        {            get            {                return "hongye10";            }        }        ///         /// 加密字符串        /// 注意:密钥必须为8位        ///         /// 字符串        /// 密钥        /// 返回加密后的字符串        public static string DesEncrypt(string inputString, string encryptKey)        {            byte[] byKey = null;            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };            try            {                byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));                DESCryptoServiceProvider des = new DESCryptoServiceProvider();                byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);                MemoryStream ms = new MemoryStream();                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);                cs.Write(inputByteArray, 0, inputByteArray.Length);                cs.FlushFinalBlock();                return Convert.ToBase64String(ms.ToArray());            }            catch (System.Exception error)            {                //return error.Message;                return null;            }        }        ///         /// 解密字符串        ///         /// 加了密的字符串        /// 密钥        /// 返回解密后的字符串        public static string DesDecrypt(string inputString, string decryptKey)        {            byte[] byKey = null;            byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };            byte[] inputByteArray = new Byte[inputString.Length];            try            {                byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));                DESCryptoServiceProvider des = new DESCryptoServiceProvider();                inputByteArray = Convert.FromBase64String(inputString);                MemoryStream ms = new MemoryStream();                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);                cs.Write(inputByteArray, 0, inputByteArray.Length);                cs.FlushFinalBlock();                System.Text.Encoding encoding = new System.Text.UTF8Encoding();                return encoding.GetString(ms.ToArray());            }            catch (System.Exception error)            {                //return error.Message;                return null;            }        }    }}

以上就是c#加密类使用方法示例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1431933.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 05:57:08
下一篇 2025年12月17日 05:57:14

相关推荐

发表回复

登录后才能评论
关注微信