3

我今天正在经历加密,我的同事告诉我一个更简单/自定义的方法,我所做的列在那里,所以你能告诉我那是什么类型的加密或 hashign 吗?他告诉我这是某种散列和加密的混合。对不起,愚蠢的问题,我是新来的。:) 下面的方法需要一个字符串(在我的例子中是密码)。反转它并制作一个字典(添加字典项如下所列)并匹配键和值并返回加密数据。

public static string Encrypt(string source)
{
    string enCrypted = "";
    try
    {
        source = Reverse(source);
        Dictionary<char, char> sourceTable = AddDictionaryItems();

        char[] sourceArray = source.ToCharArray();
        StringBuilder encryptedData = new StringBuilder();
        foreach (char chr in sourceArray)
        {
            KeyValuePair<char, char> Pair;
            Pair = sourceTable.First(tuple => tuple.Key == chr);
            encryptedData.Append(Pair.Value);
        }
        enCrypted=encryptedData.ToString();
    }
    catch (Exception ex)
    { 

    }
    return enCrypted;
}

以下方法将项目添加到字典中,项目基本上基于 ascii 代码。我遍历所有asciis,然后在字典中添加相应的字符。但真正的诀窍是在字典中添加项目时。例如,当我在字典中添加“A”作为键时,它的值将是“D”。变量“jump”指定键和值之间的差异。所以如果我的密码是“ABC”,如果我反转那个东西,它会返回“DEF”或“FED”。

public static Dictionary<char, char> AddDictionaryItems()
{
    Dictionary<char, char> dc = new Dictionary<char, char>();
    try
    {
        int initial = 33;
        int jump = 3;
        int final = 127 - jump;

        for (int ascii = initial; ascii < final; ascii++)
        {
            dc.Add((char)ascii, (char)(ascii + jump));
        }
        for (int ascii = final; ascii < final + jump; ascii++)
        {
            dc.Add((char)ascii, (char)(initial));
            initial++;
        }
    }
    catch (Exception ex)
    { 
        throw ex;
    }
    return dc;
}
4

2 回答 2

7

那将是一个替代密码,但是除非你是为了好玩,否则不要将此方法用于任何需要安全的事情。每天有成千上万的人为了好玩而破解这种加密方法(查看您当地的报纸,它可能也有,它通常在填字游戏旁边)。

于 2013-01-23T04:59:42.220 回答
0

伙计们,我已经定制了我的代码

public static string Reverse(string str)
    {
        char[] charArray = str.ToCharArray();
        Array.Reverse(charArray);
        return new string(charArray);
    }

    public static string Encrypt(string source)
    {
        string prefix = Guid.NewGuid().ToString();
        string infix = Guid.NewGuid().ToString();
        string postfix = Guid.NewGuid().ToString();

        int length = source.Length;
        string firstHalf = source.Substring(0, length / 2);
        string secondHalf = source.Substring(length / 2);

        string ConcatedPassword = prefix + firstHalf + infix + secondHalf + postfix;

        string enCrypted = "";
        try
        {
            ConcatedPassword = Reverse(ConcatedPassword);
            Dictionary<char, char> sourceTable = AddDictionaryItems();

            char[] sourceArray = ConcatedPassword.ToCharArray();
            StringBuilder encryptedData = new StringBuilder();
            foreach (char chr in sourceArray)
            {
                KeyValuePair<char, char> Pair;
                Pair = sourceTable.First(tuple => tuple.Key == chr);
                encryptedData.Append(Pair.Value);
            }
            enCrypted = encryptedData.ToString();
        }
        catch (Exception ex)
        {

        }
        return enCrypted;
    }

    public static string Decrypt(string source)
    {
        string deCrypted = "";
        try
        {
            source = Reverse(source);
            Dictionary<char, char> sourceTable = AddDictionaryItems();

            char[] sourceArray = source.ToCharArray();
            StringBuilder decryptedData = new StringBuilder();
            foreach (char chr in sourceArray)
            {
                KeyValuePair<char, char> Pair;
                Pair = sourceTable.First(tuple => tuple.Value == chr);
                decryptedData.Append(Pair.Key);
            }
            deCrypted = decryptedData.ToString();
            string prefixRemoved = deCrypted.Remove(0, 36);
            string postfixRemoved = prefixRemoved.Remove(prefixRemoved.Length - 36);
            string infixRemoved = postfixRemoved.Remove((postfixRemoved.Length - 36) / 2, 36);
            deCrypted = infixRemoved;
        }
        catch (Exception ex)
        {

        }
        return deCrypted;
    }

    public static Dictionary<char, char> AddDictionaryItems()
    {
        Dictionary<char, char> dc = new Dictionary<char, char>();
        try
        {
            int initial = 33;
            int jump = 10;
            int final = 127 - jump;

            for (int ascii = initial; ascii < final; ascii++)
            {
                dc.Add((char)ascii, (char)(ascii + jump));
            }
            for (int ascii = final; ascii < final + jump; ascii++)
            {
                dc.Add((char)ascii, (char)(initial));
                initial++;
            }
        }
        catch (Exception ex)
        {

        }
        return dc;
    }

protected void Page_Load(object sender, EventArgs e)
{
     string password = "$Om3P@55w0r6";
     string encrypted = Encrypt(password);
     string decrypted = Decrypt(encrypted);
}

加密并不安全,但它是一个开始。我希望它可以帮助某人从头开始编写自己的方法。

于 2013-01-23T10:00:41.170 回答