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