[C#] Dynamic Crypto

ok today im going to show you how to make a dynamic crypto

a dynamic crypto in a nutshell
if my message is: hi how are you?
i have 2xh and 2xo
my encrypted text: ij,jpx.bsf:zqv!
its with the cizer cipher just dynamic
if i just decoded it id get hi iow are ypu?
its simple but were gonna make it a bit better
were gonna add 1 to the offset for each next letter
we will just be using

using System;//just for a the exeptions

now we will make to encryption class
we will set up the offsets and the alphabet

        private int e_offset = 1;
        private int d_offset = 1;
        public Char[] alphabet = new Char[]
        { 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'y', 'x', 'c', 'v', 'b',
            'n', 'm', 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Y', 'X', 'C',
            'V', 'B', 'N', 'M', ' '}/;/german qwertz keyboard

for the encryption we will turn the string into a char array
then we will find where the char is in our alphabet and add the offset
and add the result to a string and repeat for the rest
iv made it easier for you to add your own alphabet

        public string encrypt(string text)
        {
            string end = "";
            char[] char_text = text.ToCharArray();
            for (int y = 0; y < text.Length; y++)
            {
                int num_of_char = 0;
                for (int x = 0; x < alphabet.Length; x++)
                {
                    if (char_text[y] == alphabet[x])
                    {
                        num_of_char = x + e_offset;
                        e_offset++;
                        break;
                    }
                    else if (x == alphabet.Length)
                    {
                        throw new Exception("un-supported character");
                    }
                }
                if (num_of_char > alphabet.Length)
                {
                    
                    int temp = num_of_char % alphabet.Length;
                    num_of_char = 0;
                    for (int x = 0; x < alphabet.Length; x++)
                    {
                        if (temp != 0)
                        {
                            num_of_char++;
                            temp++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                end += alphabet[num_of_char].ToString();
            }
            return end;
        }

for the decryption it gets a bit harder we have to do the same thing just in revers so instead of + its -
and if we go under the 0 it we have carry on from alphabet.Length

        public string decrypt(string text)
        {
            string end = "";
            char[] char_text = text.ToCharArray();//turns string to char 
            for (int y = 0; y < text.Length; y++)
            {
                int num_of_char = 0;
                for (int x = 0; x < alphabet.Length; x++)
                {
                    if (char_text[y] == alphabet[x])//gets to location of the same char
                    {
                        num_of_char = x;
                        
                        break;
                    }
                    else if (x == alphabet.Length)
                    {
                        throw new Exception("un-supported character");
                    }
                }
            int res = 0;// % will allwas return as a int less than 0
            int temp = num_of_char - d_offset % alphabet.Length;//so we dont go over
            for (int x = 0; x < alphabet.Length; x++ )//so we dont go under
            {
                if (temp >= 0)
                {
                    temp++;
                    res++;//flips int e.g: -9 will turn into 9
                }
                else
                {
                    break;
                }
            }
                end += alphabet[res].ToString();//gets the char pos and gose back as far as the off set is
                d_offset++;
            }
            return end;

        }
    }
    

i encrypted (hhhhh) and got (jklyx)
thats all to it have fun
-dedady-

What?

wat.

Could you please explain this all a little more?

3 Likes

oaktree is this better?

Well, if I understood correctly it is like a monoalphabetic cipher with an incremental padding but I doubt about the efficiency and the applicability of such cypher (cf. Kerckhoffs principle).

Upstream, I have some feedbacks about your article presentation. Firstly, it could be nice to be consistent in your coding style and try, as much as possible, to aerate your code base ! It will considerably improve your readability and consequently, the enjoyability of such reading. Lastly, more explanations would be appreciated, mainly if the code is not self explaining.

Good luck.

Best,
Nitrax

P.S: Your code can be easily simplify. Think about ascii character representation, pre incrementation, …

4 Likes

Bravo made me laugh like mad :laughing:

This topic was automatically closed after 30 days. New replies are no longer allowed.