c# 加密

来源:互联网 发布:mac high sierra 升级 编辑:程序博客网 时间:2024/05/22 02:06

//密钥
    const string key = "783b22ec9fd04a35a8fb52098b49a54e";
    private byte[] sKey = Convert.FromBase64String(key);
    //矢量
    private byte[] sIV = { (byte) 0x12, (byte) 0x34, (byte) 0x56,
(byte) 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF };
    private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

#region 加密解密
    /// <summary>
    /// DES3 CBC模式加密
    /// </summary>
    /// <param name="key">密钥</param>
    /// <param name="iv">IV</param>
    /// <param name="data">明文的byte数组</param>
    /// <returns>密文的byte数组</returns>
    private string Des3EncodeCBC(string value)
    {
        try
        {
            byte[] data = Encoding.UTF8.GetBytes(value);
            // Create a MemoryStream.
            MemoryStream mStream = new MemoryStream();

            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
            tdsp.Mode = CipherMode.CBC;             //默认值
            tdsp.Padding = PaddingMode.PKCS7;       //默认值

            // Create a CryptoStream using the MemoryStream
            // and the passed key and initialization vector (IV).
            CryptoStream cStream = new CryptoStream(mStream,
                tdsp.CreateEncryptor(sKey, sIV),
                CryptoStreamMode.Write);

            // Write the byte array to the crypto stream and flush it.
            cStream.Write(data, 0, data.Length);
            cStream.FlushFinalBlock();

            // Get an array of bytes from the
            // MemoryStream that holds the
            // encrypted data.
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            // Return the encrypted buffer.
            return Convert.ToBase64String(ret);
        }
        catch (CryptographicException e)
        {
            return null;
        }
    }

    /// <summary>
    /// DES3 CBC模式解密
    /// </summary>
    /// <param name="key">密钥</param>
    /// <param name="iv">IV</param>
    /// <param name="data">密文的byte数组</param>
    /// <returns>明文的byte数组</returns>
    private string Des3DecodeCBC(string value)
    {
        try
        {
            byte[] data = Convert.FromBase64String(value);
            // Create a new MemoryStream using the passed
            // array of encrypted data.
            MemoryStream msDecrypt = new MemoryStream(data);

            TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
            tdsp.Mode = CipherMode.CBC;
            tdsp.Padding = PaddingMode.PKCS7;

            // Create a CryptoStream using the MemoryStream
            // and the passed key and initialization vector (IV).
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                tdsp.CreateDecryptor(sKey, sIV),
                CryptoStreamMode.Read);

            // Create buffer to hold the decrypted data.
            byte[] fromEncrypt = new byte[data.Length];

            // Read the decrypted data out of the crypto stream
            // and place it into the temporary buffer.
            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

            //Convert the buffer into a string and return it.
            return UTF8Encoding.UTF8.GetString(fromEncrypt);
        }
        catch (CryptographicException e)
        {
            return null;
        }
    }

    /// <summary>
    /// MD5加密,加密时在被加密的串上加上密钥
    /// </summary>
    private string MD5Encode(string vlaue)
    {
        byte[] v = UTF8Encoding.UTF8.GetBytes(vlaue + key);
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] ev = md5.ComputeHash(v);
        return Convert.ToBase64String(ev);
    }

    #endregion

 

原创粉丝点击