RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Encryption Algorithm

Axle

Wanderer
Encryption Algorithm

I'm currently working on an encryption project that uses a key and only standard ascii characters. It basically adds the character to be encrypted to a character in the key. The problem is I can only use standard ascii characters 32 - 126. When I add the two values, I end up with a minimum value of 64, and a maximum value of 252. In order to get the new values back within range I have tried to subtract 126, but this often time returns values < 32. I've tried division (by 2), but I lose precision and accuracy when trying to decrypt. If anyone has done something like this before and has any tips I would appreciate them.
 

Phantom

Knight
Axle said:
I'm currently working on an encryption project that uses a key and only standard ascii characters. It basically adds the character to be encrypted to a character in the key. The problem is I can only use standard ascii characters 32 - 126. When I add the two values, I end up with a minimum value of 64, and a maximum value of 252. In order to get the new values back within range I have tried to subtract 126, but this often time returns values < 32. I've tried division (by 2), but I lose precision and accuracy when trying to decrypt. If anyone has done something like this before and has any tips I would appreciate them.

Why don't you use the extended ascii character set?

If you must, you could try to mod the two numbers by a number, add the results then at the end process add the remainder back to the two numbers.
 

David

Moderate
Does your output have to be the same size as your input? You could split the resultant byte in half and add or subtract by a known amount (or bit shift) to turn each nibble into a byte and get it into the ASCII range.

It would double the size of the data but it would also further complicate attempted decryption. Actually you would have spare bits that will be masked off during your decryption routine, you could fill those with random values to even further complicate things for attackers.
 

Axle

Wanderer
Thanks for the response, I have to use the standard ascii, I'm presented with no other options, it is a requirement for the program. Here are my encrypt and decrypt methods so you can see what all I have tried:
Code:
public void DisplayMessage(char ch)
        {
            Console.Write("  " + ch);
        }

        public string Encrypt(string args)
        {
            try
            {
                StreamReader inputFile = new StreamReader("Key.txt");
                StreamWriter outputFile = new StreamWriter("output.txt");
                //StreamWriter PlainTextOut = new StreamWriter("PlainTextOut.txt");
                StreamReader PlainTextIn = new StreamReader("PlainTextOut.txt");
                //PlainTextOut.WriteLine(args);
                //PlainTextOut.Close();
                args = PlainTextIn.ReadLine();
                string keyInput = inputFile.ReadLine();
                int keyIndex = 0;
                int keyLength = keyInput.Length - 1;

                this.PlainText = new char[args.Length];


                for (int i = 0; i < args.Length; ++i)
                {
                    char keyValue = char.Parse(keyInput.Substring(keyIndex, 1));
                    char oldCharValue = char.Parse(args.Substring(i, 1));
                    char newCharValue = (char)(oldCharValue + keyValue);
                    //this.PlainText[i] = char.Parse(args.Substring(i, 1));

                    Console.Write("\nOld value: " + args.Substring(i, 1) + " + key value: " + keyInput.Substring(keyIndex, 1) + " = " + (int)newCharValue);

                    if (newCharValue > 126)
                    {
                        newCharValue = (char)(newCharValue - 126);
                    }
                    if (newCharValue < 32)
                    {
                        newCharValue = (char)(newCharValue + 32);
                    }
                    //if ((newCharValue % 10) == 0)
                    //{
                    //    newCharValue = (char)((newCharValue / 2) + 1);
                    //}

                    //else if ((newCharValue % 2) == 1)
                    //{
                    //    newCharValue = (char)((newCharValue / 2) + 1);

                    //}
                    //else
                    //{
                    //    newCharValue = (char)((newCharValue / 2));
                    //}


                    outputFile.Write(newCharValue);
                    DisplayMessage(newCharValue);

                    //Resets the key character back to the first character 
                    //when the last character is reached in the key
                    if (keyIndex >= keyLength)
                    {
                        keyIndex = 0;
                    }
                    else
                    {
                        ++keyIndex;
                    }
                }
                inputFile.Close();
                outputFile.Close();
                //PlainTextOut.Close();
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            //for (int i = 0; i < args.Length; ++i)
            // {
            //    //cast the single character to integer, add one to the value and cast that value to a char argument 
            //    //that gets passed to the DisplayMessage method.
            //    this.DisplayMessage( (char)( 1 + (int)char.Parse( args.Substring(i, 1) ) ) );
            // }
            return args;
        }

        public string Decrypt(string args)
        {
            try
            {
                StreamReader keyInputFile = new StreamReader("Key.txt");
                StreamReader inputFile = new StreamReader("output.txt");
                StreamWriter newOutputFile = new StreamWriter("oldtext.txt");

                args = inputFile.ReadLine();
                string keyInput = keyInputFile.ReadLine();
                int keyLength = keyInput.Length - 1;
                int keyIndex = 0;
                int keyValue;
                int oldValue;
                int newValue;

                for (int i = 0; i < args.Length; ++i)
                {
                    keyValue = Convert.ToInt32(char.Parse(keyInput.Substring(keyIndex, 1)));
                    newValue = Convert.ToInt32(char.Parse(args.Substring(i, 1)));
                    //newValue = (char)(newValue - 32);


                    oldValue = (int)(newValue);

                    //if (newValue - 32 < 64)
                    //{
                    //    newValue = ((newValue + 126) - keyValue);
                    //}
                    if ((newValue - keyValue) < 32)
                    {
                        newValue = ((newValue + 126) - keyValue);
                    }
                    else
                    {
                        newValue -= keyValue;
                    }
                    if (newValue > 126)
                    {
                        newValue -= 32;
                    }

                    //if ((newValue % 10) == 1)
                    //{
                    //    newValue = (char)(((newValue - 1) * 2) - keyValue);
                    //}
                    //else if ((newValue % 2) == 1)
                    //{
                    //    newValue = (char)((newValue * 2) - (keyValue));
                    //}
                    //else
                    //{
                    //    newValue = (char)((newValue * 2) - (keyValue + 1));
                    //}

                    Console.WriteLine("Encrypted value: " + (int)oldValue + " Old char value: " + (char)newValue);
                    newOutputFile.Write(oldValue);
                    if (keyIndex >= keyLength)
                    {
                        keyIndex = 0;
                    }
                    else
                    {
                        ++keyIndex;
                    }
                }
                keyInputFile.Close();
                inputFile.Close();
                newOutputFile.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            //for (int i = 0; i < args.Length; ++i)
            // {
            //    //cast the single character to integer, subtract one from the value and cast that value to a char argument 
            //    //that gets passed to the DisplayMessage method.
            //    this.DisplayMessage((char)(-1 + (int)char.Parse(args.Substring(i, 1))));
            // }
            return args;
        }

Any suggestions and/or tips are welcome.
 

Axle

Wanderer
David said:
Does your output have to be the same size as your input? You could split the resultant byte in half and add or subtract by a known amount (or bit shift) to turn each nibble into a byte and get it into the ASCII range.

It would double the size of the data but it would also further complicate attempted decryption. Actually you would have spare bits that will be masked off during your decryption routine, you could fill those with random values to even further complicate things for attackers.

I appreciate that information, unfortunately that's way over my head atm. This application is for my CS101 class, and requirements are to add a character to a key character (from a file) and keep the results within the range of 32 - 126. It also has to be able to properly decrypt the data. The encryption is easy enough, but decryption has proven very complicated
with the said restrictions.

character (32 to 126) + keycharacter (32 to 126) = newchar (64 - 252)

simple enough to put

Code:
if(newvalue > 126)
{
    newvalue = newvalue - 126;
}

however if the total of of both characters is 64 + 64 = 128
then 128 - 126 = 2

which I catch by adding 32 to newvalues < 32

but only further complicates things when decypting

I've also tried using the mod (it's commented out)

but when both 158 and 159 are divided by 2, 79 is the product
 

joey snitch

Wanderer
try this:
Code:
while (newCharValue > 126)
{
newCharValue -= 95;
}
that will ensure that each encoded character will have a unique value between 32 and 126 inclusive.

to decrypt is a bit harder
Code:
while (newValue - keyValue) < 32 )
{
newValue += 95;
}
newValue -= keyValue;
after that, newValue should contain the same value it did before you began, unless you used a different key value.
 

Axle

Wanderer
joey snitch said:
try this:
Code:
while (newCharValue > 126)
{
newCharValue -= 95;
}
that will ensure that each encoded character will have a unique value between 32 and 126 inclusive.

to decrypt is a bit harder
Code:
while (newValue - keyValue) < 32 )
{
newValue += 95;
}
newValue -= keyValue;
after that, newValue should contain the same value it did before you began, unless you used a different key value.

Hey thanks for the response and the info, unfortunately, I figured it out earlier today before checking this site. The magic number I was looking for was 94 (126 - 32), which is the number of characters that fall out of range. Although I took a different approach, the outcome is still the same as your algorithm. I add the two values together then subtract 126, then:

Code:
if (newCharValue < 32)
                    {
                        // the number of characters that can return a - value is 62 + 32 = 94
                        newCharValue = newCharValue + 95; // 31 + 94 = 125, so we add 1 to ensure every char gets modified
                    }
I noticed that some character combinations would result in using the original value of the character, then deduced 31 + 94 = 125, so I can add one to ensure every character gets modified. Then for decryption:

Code:
if ((newValue - keyValue) < 126)     //Character didn't get 95 added to it
                    {
                        newValue -= keyValue;            //So we can just subtract the key value
                    }
                    else
                    {
                        newValue -= (keyValue + 95);     //Subtract keyValue and 95 from the current value
                    }

Like I said outcome is the same, I just wish I would have seen your post sooner, would have saved me a lot of time. Thanks again!
 

milt

Knight
Axle said:
Just finished with 102%. I'm going into my 4th term carrying a 4.0 GPA.
Nice, sounds like you work very hard. Keep it up.

What college/university do you attend?
 

swtrse

Wanderer
I have just one tipp

If you write your own cryptalgorithm in .NET you should do that by implementing the CryptProvider Interfaces. That whay the Algorithm can be easy used by all .NET classes that youe Encryption/Decryption (for exampe CryptStream).
 

Troad78

Wanderer
I'm in the same class as Axle. I've got the algorithm part down but what I can't figure out is how to make the text file that contains the encryption key. Everywhere I look has much more complicated encryption processes. My professor had a sample posted up but no tips on how to create it...Can anyone help??

This is his sample file....entitled cryptokey.txt
Code:
4tM6V=VA$H2lGdI-DC]+%#2b`aSdD/*o<qpl)LdYif)oCRgk.2
@9,7CVF:/mrkDT$v]]<<sjbkT&3 hV/[^H42=>tS-1A:Tsl&7h
*)[#VaVsC9GhpY*^f*vC+HB$NOG'b?B7+2y.(8%D`ji+-/@,dF
2xNJxP^r#NCV!zUtvt_:Fv!!R=@>9$hE>PW2$vT0#Qvy[-/(>M
 #3fThK?plC<R6r78K4&<QR+&3B2#55nRfRCC8:clhr#JDD50W
%<cFUiFt2h!/HDV$=eX'DSUfV,^A/,!nkvMl8V.4,S!Ac'$YVH
Uqsr](3QX2>n=piBS.n# /2Zn_e6(6EP,C'4wE%YIW$5qMhI_?
XNw xW)_-0.g?Usp2pVPw-,uKwk]+DF%oA<(PF)QW%z<LP@>V/
 E99G2l^NP^p%Vvi[yOH (nuX!LbCZ6r0sUm'AsU@mt`t4yhB(
Iajj=7Mtj=_3:):$`p=)zVS 6[)tr,u[uqhU^T4:U(2%EX?$)X
Uwl:x>$UC78W-+($RXqBP:U]m=TDpriYxl)E$Iitrl>WyBp(57
.y/@pFvz:b#!AjN:'^5p@MEu4ATRb_EW#[!B7C.=$y%pQr+x1l
lqB6A(6`N('8feW;)gca,N%>A+O6-9^fpy'%L,lg3B4,)R@?2X
S-Q8fACy?tHpcTX*HX. QtXuDHKzc[r=]OGvzX5uJ^)`J[Rqh`
Oocz-j=) fn4=rc4-gUTLR$Y`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