我想要最小的努力,最好只是将文件放入我的项目和构建.
理想情况下,您应该已经使用代码在Delphi和C中对文件进行解/隐藏(我想在Atmel UC3微处理器(C编码)和Windows PC(Delphi编码)之间交换文件-de-crypt在两个方向)
我非常喜欢单个.PAS单元和一个.C / .H文件.我不想使用一个支持数十种加密算法的DLL或一个库,只需一个(我肯定不想要任何安装程序).
我希望我在这里听起来不太挑剔,但我一直在搜索和搜索尝试代码一个多星期,仍然找不到两个匹配的实现.我怀疑只有一个已经做到这一点的人可以帮助我…
提前致谢.
随着my previous post的发展,我仍然在寻找一些非常简单的代码,为什么我可以用最少的努力,将一个文件隐藏起来,并在PC上的Delphi和Atmel UC3 u处理器的C之间进行交换.
在理论上听起来很简单,但实际上这是一场噩梦.有很多可能的候选人,我花了几天的时间,并尝试出去 – 无济于事.
有些是可变图书馆,支持许多加密算法,我想要一些轻量级(特别是在C / u处理器端).
一些看起来不错,但是一组源仅提供块操作,其他字符串(我更喜欢整个文件en / de-crypt).
大多数似乎是非常糟糕的文档,没有意义的参数名称和没有示例代码来调用这些功能.
在过去的一个周末(再加上几天),我已经烧了一大堆XTEA,XXTEA和BlowFish的实现,但是在加密的时候,我无法扭转这个过程.
现在我正在看AES-256. Dos任何人都知道C中的一个实现是一个单一的AES.C文件? (加上AES.H,当然)
坦白说,我会采取任何在Delphi和C之间执行整个文件en / de-crypt的东西,但是除非有人自己做了这些,否则我只希望听到“任何符合标准的实现都应该” – 这是一个很好的但是对我而言却是没有办法:-(
任何简单的AES-256在C那里?我有一些合理的Delphi代码,但一直在尝试之前不会确定.
提前致谢 …
解决方法
C已经在生产系统中使用了五年.
我添加了轻微测试的Delphi代码. Pascal是一个逐行端口,带有unsigned char到Byte.我只运行Pascal在Free Pascal与Delphi选项打开,而不是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
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,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,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,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
这里是轻微测试的帕斯卡.我可以在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,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}