zoukankan      html  css  js  c++  java
  • 对称加密解密类c#

    namespace EncryptDLL
    {
       
    #region 对称加密类
       
    /// <summary>
       
    /// 对称加密解密类
       
    /// </summary>
        public static class SymmetricEncryptClass
        {
           
    #region 对称加密代码
           
    /// <summary>
           
    /// 对称加密方法
           
    /// </summary>
           
    /// <param name="strMsg">要加密的数据</param>
           
    /// <param name="key">加密Key</param>
           
    /// <param name="IV">加密向量</param>
           
    /// <returns>返回加密后的数据,类型byte[]</returns>
            public static byte[] SymmetricEncrptFunction(this string strMsg, string key, string IV)
            {
                TripleDESCryptoServiceProvider tdes
    = new TripleDESCryptoServiceProvider();
                SHA1 sha1
    = new SHA1CryptoServiceProvider();
               
    //初始密钥的设定
                string strNamePlusBirthday = key + IV;
               
    string strKey = new string(strNamePlusBirthday.Reverse().ToArray());
               
    if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length > tdes.Key.Length)
                {
                    tdes.Key
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().Substring(0, tdes.Key.Length));
                }
               
    else if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length < tdes.Key.Length)
                {
                    tdes.Key
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().PadRight(tdes.Key.Length, ' '));
                }
               
    //初始向量的设定

               
    if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length > tdes.IV.Length)
                {
                    tdes.IV
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().Substring(0, tdes.IV.Length));
                }
               
    else if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length < tdes.IV.Length)
                {
                    tdes.IV
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().PadRight(tdes.IV.Length, ' '));
                }
               
    byte[] encryptBuffer = Encoding.Default.GetBytes(strMsg);
                MemoryStream streamMemory
    = new MemoryStream();
                CryptoStream cryStream
    = null;//加密流
                byte[] readBuffer;//用来接受加密数据
                try
                {
                    cryStream
    = new CryptoStream(streamMemory, new TripleDESCryptoServiceProvider().CreateEncryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write);
                    cryStream.Write(encryptBuffer,
    0, encryptBuffer.Length);
                    cryStream.FlushFinalBlock();
                }
               
    catch
                {
                   
    return null;
                }
               
    finally
                {
                    readBuffer
    = streamMemory.ToArray();
                    cryStream.Close();
                    streamMemory.Close();
                }
               
    return readBuffer;
            }

           
    #endregion
           
    #region 对称解密代码
           
    /// <summary>
           
    /// 对称解密方法
           
    /// </summary>
           
    /// <param name="bufMsg">需要解密的数据</param>
           
    /// <param name="key">解密Key</param>
           
    /// <param name="IV">解密向量</param>
           
    /// <returns>解密后的数据</returns>
            public static string SymmetricDecryptionFunction(this byte[] bufMsg, string key, string IV)
            {
                TripleDESCryptoServiceProvider tdes
    = new TripleDESCryptoServiceProvider();
                SHA1 sha1
    = new SHA1CryptoServiceProvider();
               
    //初始密钥的设定
                string strNamePlusBirthday = key + IV;
               
    string strKey = new string(strNamePlusBirthday.Reverse().ToArray());
               
    if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length > tdes.Key.Length)
                {
                    tdes.Key
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().Substring(0, tdes.Key.Length));
                }
               
    else if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length < tdes.Key.Length)
                {
                    tdes.Key
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().PadRight(tdes.Key.Length, ' '));
                }
               
    //初始向量的设定
                if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length > tdes.IV.Length)
                {
                    tdes.IV
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().Substring(0, tdes.IV.Length));
                }
               
    else if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length < tdes.IV.Length)
                {
                    tdes.IV
    = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().PadRight(tdes.IV.Length, ' '));
                }
               
    byte[] readBuffer = bufMsg;
                MemoryStream streamRead
    = new MemoryStream(readBuffer);
                CryptoStream deCryStream
    = null;
               
    byte[] bufferReader;
               
    try
                {
                    deCryStream
    = new CryptoStream(streamRead, new TripleDESCryptoServiceProvider().CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Read);
                    bufferReader
    = new byte[readBuffer.Length];
                    deCryStream.Read(bufferReader,
    0, bufferReader.Length);
                }
               
    finally
                {
                    deCryStream.Close();
                    streamRead.Close();
                }
               
    string deCryString = Encoding.Default.GetString(bufferReader);
               
    return deCryString;
            }
           
    #endregion
        }
       
    #endregion
    }

  • 相关阅读:
    X-010 FriendlyARM tiny4412 uboot移植之移植网卡驱动TFTP用起来
    使用NFS启动Tiny4412开发板根文件系统
    《C专家编程》第四章——令人震惊的事实:数组和指针并不相同
    《C专家编程》第三章——分析C语言的声明
    《C专家编程》第二章——这不是Bug,而是语言特性
    《C专家编程》第一章——C:穿越时空的迷雾
    《C与指针》读后感
    《C与指针》第十五章练习
    《C与指针》第十四章练习
    《C与指针》第十三章练习
  • 原文地址:https://www.cnblogs.com/jhabb/p/1925495.html
Copyright © 2011-2022 走看看