Translate simple C code to Delphi 5

Posted on 2001-06-10
Last Modified: 2010-04-06
   Please translate this code to Delphi 5 . I need it as soon as possible.
   Requires libdes to compile.

#include <stdlib.h>
#include <stdio.h>
#include "des.h"

int main(int argc, char *argv[])
     FILE *infile, *outfile;

     char *header = "Allaire Cold Fusion Template\012Header Size: ";
     char buffer[54];
     int headsize, outlen;
     int skip_header;
     int len, i;

     char *keystr = "Error: cannot open template file--\"%s\". Please, try again!\012\012";
     des_cblock key;
     des_cblock input;
     des_cblock output;
     des_key_schedule schedule;

     switch (argc)
     case 2:
          outfile = stdout;

     case 3:
          if (!(outfile = fopen(argv[2], "wb")))
               fprintf(stderr, "Error opening output file %s\n", argv[2]);
               return 1;

          fprintf(stderr, "Usage: cfdecrypt <encrypted template> [output file]\n");
          return 1;

     if (!(infile = fopen(argv[1], "rb")))
          fprintf(stderr, "Error opening input file %s\n", argv[1]);
          return 1;

     if ((fread(buffer, 1, 54, infile) < 54) || (memcmp(buffer, header, 42)))
          fprintf(stderr, "File is not an encrypted template\n");
          return 1;

     if (!memcmp(&buffer[42], "New Version", 11))
          headsize = 69;
          skip_header = 1;
          headsize = atoi(&buffer[42]);
          skip_header = 0;

     if ((headsize < 54) || (fseek(infile, headsize, SEEK_SET) < 0))
          fprintf(stderr, "Error in file format\n");
          return 1;

     des_string_to_key(keystr, &key);
     des_set_key(&key, schedule);
     outlen = 0;

     while ((len = fread(input, 1, 8, infile)) == 8)
          des_ecb_encrypt(&input, &output, schedule, 0);
          outlen += 8;
          i = 0;

          if (skip_header)
               while (i < 8)
                    if (output[i++] == 0x1A)
                         skip_header = 0;

          fwrite(output + i, 1, 8 - i, outfile);

     for (i = 0; i < len; i++)
          output[i] = input[i] ^ (outlen + i);

     fwrite(output, 1, len, outfile);

     return 0;
Question by:MrNet
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 6172933
Look... This code uses module with DES encryption procedures. I think it is impossible to translate it into Delphi code without having at least des.h header file. And I think it is fully impossible to translate it if des.h is not a header file for DLL which provides functions for working with DES encryption until you have another one for Delphi.

Will be happy to know if I am not right...

Best regards,
Alexey Zverev.

Author Comment

ID: 6173125
ok Alexey,

I have exe file already compiled with the prevoius code. I made Delphi project which using the library SellAPI to run the exe with ShellExe() in delphi.

Every thing works fine. The exe run under DOS and the thing that I want happend. BUT I cannot recieve the error message (if error occured) from the exe into my delphi project.

So, You have the C code and you know Delphi. Tell me how to recieve the errors from DOS EXEs.

LVL 20

Accepted Solution

Madshi earned 300 total points
ID: 6173130
I'm not sure at all, what your real problem is. If I understand your last comment correctly, you are executing a DOS application and want to get the output back. If that is the case, then the following function will perhaps help you. However, I don't see any connection of this to your original question...

Regards, Madshi.

function StartPiped(cmdLine: string) : string;
var sa     : TSecurityAttributes;
    si     : TStartupInfo;
    pi     : TProcessInformation;
    pr, pw : dword;
    dw1    : dword;
  result := '';
  with sa do begin
    nLength              := sizeOf(sa);
    bInheritHandle       := true;
    lpSecurityDescriptor := nil;
  CreatePipe(pr, pw, @sa, 0);
    ZeroMemory(@si, sizeOf(si));
    with si do begin
      cb          := sizeOf(si);
      wShowWindow := SW_HIDE;
      hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
      hStdOutput  := pw;
      hStdError   := pw;
    if CreateProcess(nil, pchar(cmdLine), nil, nil, true, 0, nil, nil, si, pi) then
        WaitForSingleObject(pi.hProcess, INFINITE);
        if PeekNamedPipe(pr, nil, 0, nil, @dw1, nil) and (dw1 > 0) then begin
          SetLength(result, dw1);
          if (not ReadFile(pr, pointer(result)^, dw1, dw1, nil)) or (dw1 = 0) then
            result := '';
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6173246
OoOoh Madshi, Long time didn't see your answers !! ( I am the absent;) )

In my first question I asked to translate a code, but as alexey said it is impossible I decided to use my already made exe (under DOS) made by the above C code. The problem that I found it that I cannot retrieve the error messages comes out from the exe.

If the exe did his work without any problem, Nothing is output.

If the exe faced a problem, the error message will be the output.

you can see the error messages that may occure on my first question's code.

LVL 20

Expert Comment

ID: 6173465

Have you tried the function I've posted? It might do exactly what you want, namely return the error string a an empty string (for success).

Regards, Madshi.

Author Comment

ID: 6173632

I tried you function. My system got HANG ;)

tell me how to use it.

I have the file decrypt.exe which receive 2 parameters like

d:\decrypt.exe d:\file1.txt d:\file2.txt

LVL 20

Expert Comment

ID: 6173786
Does the DOS exe need input? In that case you can't use SW_HIDE, please try it with:

     wShowWindow := SW_SHOWNORMAL;

Regards, Madshi.

Expert Comment

ID: 6901335
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
Thank you everyone.
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
LVL 14

Expert Comment

ID: 9285670
No comment has been added lately (526 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to Madshi http:#6173786

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --


EE Cleanup Volunteer

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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