zoukankan      html  css  js  c++  java
  • 一个对称加密、解密的方法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;
            }
    
    
        }
    }
  • 相关阅读:
    Centos 安装Apache软件
    简单工厂模式和策略模式的差别
    ASCII对比表
    谋哥:这个时代没有比程序猿更适合创业
    Android 自己主动化測试(3)&lt;monkeyrunner&gt; 依据ID查找对象&amp;touch&amp;type (python)
    Java学习笔记七(目录操作)
    我读经典(7):读《程序猿生存定律》有感
    SELECT语句逻辑运行顺序,你知道吗?
    Qt Creator新安装后运行一个程序后,出现错误:Error while building/deploying project dict-qt (kit: Desktop Qt 5.10.0 MinGW 32bit) When executing step "qmake"
    软件的各版本分类介绍
  • 原文地址:https://www.cnblogs.com/gc2013/p/3682997.html
Copyright © 2011-2022 走看看