Encryption name

I went through encryption today and my colleague told me an easier / more convenient way to do this, what I did is listed there, so please tell me what type of encryption or hashign? he told me it was a mixture of some sort of hashing and encryption. Sorry for the stupid question, I'm new to here. :) below method takes a string (password in my case). overrides it and makes a dictionary (adding dictionary elements is given below) and matches keys and values ​​and returns encrypted data.

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;
}

      

below method adds items to dictionary, items are mostly based on ascii codes. I iterate over all asciis and then the corresponding words are added to the dictionary. but the real trick is when adding an item to a dictionary. for example, when I add "A" in the dictionary as a key, then its value will be "D". the variable "jump" indicates the difference between the key and the value. so if my password is "ABC" it will return "DEF" or "FED" if I canceled it.

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;
}

      

+3


source to share


2 answers


This will be an encryption replacement , however, if you are not doing it for fun, do not use this method for anything that requires security. 1000 people use this encryption method every day for fun (check your local newspaper, most likely it's usually next to the crossword puzzle).



+7


source


Guys, I adapted my code

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);
}

      



encryption is not secure, but running it. I hope this helps someone write their own method from scratch.

0


source







All Articles