欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#加密类使用方法示例

程序员文章站 2024-02-26 21:36:10
复制代码 代码如下:using system;using system.io;using system.text;using system.security.cryptog...
复制代码 代码如下:

using system;
using system.io;
using system.text;
using system.security.cryptography;
using system.web;

namespace encryption.app_code
{
    /// <summary>
    /// 加密码类
    /// </summary>
    public class encryption
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="inputstring"></param>
        /// <returns></returns>
        public static string desencrypt(string inputstring)
        {
            return desencrypt(inputstring, key);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="inputstring"></param>
        /// <returns></returns>
        public static string desdecrypt(string inputstring)
        {
            return desdecrypt(inputstring, key);
        }
        /// <summary>
        /// 密匙
        /// </summary>
        private static string key
        {
            get
            {
                return "hongye10";
            }
        }
        /// <summary>
        /// 加密字符串
        /// 注意:密钥必须为8位
        /// </summary>
        /// <param name="strtext">字符串</param>
        /// <param name="encryptkey">密钥</param>
        /// <param name="encryptkey">返回加密后的字符串</param>
        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;
            }
        }
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="this.inputstring">加了密的字符串</param>
        /// <param name="decryptkey">密钥</param>
        /// <param name="decryptkey">返回解密后的字符串</param>
        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;
            }
        }
    }
}