C# 一个简易的加密、解密字符串工具类

荒凉一梦 提交于 2019-12-25 22:06:24

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

本文实现的类CypherHelper是一个用于加密、解密字符串的工具类~~~,适合于方便地加密、解密长度较短的密码等字符串(C#里面已经有了库System.Security.Cryptography,对于比较长的字符串或流来说是一个更好的选择)

类CypherHelper代码:

class CypherHelper
{
    /// <summary>
    /// 加密字符串
    /// </summary>
    /// <param name="word">被加密字符串</param>
    /// <param name="key">密钥</param>
    /// <returns>加密后字符串</returns>
    public static string Encrypt(string word, string key)
    {
        if (!System.Text.RegularExpressions.Regex.IsMatch(key, "^[a-zA-Z]*$"))
        {
            throw new Exception("key 必须由字母组成");
        }

        key = key.ToLower();

        //逐个字符加密字符串
        char[] s = word.ToCharArray();
        for (int i = 0; i < s.Length; i++)
        {
            char a = word[i];
            char b = key[i % key.Length];
            s[i] = EncryptChar(a, b);
        }

        return new string(s);
    }

    /// <summary>
    /// 加密单个字符
    /// </summary>
    /// <param name="a">被加密字符</param>
    /// <param name="b">密钥</param>
    /// <returns>加密后字符</returns>
    private static char EncryptChar(char a, char b)
    {
        int c = a + b - 'a';

        if (a >= '0' && a <= '9') //字符0-9的转换
        {
            while (c > '9') c -= 10;
        }
        else if (a >= 'a' && a <= 'z') //字符a-z的转换
        {
            while (c > 'z') c -= 26;
        }
        else if (a >= 'A' && a <= 'Z') //字符A-Z的转换
        {
            while (c > 'Z') c -= 26;
        }
        else return a; //不再上面的范围内,不转换直接返回

        return (char)c; //返回转换后的字符
    }

    /// <summary>
    /// 解密字符串
    /// </summary>
    /// <param name="word">被解密字符串</param>
    /// <param name="key">密钥</param>
    /// <returns>解密后字符串</returns>
    public static string Decrypt(string word, string key)
    {
        if (!System.Text.RegularExpressions.Regex.IsMatch(key, "^[a-zA-Z]*$"))
        {
            throw new Exception("key 必须由字母组成");
        }

        key = key.ToLower();

        //逐个字符解密字符串
        char[] s = word.ToCharArray();
        for (int i = 0; i < s.Length; i++)
        {
            char a = word[i];
            char b = key[i % key.Length];
            s[i] = DecryptChar(a, b);
        }

        return new string(s);
    }

    /// <summary>
    /// 解密单个字符
    /// </summary>
    /// <param name="a">被解密字符</param>
    /// <param name="b">密钥</param>
    /// <returns>解密后字符</returns>
    private static char DecryptChar(char a, char b)
    {
        int c = a - b + 'a';

        if (a >= '0' && a <= '9') //字符0-9的转换
        {
            while (c < '0') c += 10;
        }
        else if (a >= 'a' && a <= 'z') //字符a-z的转换
        {
            while (c < 'a') c += 26;
        }
        else if (a >= 'A' && a <= 'Z') //字符A-Z的转换
        {
            while (c < 'A') c += 26;
        }
        else return a; //不再上面的范围内,不转换直接返回

        return (char)c; //返回转换后的字符
    }
}

Main函数调用示例

class Program
{
    static void Main(string[] args)
    {
        //1.加密
        string a = CypherHelper.Encrypt("TsybiusLee1234", "Galatea");
        Console.WriteLine("加密:TsybiusLee1234 → " + a);

        //2.解密
        string b = CypherHelper.Decrypt(a, "Galatea");
        Console.WriteLine("解密:" + a + " → " + b);

        Console.ReadLine();
    }
}

运行结果

END

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!