Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Delphi] Encrypt/Convert Text

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Sat Nov 24, 2007 9:16 am    Post subject: [Delphi] Encrypt/Convert Text Reply with quote

Hey.
Im making a Console application in Delphi atm.
I want it to have a feature that can "encrypt" text.
So if you type something like
Hello
It types:
@099+
And the command should be something like
Code:
encrypt (text here)

Example:
Code:
encrypt hello my name is moller

And then I want the program to encrypt that.

2. problem would be how to make the user able to copy the encrypted text.
You guys got any idea?
- Møller
Back to top
View user's profile Send private message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Nov 24, 2007 9:51 am    Post subject: Reply with quote

Make a Windows app, with some text boxes and a button. The user fills in one box (to encrypt or to decrypt), then clicks the box, and the program fills in the other one
Back to top
View user's profile Send private message
ZenX
Grandmaster Cheater Supreme
Reputation: 1

Joined: 26 May 2007
Posts: 1021
Location: ">>Pointer<<" : Address 00400560 Offset :1FE

PostPosted: Sat Nov 24, 2007 9:58 am    Post subject: Reply with quote

Source Provided by LatimusSoftware

No credits to me~

Code:

Create a new project. Add 3 edit boxes (Edit1, Edit2 and Edit3). Add 4 buttons (encrypt, decrypt, hash and browse). Also add a radiogroup and call it RadioHex, insert two radiobuttons in there, one "Show in Hex" and the other "Show in Base64".

We are going to add the following to the uses clause:

uses SHA1, Twofish, Base64;

    *

      SHA1 is the Hash. One-way hash functions are like fingerprints of the data being hashed, it takes variable-length input and produces a fixed-length output (160 bits in SHA1). The hash function ensures that, if the information is changed in any way —even by just one bit— an entirely different output value is produced. This is useful in many ways. One of them for example is to encrypt a file and then make a hash of that encrypted file. You send both the file and the hash of it. When the other end gets the file he or she can check if the file was tampered with by creating a hash of the file and comparing it with the hash sent. Of course the hash has to be sent in a secure way to be sure that it is untouched (probably using some public key encryption algorithm like RSA). We are going to use hashes to create a fingerprint of both the password entered by the user and the encrypted file.
    *

      TWOFISH is the encryption algorithm.
    *

      Base64 is a way of coding output so it has only printable characters.

Add the following procedures to your program:

// produces a hash of a string
procedure _HashString(s: string; var Digest: TSHA1Digest);
var
  Context: TSHA1Context;               // record to store intermediate
// data
begin
  SHA1Init(Context);                   // initialize the data record
  SHA1Update(Context,@S[1],Length(S)); // update the data record with
                                       // the string
  SHA1Final(Context,Digest);           // produce the final hash
end;

// produces a hash of a file
procedure _HashFile(filename: string; var Digest: TSHA1Digest);
var
  Context: TSHA1Context;               // record to store intermediate
 // data
  Source: file;                        // source file
  Buffer: array[1..8192] of byte;      // read buffer
  Read: integer;                       // number of bytes read
begin
  AssignFile(Source,filename);
  try
    Reset(Source,1);
  except
    MessageDlg('Unable to open source file.',mtInformation,[mbOK],0);
    Exit;
  end;
  SHA1Init(Context);                  // initialize the data structure
  repeat
    BlockRead(Source,Buffer,Sizeof(Buffer),Read);
    SHA1Update(Context,@Buffer,Read);    // update the hash
  until Read<> Sizeof(Buffer);
  SHA1Final(Context,Digest);             // produce the final hash
  CloseFile(Source);
end;

These procedures will produce the One-Way Hash for files and strings.

Note that the encryption and decryption process will be like this:

   1.

      get a hash of the password
   2.

      initialize the key in the algorithm using the hash
   3.

      make an IV (the first block to use in the chaining modes)
   4.

      encrypt or decrypt
   5.

      burn data (key in memory, IV, etc.) and sets the algorithm for the next task.
   6.

      (Optional) get a hash of the encrypted file

In (1) we generate the hash of the user's password. This hash will be used in (2) to initialize the key. This key is NOT the password the user typed. In (3) we generate the first block of encrypted data, this block contains random data. This is done because we are using one of the chaining modes and we need and each block depends on the previous one to be encrypted. This helps to hide patters in the text like redundancy and other file headers (like the "PK" in zipped files). In (4) we perform the actual encryption/decryption and in (5) we get rid of the data place in memory (we don't want any sniffer stealing our password, right?)

So, in the OnClick procedure for the button you called Encrypt you can write the following (Warning, this will OVERWRITE the original file. Unless you remember the password you won't be able to recover the data):

var
  KeyData: TTwofishData;         // the initialized key data
  Digest: TSHA1Digest;
  IV: array[0..15] of byte;      // the initialization vector needed for
                                 // chaining modes
  Buffer: array[0..8191] of byte;
  Source,source2: file;
  i, j, n: integer;
  Key: string;
  NumRead, NumWritten: Integer;

begin
  Key:= edit2.Text; // you wrote the password in edit2
    _HashString(Key,Digest);
  TwofishInit(KeyData,@Digest,Sizeof(Digest),nil);   // initialize the
                                   // key data using a hash of the key
  FillChar(IV,Sizeof(IV),0);            // make the IV all zeros
  TwofishEncryptCBC(KeyData,@IV,@IV);   // encrypt the IV to
                                        // get a 'random' IV
  Move(IV,KeyData.InitBlock,Sizeof(KeyData.InitBlock));    // move the
                            // IV into the keydata so can use chaining
  TwofishReset(KeyData);    // reset the keydata so it uses the new IV
  AssignFile(Source, edit1.Text); // file to encrypt in edit1
  try
    Reset(Source,1);
  except
    TwofishBurn(KeyData);   // get rid of the data in memory
    MessageDlg('File cannot be opened...', mtInformation, [mbOK], 0);
    Edit2.text:='00000000000000000000000000000000';
    Edit2.text:='';
    Exit;
  end;

  repeat
    n:= FilePos(Source);
    BlockRead(Source,Buffer,Sizeof(Buffer),i);
    for j:= 1 to (i div 16) do       // 16 is the blocksize of Twofish
                                     // so process in 16 byte blocks
    TwofishEncryptCBC(KeyData,@Buffer[(j-1)*Sizeof(IV)],   // encrypt!
                      @Buffer[(j-1)*Sizeof(IV)]);

    if (i mod 16)<> 0 then        // encrypt the last bytes that don't
                                  // fit in to a full block
    begin
      Move(KeyData.LastBlock,IV,Sizeof(IV));
      TwofishEncryptCBC(KeyData,@IV,@IV);    // encrypt the full block
                              // again (so that it is encrypted twice)
      for j:= 1 to (i mod 16) do
        // xor this encrypted block with the short block
        Buffer[(i and not 15)+j]:= Buffer[(i and not 15)+j] xor IV[j];
      end;
    Seek(Source,n);
    BlockWrite(Source,Buffer,i);   // write out the buffer to the file
  until i<> Sizeof(Buffer);

  CloseFile(Source);
  TwofishBurn(KeyData); // get rid of data
  Edit2.text:='00000000000000000000000000000000';
  Edit2.text:='';
  Edit1.text:='';
  MessageDlg('All done. File Encrypted with the same name as the ' +
             'original.', mtInformation,[mbOK],0);
end;

Presto! We just encrypted the file in Edit1 with the password in Edit2!

To decrypt do exactly the same, but instead of using TwofishEncryptCBC() use:

  TwofishDecryptCBC(KeyData,@Buffer[(j-1)*Sizeof(IV)],
                        @Buffer[(j-1)*Sizeof(IV)]);

Simple, huh?

Ok, you have your encrypted file. Some nice touch will be to generate the hash of that encrypted file and send it along with it.

To achieve this we will use the _HashFile procedure we wrote before. On the OnClick procedure for the button you named Hash add the following:

procedure TForm1.HashClick(Sender: TObject);
var
  Digest: TSHA1Digest;   // binary form of the hash
  i: integer;
begin
if Edit1.Text='' then exit;      // make sure we have a file to hash
  _HashFile(Edit1.Text,Digest);  // calculate the hash
                                 // and store in Digest
if radiohex.ItemIndex=0 then     // we want to show the
                                 // hash in hexadecimal
begin
  Edit3.Text:= '0x';
  for i:= 0 to (Sizeof(Digest)-1) do
    Edit3.Text:= Edit3.Text+IntToHex(Digest[i],2);   // convert Digest
                                            // to a hexadecimal string
end
else // in base64
 begin
  Edit3.Text:= '';
  for i:= 0 to (Sizeof(Digest)-1) do
    Edit3.Text:= Edit3.Text+chr(Digest[i]); // convert Digest to a
                                            // base64
  Edit3.Text:=B64Encode(Edit3.Text);
 end;
end;

_________________
CEF Moderator since 2007 ^_^
ZenX-Engine
Back to top
View user's profile Send private message Yahoo Messenger
HolyBlah
Master Cheater
Reputation: 2

Joined: 24 Aug 2007
Posts: 446

PostPosted: Sat Nov 24, 2007 10:24 am    Post subject: Reply with quote

ZenX wrote:
Source Provided by LatimusSoftware

No credits to me~

Code:

Create a new project. Add 3 edit boxes (Edit1, Edit2 and Edit3). Add 4 buttons (encrypt, decrypt, hash and browse). Also add a radiogroup and call it RadioHex, insert two radiobuttons in there, one "Show in Hex" and the other "Show in Base64".

We are going to add the following to the uses clause:

uses SHA1, Twofish, Base64;
...
...
...

Where is SHA1.pas, Twofish.pas and Base64.pas?


I think you need to use case like this:
Code:

i,z:Integer;
TempLine:String;
begin
  for i:=0 to ToEencrypt.Lines.Count-1 do  //ToEencrypt is a Memo
  begin
    for z:=0 to length(ToEencrypt.Lines[i])-1 do
    begin
      case Byte(ToEencrypt.Lines[i][z]) of
        1:TempLine:=TempLine+ 'a';
        2:TempLine:=TempLine+ 'G';
        3:TempLine:=TempLine+ ';';
        //and more here
      end;
    end;
      Eencrypted.Lines.add(TempLine); //Eencrypted is another Memo
      TempLine:='';
  end;
end;

or:
Code:
i,z:Integer;
TempLine:String;
begin
  for i:=0 to ToEencrypt.Lines.Count-1 do  //ToEencrypt isa Memo
  begin
    for z:=0 to length(ToEencrypt.Lines[i])-1 do
      TempLine:=TempLine+char(Byte(ToEencrypt.Lines[i][z])+[Some Number here]);
    Eencrypted.Lines.add(TempLine); //Eencrypted is an other Memo
    TempLine:='';
  end;
end;
Back to top
View user's profile Send private message
ZenX
Grandmaster Cheater Supreme
Reputation: 1

Joined: 26 May 2007
Posts: 1021
Location: ">>Pointer<<" : Address 00400560 Offset :1FE

PostPosted: Sat Nov 24, 2007 10:27 am    Post subject: Reply with quote

HolyBlah wrote:

Where is SHA1.pas, Twofish.pas and Base64.pas?


Here

_________________
CEF Moderator since 2007 ^_^
ZenX-Engine
Back to top
View user's profile Send private message Yahoo Messenger
rapion124
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Mar 2007
Posts: 1095

PostPosted: Sat Nov 24, 2007 2:15 pm    Post subject: Reply with quote

Much simpler way:

Use CryptoAPIs. If you don't know how, go look in your help files that come with Delphi. Find the one that talks about Win32 programming reference. Go to the index and type in Cryptography.

It's much more secure but you have to know how to use them correctly and so forth.
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Sat Nov 24, 2007 3:31 pm    Post subject: Reply with quote

Thanks for all that stuff.
Ill look through its all.
Back to top
View user's profile Send private message
squrrilslayer
Master Cheater
Reputation: 0

Joined: 26 Dec 2006
Posts: 323

PostPosted: Sat Nov 24, 2007 7:33 pm    Post subject: Reply with quote

ive already made one in VB2005. if you were using VB, i could help.
although, by description, it seems that what your trying to make is exactly like mine: Here

meh the code might not be useful to you but the ui?

_________________
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Sat Nov 24, 2007 11:05 pm    Post subject: Reply with quote

CryptDecrypt/Encrypt.
Back to top
View user's profile Send private message
assaf84
Expert Cheater
Reputation: 0

Joined: 03 Oct 2006
Posts: 238

PostPosted: Sun Nov 25, 2007 4:06 am    Post subject: Reply with quote

Loop threw the letters and encrypt them. XOR encryption with a keyword would do just fine..
Back to top
View user's profile Send private message
squrrilslayer
Master Cheater
Reputation: 0

Joined: 26 Dec 2006
Posts: 323

PostPosted: Sun Nov 25, 2007 5:50 am    Post subject: Reply with quote

assaf84 wrote:
Loop threw the letters and encrypt them. XOR encryption with a keyword would do just fine..

lol. that is exactly my program... Razz
its easy. just remember to convert to ascii characters first.

_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites