Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:51:41+00:00 2026-06-16T01:51:41+00:00

[Update] I am offering a bonus for this. Frankly, I don’t care which encryption

  • 0

[Update] I am offering a bonus for this. Frankly, I don’t care which encryption method is used. Preferably something simple like XTEA, RC4, BlowFish … but you chose.

I want minimum effort on my part, preferably just drop the files into my projects and build.

Idealy you should already have used the code to en/de-crypt a file in Delphi and C (I want to trade files between an Atmel UC3 micro-processor (coding in C) and a Windows PC (coding in Delphi) en-and-de-crypt in both directions).

I have a strong preference for a single .PAS unit and a single .C/.H file. I do not want to use a DLL or a library supporting dozens of encryption algorithms, just one (and I certainly don’t want anything with an install program).

I hope that I don’t sound too picky here, but I have been googling & trying code for over a week and still can’t find two implementations which match. I suspect that only someone who has already done this can help me …

Thanks in advance.


As a follow up to my previous post, I am still looking for some very simple code with why I can – with minimal effort – en-de crypt a file and exchange it between Delphi on a PC and C on an Atmel UC3 u-processor.

It sounds simple in theory, but in practice it’s a nightmare. There are many possible candidates and I have spend days googling and trying them out – to no avail.

Some are humonous libraries, supporting many encryption algorithms, and I want something lightweight (especially on the C / u-processor end).

Some look good, but one set of source offers only block manipulation, the other strings (I would prefer whole file en/de-crypt).

Most seem to be very poorly documented, with meaningless parameter names and no example code to call the functions.

Over the past weekend (plus a few more days), I have burned my way through a slew of XTEA, XXTEA and BlowFish implementations, but while I can encrypt, I can’t reverse the process.

Now I am looking at AES-256. Dos anyone know of an implementation in C which is a single AES.C file? (plus AES.H, of course)

Frankly, I will take anything that will do whole file en/de-crypt between Delphi and C, but unless anyone has actually done this themselves, I expect to hear only “any implementation that meets the standard should do” – which is a nice theoory but just not working out for me 🙁

Any simple AES-256 in C out there? I have some reasonable looking Delphi code, but won’t be sure until I try them together.

Thanks in advance …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:51:42+00:00Added an answer on June 16, 2026 at 1:51 am

    Here is RC4 code. It is very lightweight.

    The C has been used in a production system for five years.

    I have added lightly tested Delphi code. The Pascal is a line-by-line port with with unsigned char going to Byte. I have only run the Pascal in Free Pascal with Delphi option turned on, not Delphi itself. Both C and Pascal have simple file processors.

    Scrambling the ciphertext gives the original cleartext back.

    No bugs reported to date. Hope this solves your problem.

    rc4.h

    #ifndef RC4_H
    #define RC4_H
    
    /*
     * rc4.h -- Declarations for a simple rc4 encryption/decryption implementation.
     * The code was inspired by libtomcrypt.  See www.libtomcrypt.org.
     */
    typedef struct TRC4State_s {
      int x, y;
      unsigned char buf[256];
    } TRC4State;
    
    /* rc4.c */
    void init_rc4(TRC4State *state);
    void setup_rc4(TRC4State *state, char *key, int keylen);
    unsigned endecrypt_rc4(unsigned char *buf, unsigned len, TRC4State *state);
    
    #endif
    

    rc4.c

    void init_rc4(TRC4State *state)
    {
      int x;
      state->x = state->y = 0;
      for (x = 0; x < 256; x++)
        state->buf[x] = x;
    }
    
    void setup_rc4(TRC4State *state, char *key, int keylen)
    {
      unsigned tmp;
      int x, y;
    
      // use only first 256 characters of key 
      if (keylen > 256) 
        keylen = 256;
    
      for (x = y = 0; x < 256; x++) {
        y = (y + state->buf[x] + key[x % keylen]) & 255;
        tmp = state->buf[x]; 
        state->buf[x] = state->buf[y]; 
        state->buf[y] = tmp;
      }
      state->x = 255;
      state->y = y;
    }
    
    unsigned endecrypt_rc4(unsigned char *buf, unsigned len, TRC4State *state)
    {
      int x, y; 
      unsigned char *s, tmp;
      unsigned n;
    
      x = state->x;
      y = state->y;
      s = state->buf;
      n = len;
      while (n--) {
        x = (x + 1) & 255;
        y = (y + s[x]) & 255;
        tmp = s[x]; s[x] = s[y]; s[y] = tmp;
        tmp = (s[x] + s[y]) & 255;
        *buf++ ^= s[tmp];
      }
      state->x = x;
      state->y = y;
      return len;
    }
    
    int endecrypt_file(FILE *f_in, FILE *f_out, char *key)
    {
      TRC4State state[1];
      unsigned char buf[4096];
      size_t n_read, n_written;
    
      init_rc4(state);
      setup_rc4(state, key, strlen(key));
      do {
        n_read = fread(buf, 1, sizeof buf, f_in);
        endecrypt_rc4(buf, n_read, state);
        n_written = fwrite(buf, 1, n_read, f_out);
      } while (n_read == sizeof buf && n_written == n_read);
      return (n_written == n_read) ? 0 : 1;
    }
    
    int endecrypt_file_at(char *f_in_name, char *f_out_name, char *key)
    {
      int rtn;
    
      FILE *f_in = fopen(f_in_name, "rb");
      if (!f_in) {
        return 1;
      }
      FILE *f_out = fopen(f_out_name, "wb");
      if (!f_out) {
        close(f_in);
        return 2;
      }
      rtn = endecrypt_file(f_in, f_out, key);
      fclose(f_in);
      fclose(f_out);
      return rtn;
    }
    
    #ifdef TEST
    
    // Simple test.
    int main(void)
    {
      char *key = "This is the key!";
    
      endecrypt_file_at("rc4.pas", "rc4-scrambled.c", key);
      endecrypt_file_at("rc4-scrambled.c", "rc4-unscrambled.c", key);
      return 0;
    }
    #endif
    

    Here is lightly tested Pascal. I can scramble the source code in C and descramble it with the Pascal implementation just fine.

    type
      RC4State = record
        x, y : Integer;
        buf : array[0..255] of Byte;
      end;
    
      KeyString = String[255];
    
    procedure initRC4(var state : RC4State);
    var
      x : Integer;
    begin
      state.x := 0;
      state.y := 0;
      for x := 0 to 255 do
        state.buf[x] := Byte(x);
    end;
    
    procedure setupRC4(var state : RC4State; var key : KeyString);
    var
      tmp : Byte;
      x, y : Integer;
    begin
      y := 0;
      for x := 0 to 255 do begin
        y := (y + state.buf[x] + Integer(key[1 + x mod Length(key)])) and 255;
        tmp := state.buf[x];
        state.buf[x] := state.buf[y];
        state.buf[y] := tmp;
      end;
      state.x := 255;
      state.y := y;
    end;
    
    procedure endecryptRC4(var buf : array of Byte; len : Integer; var state : RC4State);
    var
      x, y, i : Integer;
      tmp : Byte;
    begin
      x := state.x;
      y := state.y;
      for i := 0 to len - 1 do begin
        x := (x + 1) and 255;
        y := (y + state.buf[x]) and 255;
        tmp := state.buf[x];
        state.buf[x] := state.buf[y];
        state.buf[y] := tmp;
        tmp := (state.buf[x] + state.buf[y]) and 255;
        buf[i] := buf[i] xor state.buf[tmp]
      end;
      state.x := x;
      state.y := y;
    end;
    
    procedure endecryptFile(var fIn, fOut : File; key : KeyString);
    var
      nRead, nWritten : Longword;
      buf : array[0..4095] of Byte;
      state : RC4State;
    begin
      initRC4(state);
      setupRC4(state, key);
      repeat
        BlockRead(fIN, buf, sizeof(buf), nRead);
        endecryptRC4(buf, nRead, state);
        BlockWrite(fOut, buf, nRead, nWritten);
      until (nRead <> sizeof(buf)) or (nRead <> nWritten);
    end;
    
    procedure endecryptFileAt(fInName, fOutName, key : String);
    var
      fIn, fOut : File;
    begin
      Assign(fIn, fInName);
      Assign(fOut, fOutName);
      Reset(fIn, 1);
      Rewrite(fOut, 1);
      endecryptFile(fIn, fOut, key);
      Close(fIn);
      Close(fOut);
    end;
    
    {$IFDEF TEST}
    // Very small test.
    const
      key = 'This is the key!';
    begin
      endecryptFileAt('rc4.pas', 'rc4-scrambled.pas', key);
      endecryptFileAt('rc4-scrambled.pas', 'rc4-unscrambled.pas', key);
    end.
    {$ENDIF}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update : This is no longer an issue from C# 6, which has introduced
Update 16th November 2012 I would like to raise this question again, offering with
Update: I reported this as a bug to Apple and they fixed it! All
UPDATE: I've been playing around with this more, and it seems like tmux's clear-history
UPDATE: I'm getting this error: (No route matches /docs/index.html... ) when accessing admin.example.com/docs/index.html The
UPDATE: It was suggested in the comments that I create a wiki for this.
UPDATE: I've spent way too much time on this and have decided to ditch
(Update: Problem caused by zsh, see accepted answer) Long ago, I followed this great
Update 2018 : This question was asked long before PostCSS existed, and I would
Update: The index.php file here: /public_html/d/index.php includes: /public_html/d/core/source/class.File1.php This Class.File1.php here has this include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.