Results 1 to 11 of 11
  1. #1
    Departure's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    805
    Reputation
    125
    Thanks
    1,794
    My Mood
    Doh

    [Info][Tutorial] Shift String Encryption [Delphi][Source Code]

    K guys I want to explain the shift position encryption cipher method, I see alot of C++ code here using shift method incorrectly below is a modified caesar cipher, First modification is that we don't use Alphabet A..Z which is 26 characters, Instead we use all readable characters and special characters, the second modification is that the Alphabet we use is mixed up making harder to decrypt as it not in its common Alphabet order. The reason this will work is because we use modulus to cycle through the Alphabet, This means when we get to the last Character of the Alphabet is will start from the begining of the alphabet, This prevents it from making the cipher non readable if the shift is more than the alphabet, The C++ I have seen have this potential bug if used incorrectly. Anyway here is a simple Left/Right Modified Caesar Cipher.

    Modified Caesar Cipher:
    Code:
    Function CaesarLeft(sString: String; iAmount: Integer):String;
    var
     i, iPos, iAlphaBet: Integer;
     sAlphabet: String;
    begin
     sAlphabet:= 'aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz1029384756';
     iAlphaBet:= Length(sAlphabet);
     i:= 1;
     while i <= Length(sString) do
      Begin
       if sString[i] = ' ' then
        Result:= Result + ' '
        else
        begin
         iPos:= pred(pos(sString[i],sAlphabet));
         if iPos < 1 then iPos:= 1;
         Result:= Result + sAlphabet[(((iPos + iAlphaBet) - iAmount) mod iAlphaBet) + 1];
        end;
       inc(i);
      end;
    end;
     
    Function CaesarRight(sString: String; iAmount: Integer):String;
    var
     i, iPos, iAlphaBet: Integer;
     sAlphabet: String;
    begin
     sAlphabet:= 'aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz1029384756';
     iAlphaBet:= Length(sAlphabet);
     i:= 1;
     while i <= Length(sString) do
      Begin
       if sString[i] = ' ' then
        Result:= Result + ' '
        else
        begin
         iPos:= pred(pos(sString[i],sAlphabet));
         Result:= Result + sAlphabet[((iPos + iAmount) mod iAlphaBet) + 1];
        end;
       inc(i);
     end;
    end;
    Now the next Function is similar to a Caesar cipher(shift method) but would be a lot harder to crack without knowing the "key" its called the vigenere cipher. This is my implementation of the vigenere cipher and this code also got published on delphi.abou*****m

    Vigenere Cipher:
    Code:
    Function VigenereExEncrypt(sSource, sKey: String; bDecrypt: Boolean = False; iTableSize: Integer = 94): String;
    var
     i, iPosText, iPosKey: Integer;
     sTable: string;
    begin
    //Create our Cipher Table
     i:= 32;
      While i <= (iTableSize + 32) do
       Begin
        sTable:= ConCat(sTable, Chr(i));
        inc(i);
       end;
    
    //Make the key the same size or greater than the Source
     while Length(sSource) >= Length(sKey) do
       sKey:= ConCat(sKey,sKey);
    
    //Remove Line Feed & Carrage Returns from Cipher
     i:=0;
      while i<=Length(sSource) do
        if (sSource[i]=Chr(10)) or (sSource[i]=Chr(13)) then
         Delete(sSource, i, 1)
        else
         Inc(i);
    
    {Uncomment if you need to remove spaces
    //Remove Spaces from Cipher
     i:=0;
      while i<=Length(sSource) do
        if sSource[i]=' ' then
         Delete(sSource, i, 1)
        else
         Inc(i);
    }
    
    //Vegenere Encryption/Decryption routine
     i:= 1;
      while i <= Length(sSource) do
       Begin
        iPosText  := pred(pos(sSource[i],sTable));
        iPosKey   := pred(pos(sKey[i],sTable));
    //Encrypt or Decrypt(Default is Encrypt)
        Case bDecrypt of
         False: Result    := Result + sTable[((iPosText  + iPosKey) mod iTableSize) + 1];
         True : Result    := Result + sTable[(((iPosText + iTableSize) - iPosKey) mod iTableSize) + 1];
        end;
        inc(i);
       end;
    end;
    Hope this helps anyone wanting to understand the Shift method of encryption, The vigenere is ofcause alot harder to crack than caesar and Xor methods... I also coded this in C# so if people have trouble reading Delphi syntax and fine it easier to read in C# then I can post that source code also.


    //Edit

    Decided to add my C# source because I know not many people code in Delphi and it will be easier for them to read C#, Mind you C# is not my first choice of programming languages and if you are a full C# coder you might see a better way to code this, If you do find a better way then please post your implementation

    C# Caesar/Rot13/Vigenere:
    Code:
    using System;
    /*
    Class  : StringCrypt.cs
    Aurthor: Departure
    Url    : im-integrations.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 &gt;= 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);
    }
    }
    Last edited by Departure; 07-11-2012 at 02:05 AM. Reason: Added C# source

  2. The Following 7 Users Say Thank You to Departure For This Useful Post:

    ac1d_buRn (07-11-2012),Flengo Jr. (07-11-2012),luccss (07-11-2012),OBrozz (07-11-2012),pDevice (08-22-2012),PikaMucha_Itu (07-11-2012),Reflex- (07-11-2012)

  3. #2
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Nice one. I took the liberty of writing a C# version for my own benefit, here are the fruits of my labour:

    Code:
        public static class VigenereCipher
        {
            private const int LOWER_ASCII = 32; // start at ' ' (space)
            private const int UPPER_ASCII = 129; // end past DEL. Note, I will be using 127/128/129 to mask CR/LF/TAB respectively.
            private const int RANGE_ASCII = UPPER_ASCII - LOWER_ASCII;
    
            public static string CipherText(string key, string data)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    data = data.Replace('\r', (char)127).Replace('\n', (char)128).Replace('\t', (char)129); //normalize the string first.
                    string cipherKey = CreateKey(key, data.Length); //need an acceptable key.
    
                    // a touch of LINQ magic brightens everyone's day :3
                    return data.Aggregate(new StringBuilder(data.Length), (builder, c) =>
                                              builder.Append(
                                                    (char)((((c + cipherKey[builder.Length]) - (LOWER_ASCII << 1)) % RANGE_ASCII) + LOWER_ASCII) // mindfuck alert.
                                              )
                                         ).ToString();
                }
                return data;
            }
    
            public static string DecipherText(string key, string data)
            {
                if (!string.IsNullOrEmpty(data))
                {
                    string decipherKey = CreateKey(key, data.Length); 
                    data = data.Aggregate(new StringBuilder(data.Length), (builder, c) =>
                                              builder.Append(
                                                    (char)((((c + RANGE_ASCII) - decipherKey[builder.Length]) % RANGE_ASCII) + LOWER_ASCII) // MOAR MINDFUX
                                              )
                                         ).ToString();
                    return data.Replace((char)127, '\r').Replace((char)128, '\n').Replace((char)129, '\t'); //reverse the special char replacement.
                }
                return data;
            }
    
            // simple helper method to create an acceptable Vigenére cipher key.
            private static string CreateKey(string baseKey, int len)
            {
                if (baseKey.Length == len)
                    return baseKey;
                else if (baseKey.Length > len)
                    return baseKey.Substring(0, len);
                else
                    return Enumerable.Range(0, len - baseKey.Length).Aggregate(new StringBuilder(baseKey, len), (builder, index) => builder.Append(builder[index])).ToString();
            }
        }
    DISCLAIMER: Only tested with a few basic ones. Also, doesn't handle special characters (i.e éêþ and whatever else)
    Last edited by Jason; 07-11-2012 at 07:51 AM.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  4. The Following User Says Thank You to Jason For This Useful Post:

    Departure (07-11-2012)

  5. #3
    PikaMucha_Itu's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    Itu City
    Posts
    2,391
    Reputation
    59
    Thanks
    4,105
    My Mood
    Goofy
    good job.
    This takes away the lag!

  6. #4
    Departure's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    805
    Reputation
    125
    Thanks
    1,794
    My Mood
    Doh
    Nice work Jason, judging by your code, you are long time C# programmer... I am also going to try an assembly version that way it can be used as inline assembly with most programming languages without changing the syntax too much. I don't normally code in assembly because of the time it takes, but in this case I think this could be done easier in assembly because its really just moving bytes around and using basic addition, subtraction and modulus

  7. #5
    Nightmare's Avatar
    Join Date
    Jun 2011
    Gender
    male
    Location
    North of Hell
    Posts
    2,396
    Reputation
    149
    Thanks
    6,601
    My Mood
    Worried
    Great job!

  8. #6
    luccss's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Posts
    482
    Reputation
    183
    Thanks
    3,440
    My Mood
    Breezy
    Nice man great job

  9. #7
    XxkyorakuxX's Avatar
    Join Date
    Jun 2012
    Gender
    male
    Location
    Perth, Australia MATE!!
    Posts
    168
    Reputation
    10
    Thanks
    18
    My Mood
    Drunk
    i guess its gonna take a bit longer than a single 1 hour C++ to understand that

  10. #8
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Quote Originally Posted by Departure View Post
    Nice work Jason, judging by your code, you are long time C# programmer... I am also going to try an assembly version that way it can be used as inline assembly with most programming languages without changing the syntax too much. I don't normally code in assembly because of the time it takes, but in this case I think this could be done easier in assembly because its really just moving bytes around and using basic addition, subtraction and modulus
    Assembly isn't *that* portable really. Sure, the assembler code itself is, but running it within various different languages is a hassle. Plus, writing in the languages own syntax can have better results (i.e JIT will architecturally optimize IL code depending on the current environment, which would won't get with static assembly code)
    And yeah, been using C# for just over a year now I think, it's brilliant.
    Last edited by Jason; 07-12-2012 at 08:46 AM.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  11. #9
    Departure's Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    805
    Reputation
    125
    Thanks
    1,794
    My Mood
    Doh
    Sorry but when I said "use assembly for portability" I was not thinking of .net, I was thinking more C++ native win32 sort of thing, anyway with .net it needs the .net frame work which is hardly portable. But now that I am thinking about .net, how does .net execute shell code? can it execute shell code? I know it can call win32 library's without to much problems so I was thinking there might be a way to execute shell code with parameters in C#?

  12. #10
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Quote Originally Posted by Departure View Post
    Sorry but when I said "use assembly for portability" I was not thinking of .net, I was thinking more C++ native win32 sort of thing, anyway with .net it needs the .net frame work which is hardly portable. But now that I am thinking about .net, how does .net execute shell code? can it execute shell code? I know it can call win32 library's without to much problems so I was thinking there might be a way to execute shell code with parameters in C#?
    You can run shellcode in a roundabout manner. You need some WinAPI PInvoke to do it though (VirtualAlloc is about the only one).

    i.e:

    Code:
    private delegate uint delShellcode(); // shellcode stub, takes no params (you can add some if you need to) and returns a 32-bit value (whatever eax happens to be holding)
    
    public static uint RunShellcode(byte[] bShellcode)
    {
        IntPtr alloc = VirtualAlloc(IntPtr.Zero, bShellcode.Length, 0x1000 | 0x2000, 0x40); //MEM_COMMIT | MEM_RESERVE & PAGE_EXECUTE_READWRITE
        if (alloc != IntPtr.Zero)
        {
            Marshal.Copy(bShellcode, 0, alloc, bShellcode.Length); //write the shellcode to executable memory.
            var stub = (delShellcode)Marshal.GetDelegateForFunctionPointer(alloc, typeof(delShellcode));
            return stub();
        }
        throw new OutOfMemoryException("Unable to allocate any memory");
    }
    You can also add craps to the delegate declaration to specify calling convention and other shit:

    Code:
    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
    private delegate uint delShellcode(); // shellcode stub, takes no params and returns a 32-bit value (whatever eax happens to be holding)
    And then to call it:
    Code:
    byte[] shellcode = 
    {
        0x31, 0xC0, //xor eax, eax
        0x83, 0xC0, 0x01, //add eax, 1
        0xC3 //ret
    };
    
    uint result = RunShellcode(shellcode);
    Console.WriteLine(result); //should be 1
    As for .NET, immaterial. JIT is an aspect of interpreted languages, not necessarily .NET framework. The fact remains that a lot of the common languages don't have raw asm support. With the extra time it takes to write such a cipher in ASM, you may as well just use online converters and just convert it to whatever language you need it for, then fix the mistakes :3
    Last edited by Jason; 07-12-2012 at 12:20 PM.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  13. The Following User Says Thank You to Jason For This Useful Post:

    Departure (07-13-2012)

  14. #11
    TheCrow's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Location
    US
    Posts
    6
    Reputation
    10
    Thanks
    0
    My Mood
    Bored
    OK can you explain were to set this thing on?..... act like you are explaining it to someone who don't know anything bout this stuff....in fact don't act..: LOL

Similar Threads

  1. [INFO] WarRock Source Code Section
    By AeroMan in forum WarRock Hack Source Code
    Replies: 34
    Last Post: 09-13-2011, 09:37 PM
  2. Source Code Trade - Delphi
    By Departure in forum Combat Arms Coding Help & Discussion
    Replies: 9
    Last Post: 05-23-2011, 06:24 PM
  3. [Info] Combat Arms BR Source Code
    By Alessandro10 in forum Combat Arms Brazil Discussions
    Replies: 10
    Last Post: 02-27-2011, 08:06 AM
  4. Tutorial de como hacer los Hack Source Code?
    By dark697 in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 4
    Last Post: 08-21-2010, 11:58 AM
  5. [Tutorial] Finding Source Code of Not Open Source Programs
    By treeham in forum C++/C Programming
    Replies: 21
    Last Post: 03-28-2010, 08:35 AM