c#实现3DES加密

2019-07-23    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
public class Crypto
{
    SymmetricAlgorithm mCSP;
    #region "Constants"
    const string _key = "NYYObMInlTtentKODigMiSE/NSp/4JQv";
    const string _IV = "PenS8UCVF7s=";
    #endregion
    public Crypto()
    {
        mCSP = SetEnc();
        mCSP.IV = Convert.FromBase64String(_IV);
        mCSP.Key = Convert.FromBase64String(_key);
    }
    public string EncryptString(string Value)
    {
        ICryptoTransform ct;
        MemoryStream ms;
        CryptoStream cs;
        Byte[] byt = new byte[64];

        try
        {
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);

            byt = Encoding.UTF8.GetBytes(Value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception Ex)
        {
            throw (new Exception("An error occurred while encrypting string", Ex));
        }
    }
    public string DecryptString(string Value)
    {
        ICryptoTransform ct;
        MemoryStream ms;
        CryptoStream cs;
        Byte[] byt = new byte[64];
        try
        {
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);

            byt = Convert.FromBase64String(Value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();
            string test = Encoding.UTF8.GetString(ms.ToArray());
            return Encoding.UTF8.GetString(ms.ToArray());
        }
        catch (Exception ex)
        {
            throw (new Exception("An error occurred while decrypting string", ex));
        }
    }
    private SymmetricAlgorithm SetEnc()
    {
        return new TripleDESCryptoServiceProvider();
    }
}

标签: [db:TAGG]

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:php 最最最简单的模板引擎:PHP原生模板引擎

下一篇:python将图片文件转换成base64编码