寻求可以在Delphi和C中处理整个文件en/de-crypt的单文件加密实现

Maw*_*awg 11 c delphi encryption microcontroller aes

[更新]我正在为此提供奖金.坦率地说,我不关心使用哪种加密方法.最好像XTEA,RC4,BlowFish这样的东西...但你选择了.

我想要最小的努力,最好只是将文件放入我的项目并构建.

理想情况下,您应该已经使用代码在Delphi和C中对文件进行加密/解密(我想在Atmel UC3微处理器(C编码)和Windows PC(Delphi编码)之间交换文件. -de-crypt两个方向).

非常喜欢单个.PAS单元和单个.C/.H文件.我不想使用DLL或支持几十种加密算法的库,只需要一个(我当然不希望安装程序有任何东西).

我希望我在这里听起来不太挑剔,但我一直在谷歌搜索和尝试代码超过一个星期仍然找不到两个匹配的实现.我怀疑只有已经做过这件事的人可以帮助我......

提前致谢.


作为我上一篇文章的后续内容,我仍然在寻找一些非常简单的代码,为什么我可以 - 用最少的努力 - 加密文件并在PC上的Delphi和Atmel UC3 u-processor上的C之间进行交换.

这在理论上听起来很简单,但在实践中这是一场噩梦.有很多可能的候选人,我花了几天谷歌搜索并试用它们 - 无济于事.

有些是humonous库,支持许多加密算法,我想要一些轻量级的东西(特别是在C/u处理器端).

有些看起来很好,但是一组源只提供块操作,其他字符串(我更喜欢整个文件en/de-crypt).

大多数文档似乎记录很差,没有无意义的参数名称,也没有调用函数的示例代码.

在过去的周末(再加上几天),我已经烧掉了大量的XTEA,XXTEA和BlowFish实现,但是虽然我可以加密,但我无法逆转这个过程.

现在我在看AES-256.有谁知道C中的实现是一个AES.C文件?(当然还有AES.H)

坦率地说,我将采取任何可以在Delphi和C之间进行整个文件加/解的方法,但除非任何人自己实际完成此操作,否则我希望只听到"符合标准的任何实现都应该这样做" - 这是一个不错的选择理论但只是没有为我工作:-(

C中有任何简单的AES-256吗?我有一些看起来很合理的Delphi代码,但在我一起尝试之前不会确定.

提前致谢 ...

Gen*_*ene 1

这是 RC4 代码。它非常轻。

C 已在生产系统中使用了五年。

我添加了经过轻微测试的 Delphi 代码。Pascal 是一个逐行移植,带有unsigned chargo to Byte。我只在打开了 Delphi 选项的 Free Pascal 中运行 Pascal,而不是 Delphi 本身。C 和 Pascal 都有简单的文件处理器。

对密文进行加扰可以恢复原始的明文。

迄今为止没有报告任何错误。希望这能解决您的问题。

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

这是经过轻微测试的 Pascal。我可以用 C 语言对源代码进行打乱,然后用 Pascal 实现对其进行解扰,就可以了。

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}
Run Code Online (Sandbox Code Playgroud)