Caesar/ROT String Encryption

Posts 12 of 2 · Page 1 of 1
Caesar/ROT String Encryption
Well I know some people are wanting to encrypt there strings in there hacks, I dont code in C++ and I am just learning C#, One of my projects in C# for learning purposes was to convert some of my Delphi Encryptions, Here you have a C# Caesar/ROT encryption, don't laugh at my C# coding as im just learning but this seems to run very good and maybe it might help the guys who write in C++, It should be easyer to read than Delphi due to the similarties between C++ and C#

//Edit
Decided to include the whole class which also include the Vigener Cipher(uses a key). I personally would still convert the ROT cipher for your string encryption in C++ for the hacks.

Code:
/* 
 Class  : StringCrypt.cs
 Aurthor: Departure
 Url    : www.Cheesydoodle.com 
 Info:
    Encrypt Strings with Vigenere Cipher and ROT Cipher(Caesar Cipher, Rot13 ect..)
    Based on information from wiki
 */

internal class StringCrypt
    {
        public static string Vigenere(string sSource, string sKey, int iTableSize = 94, bool bDecrypt = false)
        {
            //Variables
            int i = 32;
            int iPosText;
            int iPosKey;
            string sTable = "";
            string sResult = "";

            //Create Table
            while (i < (iTableSize + 32))
            {
                sTable +=  ((char)i);
                i++;
            }

            //Make Key same size as Cipher
            while (sSource.Length >= sKey.Length)
            {
                sKey = string.Concat(sKey, sKey);
            }

            //Vigenere Routine
            i = 0;
            while (i <= (sSource.Length - 1))
            {
                if (sTable.IndexOf(sSource[i]) == -1)
                    sResult += sSource[i];
                else
                {
                    iPosText = sTable.IndexOf(sSource[i]);
                    iPosKey = sTable.IndexOf(sKey[i]);
                    if (bDecrypt)
                        sResult += sTable[(((iPosText + iTableSize) - iPosKey) % iTableSize)];
                    else
                        sResult += sTable[((iPosText + iPosKey) % iTableSize)];
                }
                i++;

            }

            return sResult;

        }

        public static string Caesar(string sSource, int iKey, bool bDecrypt = false)
        {
            //Variables
            string sResult = "";
            string sTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int iPosText;
            int i;

            //Convert to Uppercase
            sSource = sSource.ToUpper();

            //Caesar Routine
            i = 0;
            while (i <= (sSource.Length - 1))
            {
                if (sTable.IndexOf(sSource[i]) == -1)
                    sResult += sSource[i];
                else
                {
                    iPosText = sTable.IndexOf(sSource[i]);
                    if (bDecrypt)
                        sResult += sTable[(((iPosText + sTable.Length) - iKey) % sTable.Length)];
                    else
                        
                    sResult += sTable[((iPosText + iKey) % sTable.Length)];
                }
                i++;
            }

            return sResult;

        }

        public static string ROT13(string sSource)
        {
            return Caesar(sSource, 13, false);
        }
    }

Suggestions would be to change the sTable to include all readable alphabet Letters and numbers, even include special chars also this is a Rotation of 13 change this modifying this line
return Caesar(sSource, 13, false); change 13 to how ever many positions, to decrypt the encrypted string just set this line to True
If you want me to post an XOR encryption + the .html file it uses to generate it. I got tired of using the HTML for it and just converted that to C++ and made a simple utility in a console app.
Posts 12 of 2 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?