Solved

Translate C++ to DELPHI

Posted on 2009-05-05
3
374 Views
Last Modified: 2012-05-06
Hi experts (:
i want translate this code to delphi please.
thank you.
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <windows.h>
 

#define DELTA     0x4;
 

void Encrypt(LPSTR lpBuffer, DWORD dwLen)

{

  DWORD i;

     

     if(!lpBuffer || !dwLen)

        return;

     

     for(i = 0; i < dwLen; i++)

      {

         lpBuffer[i] += DELTA;

      }

}
 

void Decrypt(LPSTR lpBuffer, DWORD dwLen)

{

  DWORD i;

     

     if(!lpBuffer || !dwLen)

        return;

     

     for(i = 0; i < dwLen; i++)

      {

         lpBuffer[i] -= DELTA;

      }

}
 

int main(int argc, char ** argv)

{ 

  char Buffer[256] = {0}; 

  int  BufferLen;

     

     if(argc < 2)

        return 1;

     

     BufferLen = strlen(argv[1]);

     

     if(BufferLen >= sizeof(Buffer))

        return 1;

        

     memcpy(Buffer, argv[1], BufferLen);

     Encrypt(Buffer, BufferLen);

     

     system("cls");

     printf("String: %s", Buffer);

     

     getchar(); 

 

  return 1;

}
 
 
 
 
 
 
 

// next \\
 
 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <windows.h>
 

#define DELTA     0x4;
 

void Encrypt(LPSTR lpBuffer, DWORD dwLen)

{

  DWORD i;

     

     if(!lpBuffer || !dwLen)

        return;

     

     for(i = 0; i < dwLen; i++)

      {

         lpBuffer[i] += DELTA;

      }

}
 

void Decrypt(LPSTR lpBuffer, DWORD dwLen)

{

  DWORD i;

     

     if(!lpBuffer || !dwLen)

        return;

     

     for(i = 0; i < dwLen; i++)

      {

         lpBuffer[i] -= DELTA;

      }

}
 

int main(int argc, char ** argv)

{ 

  char Buffer[256] = {0}; 

  int  BufferLen;

     

     if(argc < 2)

        return 1;

     

     BufferLen = strlen(argv[1]);

     

     if(BufferLen >= sizeof(Buffer))

        return 1;

        

     memcpy(Buffer, argv[1], BufferLen);

     Decrypt(Buffer, BufferLen);

     

     system("cls");

     printf("String: %s", Buffer);

     

     getchar(); 

 

  return 1;

}

Open in new window

0
Comment
Question by:MelissaCandyGirl
3 Comments
 
LVL 26

Assisted Solution

by:Russell Libby
Russell Libby earned 200 total points
ID: 24307223
I am only doing one project example, because the only diff between the 2 is that one decrypts and the other encrypts. Basically a one line change.

Delphi source for console app:

program Project1;
{$APPTYPE CONSOLE}
uses
  Windows,
  SysUtils;

const
  DELTA             =  $04;

procedure Encrypt(lpBuffer: PChar; dwLen: DWORD);
var  i:          DWORD;
begin

  if (Assigned(lpBuffer) and (dwLen > 0)) then
  begin
     for i:=0 to Pred(dwLen) do
     begin
        lpBuffer[i]:=Chr(Byte(lpBuffer[i]) + DELTA);
     end;
  end;

end;

procedure Decrypt(lpBuffer: PChar; dwLen: DWORD);
var  i:          DWORD;
begin

  if (Assigned(lpBuffer) and (dwLen > 0)) then
  begin
     for i:=0 to Pred(dwLen) do
     begin
        lpBuffer[i]:=Chr(Byte(lpBuffer[i]) - DELTA);
     end;
  end;

end;

procedure ReadKey;
var  lpInput:       INPUT_RECORD;
     dwRead:        DWORD;
begin

  // Clear struct
  System.FillChar(lpInput, SizeOf(KEY_EVENT_RECORD), 0);

  // Read one keystroke from the input buffer
  while ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), lpInput, 1, dwRead) do
  begin
     // Ensure that it is a key up event
     if (lpInput.EventType = KEY_EVENT) and not(lpInput.Event.KeyEvent.bKeyDown) then
     begin
        // Done processing
        break;
     end;
     // Clear struct
     System.FillChar(lpInput, SizeOf(KEY_EVENT_RECORD), 0);
  end;

end;

procedure ClearScreen;
var  cbi:           CONSOLE_SCREEN_BUFFER_INFO;
     coScreen:      COORD;
     dwWrite:       DWORD;
begin

  // Set new cursor's starting position
  System.FillChar(coScreen, SizeOf(coScreen), 0);

  // Get screen buffer info
  if GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), cbi) then
  begin
     // Fill the entire screen with blanks
     FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), #32, cbi.dwSize.X * cbi.dwSize.Y, coScreen, dwWrite);
     // Position the cursor
     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coScreen);
  end;

end;

var
  Buffer:           Array [0..255] of Char;
  BufferLen:        Integer;
  Arg:              String;

begin

  FillChar(Buffer, SizeOf(Buffer), 0);

  if (ParamCount < 1) then
     ExitCode:=1
  else
  begin
     Arg:=ParamStr(1);
     BufferLen:=Length(Arg);
     if (BufferLen >= SizeOf(Buffer)) then
        ExitCode:=1
     else
     begin
        Move(Pointer(Arg)^, Buffer, BufferLen);

        // The only difference between the two projects is calling encrypt / decrypt
        //    Encrypt(@Buffer, BufferLen);
        //    Decrypt(@Buffer, BufferLen);

        Encrypt(@Buffer, BufferLen);

        // Clear the screen
        ClearScreen;

        // Dump the string
        Write(Format('String: %s', [Buffer]));

        // Wait for key
        ReadKey;

        // Done
        ExitCode:=1;

     end;
  end;

end.

----

Regards,
Russell

0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 300 total points
ID: 24307357

program Q_24381988;
 

{$APPTYPE CONSOLE}
 

uses

  Windows;
 

const

  DELTA = $4;
 

procedure Encrypt(var Buffer: String; Length: DWORD);

var

  I : DWORD;

begin

  If (Buffer = '') Or (Length <= 0) Then Exit;

  For I := 1 To Length Do

    begin

    Buffer[I] := Char(Byte(Buffer[I]) + DELTA);

  end;

end;
 

procedure Decrypt(var Buffer: String; Length: DWORD);

var

  I : DWORD;

begin

  If (Buffer = '') Or (Length <= 0) Then Exit;

  For I := 1 To Length Do

    begin

    Buffer[I] := Char(Byte(Buffer[I]) - DELTA);

  end;

end;
 

var

  Output : String;

  BufferLen : Integer;

begin

  If ParamCount < 1 Then Exit;

  BufferLen := Length(ParamStr(1));

  If BufferLen >= 256 Then Exit;

  Output := ParamStr(1);

  SetLength(Output,BufferLen);

  Encrypt(Output,BufferLen);

  Writeln('String: ' + Output);

  Readln;

end.
 
 

// next \\
 
 

program Q_24381988_2;
 

{$APPTYPE CONSOLE}
 

uses

  Windows;
 

const

  DELTA = $4;
 

procedure Encrypt(var Buffer: String; Length: DWORD);

var

  I : DWORD;

begin

  If (Buffer = '') Or (Length <= 0) Then Exit;

  For I := 1 To Length Do

    begin

    Buffer[I] := Char(Byte(Buffer[I]) + DELTA);

  end;

end;
 

procedure Decrypt(var Buffer: String; Length: DWORD);

var

  I : DWORD;

begin

  If (Buffer = '') Or (Length <= 0) Then Exit;

  For I := 1 To Length Do

    begin

    Buffer[I] := Char(Byte(Buffer[I]) - DELTA);

  end;

end;
 

var

  Output : String;

  BufferLen : Integer;

begin

  If ParamCount < 1 Then Exit;

  BufferLen := Length(ParamStr(1));

  If BufferLen >= 256 Then Exit;

  Output := ParamStr(1);

  SetLength(Output,BufferLen);

  Decrypt(Output,BufferLen);

  Writeln('String: ' + Output);

  Readln;

end.

Open in new window

0
 

Author Closing Comment

by:MelissaCandyGirl
ID: 31578102
thank you (:
kisses
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 73
how to update exe applicatio from internet ? 6 67
Adoquery sql  left join does not work 25 80
Convert MS Word document to a PDF file 9 63
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now