Synapse AES encryption lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Turbonobo · 2014-10-16 15:49 Hi, I was making a simple application, just trying to figure out how i could use TSynaAes. But i couldn't get it to work properly... am I getting something wrong? (probably) If I use EncryptCFBblock / DecryptCFBblock the first 128 bits end up being nonsense. code: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); var  PrivKey  : AnsiString;  aes : TSynaAes;  a,b,c    : AnsiString; begin   PrivKey := SHA1('secretpassword');   aes := TSynaAes.Create(PadString(PrivKey, 8, #0));   try     aes.SetIV('somerandomstring');     a := memo1.Lines.Text;     //128bit block behaves as expected     b := aes.EncryptECB(a);     c := aes.DecryptECB(b);     memo2.Lines.Text := c;     //replaces first 128bits with crap     b := aes.EncryptCFBblock(a);     c := aes.DecryptCFBblock(b);     memo3.Lines.Text := c;   finally     aes.free;   end; end; the content of memo1.Lines.Text  (=the input) is: Code: [Select]Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. My code is 'inspired' by the use of TSynaAes in snmpsend.pas I tried with the trunk version and synapse40, same result. Using Lazarus 1.2.6 on Windows 7 64 bit ---------------------------------------------------------------------- ChrisF · 2014-10-16 16:44 Sorry, I don't know Synapse, but just having a quick glance at your code, I find a bit surprising the size of your AES key. AES keys are 16, 24 or 32 bytes length (generally 16). Apparently you are trying to set it to 8 bytes. Which could mean that your key has random ? bytes for its last 8 bytes. ** EDIT ** BTW, have you tried to use Encrypt/DecryptCFBblock before using Encrypt/DecryptECB ? Just in case your first calls are modifying 'something' in your aes object. ---------------------------------------------------------------------- Turbonobo · 2014-10-16 17:10 Indeed it should be replaced by Code: [Select]PadString(PrivKey, 16, #0); but still... the behavior is more or less the same, the nonsense is just different nonsense. ---------------------------------------------------------------------- ChrisF · 2014-10-16 17:20 Well, it's possible it's related to your TSynaAes - which data are modified/preserved (for instance your IV) ? - do some properties of your aes object need a defined size (for instance 16 bytes for your IV) ? - ... ---------------------------------------------------------------------- ChrisF · 2014-10-16 17:36 You could also make a try by forcing to reload your IV. Something like: Code: [Select]    a := memo1.Lines.Text;     aes.SetIV('somerandomstring');     b := aes.EncryptCFBblock(a);     aes.SetIV('somerandomstring');     c := aes.DecryptCFBblock(b);     memo3.Lines.Text := c; **EDIT** I guess your IV is probably modified during the encryption/decryption process. Which could explain why the first 128 bits (i.e. first 16 bytes) of your output data are garbage: they are coming from the incorrect IV (i.e. modified IV). ---------------------------------------------------------------------- Turbonobo · 2014-10-17 07:39 That worked! The IV is indeed modified by EncryptCFBblock. (apparently it has to be modified for every 128 bit block to be secure) It is a bit embarrassing that I didn't see that myself Now I can try to use other key sizes and find ways to make secure random values. I am new to AES, before I used some simple self-made xor encryption. (rather obscuring than encrypting) You have been very helpful! ---------------------------------------------------------------------- BigChimp · 2014-10-17 07:49 Thanks for posting - but I want more - for free... and now Would it be out of the question to post example code that demonstrates this? E.g. here or on the wiki? I'd be happy to move it to the wiki in the former case: it would be useful to inform users there's more choices than DCPCrypt...