zoukankan      html  css  js  c++  java
  • (转)C# DES

       本文原地址:http://blog.csdn.net/zhoufoxcn/article/details/1497095 作者:周公

    using System;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;

    namespace Common
    {
        
    /// <summary>
        
    /// DESEncrypt加密解密算法。
        
    /// </summary>

        public sealed class DESEncrypt
        
    {
            
    private DESEncrypt()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
            }


            
    private static string key = "zhoufoxcn";

            
    /// <summary>
            
    /// 对称加密解密的密钥
            
    /// </summary>

            public static string Key
            
    {
                
    get
                
    {
                    
    return key;
                }

                
    set
                
    {
                    key 
    = value;
                }

            }


            
    /// <summary>
            
    /// DES加密
            
    /// </summary>
            
    /// <param name="encryptString"></param>
            
    /// <returns></returns>

            public static string DesEncrypt(string encryptString)
            
    {
                
    byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(08));
                
    byte[] keyIV = keyBytes;
                
    byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider provider 
    = new DESCryptoServiceProvider();
                MemoryStream mStream 
    = new MemoryStream();
                CryptoStream cStream 
    = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 
    0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                
    return Convert.ToBase64String(mStream.ToArray());
            }


            
    /// <summary>
            
    /// DES解密
            
    /// </summary>
            
    /// <param name="decryptString"></param>
            
    /// <returns></returns>

            public static string DesDecrypt(string decryptString)
            
    {
                
    byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(08));
                
    byte[] keyIV = keyBytes;
                
    byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider provider 
    = new DESCryptoServiceProvider();
                MemoryStream mStream 
    = new MemoryStream();
                CryptoStream cStream 
    = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 
    0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                
    return Encoding.UTF8.GetString(mStream.ToArray());
            }

        }

    }
  • 相关阅读:
    xcode创建多个target,老外的写的懒得翻译了,看图
    错误 解决“Unknown class in Interface Builder file”
    Objectc 动态调用函数
    IPA PNG图片转换
    ObjectiveC的消息传递机制[转]
    [转]获取iOS设备的内存状况
    [转]史上最简单得多选uitableview
    Unity3d 添加IOS View(2)
    Core Animation学习笔记五:CAPropertyAnimation
    MKMapView指定坐标添加大头针
  • 原文地址:https://www.cnblogs.com/hhhh2010/p/3666396.html
Copyright © 2011-2022 走看看