马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

C#/ASP.NET中对称加密解密的方法

[复制链接]
查看3342 | 回复2 | 2016-5-19 09:13:59 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace CMD.EDI
{
    public class EncryptHandler
    {
        /// <summary>
        /// 加密字符串
        /// </summary>
        public static string Encrypt(string password, string cleartext)
        {
            string password2 = "Ahbool";
            string cipher;
            char[] key = new char[8];
            if (password.Length > 8)
            {
                password = password.Remove(8);
            }
            password.CopyTo(0, key, 0, password.Length);
            char[] iv = new char[8];
            if (password2.Length > 8)
            {
                password2 = password2.Remove(8);
            }
            password2.CopyTo(0, iv, 0, password2.Length);
            if (cleartext == null)
            {
                return string.Empty;
            }
            SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
            serviceProvider.Key = Encoding.ASCII.GetBytes(key);
            serviceProvider.IV = Encoding.ASCII.GetBytes(iv);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateEncryptor(), CryptoStreamMode.Write);
            StreamWriter streamWriter = new StreamWriter(cryptoStream);
            streamWriter.Write(cleartext);
            streamWriter.Dispose();
            cryptoStream.Dispose();
            byte[] signData = memoryStream.ToArray();
            memoryStream.Dispose();
            serviceProvider.Clear();
            cipher = Convert.ToBase64String(signData);
            return cipher;

        }

 

        /// <summary>
        /// 解密字符串
        /// </summary>
        public static string Decrypt(string password, string ciphertext)
        {
            string password2 = "Ahbool";
            string cipher = string.Empty;
            try
            {
                char[] key = new char[8];
                if (password.Length > 8)
                {
                    password = password.Remove(8);
                }
                password.CopyTo(0, key, 0, password.Length);
                char[] iv = new char[8];
                if (password2.Length > 8)
                {
                    password2 = password2.Remove(8);
                }
                password2.CopyTo(0, iv, 0, password2.Length);
                if (ciphertext == null)
                {
                    return cipher;
                }

                SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
                serviceProvider.Key = Encoding.ASCII.GetBytes(key);
                serviceProvider.IV = Encoding.ASCII.GetBytes(iv);
                byte[] contentArray = Convert.FromBase64String(ciphertext);
                MemoryStream memoryStream = new MemoryStream(contentArray);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateDecryptor(), CryptoStreamMode.Read);
                StreamReader streamReader = new StreamReader(cryptoStream);
                cipher = streamReader.ReadToEnd();
                streamReader.Dispose();
                cryptoStream.Dispose();
                memoryStream.Dispose();
                serviceProvider.Clear();
            }
            catch (Exception ex)
            {
                throw new SystemException("密钥错误,数据包解密失败.");
            }            return cipher;
        }
    }

}


C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2016-5-19 09:19:44 | 显示全部楼层
1
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
meng | 2016-5-19 09:20:40 | 显示全部楼层
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则