zoukankan      html  css  js  c++  java
  • 简单的加密与解密代码

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

    namespace FaibClass.Encrypting
    {

        
    public class BaseEncrypt
        {
            
    /**////<summary> 
            
    /// 加密
            
    /// </summary> 
            
    /// <param name="s">需加密的字串</param> 
            
    /// <param name="k">密钥</param> 
            
    /// <returns></returns> 
            public static string EncryptString(string s,string k)
            {
                
    byte[] byKey=null;
                
    byte[] IV= {0x120x340x560x780x900xAB0xCD0xEF};
                
    try
                {
                    
    if(k.Length>8)
                        byKey 
    = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
                    
    else
                        byKey 
    = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));
                    
                    DESCryptoServiceProvider des 
    = new DESCryptoServiceProvider();
                    
    byte[] inputByteArray = Encoding.UTF8.GetBytes(s);
                    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)
                {
                    
    throw new Exception(error.Message);
                }
            }

            
    /**////<summary> 
            
    /// 解密
            
    /// </summary> 
            
    /// <param name="s">需解密的字串</param> 
            
    /// <param name="k">密钥</param> 
            
    /// <returns></returns> 
            public static string DecryptString(string s,string k)
            {
                
    byte[] byKey = null;
                
    byte[] IV= {0x120x340x560x780x900xAB0xCD0xEF}; 
                
    byte[] inputByteArray = new Byte[s.Length];
                
    try
                {
                    
    if(k.Length>8)
                        byKey 
    = System.Text.Encoding.UTF8.GetBytes(k.Substring(0,8));
                    
    else
                        byKey 
    = System.Text.Encoding.UTF8.GetBytes((k+(new string('V',8-k.Length))));

                    DESCryptoServiceProvider des 
    = new DESCryptoServiceProvider();
                    inputByteArray 
    = Convert.FromBase64String(s);
                    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)
                {
                    
    throw new Exception(error.Message);
                }
            }
        }
    }

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    对ManualResetEvent和AutoResetEvent的巩固练习
    经纬度点距离的那点儿事
    【读书笔记】C++Primer---第三章
    .NET应用程序调试—原理、工具、方法
    【读书笔记】C++Primer---第二章
    【读书笔记】C++Primer---第一章
    8 个最好的 jQuery 树形 Tree 插件
    C++中引用(&)的用法和应用实例
    自娱自乐之直接插入排序
    自娱自乐之堆排序
  • 原文地址:https://www.cnblogs.com/Athrun/p/711520.html
Copyright © 2011-2022 走看看