Loading
0

c#加密解密
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器
c#加密解密

class Encrypt
{
    //using System.Security.Cryptography
    //using System.IO

    private static int[] mKey;
    private static string tmp;

    /// <summary>
    /// 解密
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string Decrypt(string str)
    {
        if (!string.IsNullOrEmpty(str))
        {
            int i = 0;
            int pos = 0;
            string AdjPos = "";
            string data = "";
            string[] DataArry = new string[100];
            int cnt = 0;
            tmp = "";
            Let("Quanta");
            cnt = (short)Convert.ToChar((str.Substring(0, 1))) - 35;

            AdjPos = str.Substring(1, cnt);
            data = str.Substring(cnt + 1);
            for (i = 0; i < cnt; i++)
            {
                pos = (short)Convert.ToChar((AdjPos.Substring(i, 1))) - 35;
                data = data.Substring(0, pos - 1) + (char)((short)Convert.ToChar(data.Substring(pos - 1, 1)) - 35) + data.Substring(pos);
            }
            for (i = 0; i < data.Length; i++)
            {
                tmp = tmp + (char)(((short)Convert.ToChar(data.Substring(i, 1)) ^ mKey[(i + 1) % 31]));
            }
            return tmp;
        }
        else
            return string.Empty;
    }
    /// <summary>
    /// 加密
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string EncryptString(string str)
    {
        if (!string.IsNullOrEmpty(str))
        {
            string AdjPos = "";
            short code;
            Int16 cnt = 0;
            Let("Quanta");
            tmp = "";
            for (int i = 0; i < str.Length; i++)
            {
                code = (short)((short)Convert.ToChar(str.Substring(i, 1)) ^ mKey[(i + 1) % 31]);
                if (code < 35)
                {
                    //skip ASC 1~35
                    code += 35;
                    cnt += 1;
                    AdjPos = AdjPos + (char)(35 + i + 1);
                }
                tmp = tmp + (char)(code);
            }
            //skip ASC 1~35
            return ((char)(35 + cnt)) + AdjPos + tmp;
        }
        else
            return string.Empty;
    }
    /// <summary>
    /// 秘钥
    /// </summary>
    /// <param name="str"></param>
    private static void Let(string str)
    {
        int i = 0;
        int mSeed = 0;
        mKey = new int[31];
        //str = "Quanta";
        for (i = 0; i < str.Length; i++)
        {
            mSeed = mSeed + (((short)(Convert.ToChar(str.Substring(i, 1)))) * (i + 1)) % 177;
        }

        for (i = 0; i < 31; i++)
        {
            mKey[i] = (mSeed * i) % (128 - i);
        }
    }
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="input">待加密的字符串</param>
    /// <returns></returns>
    public static string DesEncrypt(string EncryptString)
    {
        byte[] inputByteArray = Encoding.UTF8.GetBytes(EncryptString);
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        des.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        des.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(), 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)
    {
        try
        {
            byte[] inputByteArray = Convert.FromBase64String(DecryptString);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            des.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch
        {
            return "";
        }
    }
}

301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-23-36069-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?

最后编辑于:2021-06-11 13:03:42作者:

上一篇:C#文件重命名
下一篇:c#播放系统/机器声音