digitalmars.D.learn - AES encryption with openssl bindings
- brad clawsie (21/21) Apr 25 2014 hi everyone.
- Justin Whear (4/31) Apr 25 2014 It doesn't look like you're allocating space for `e` or `d`, e.g. `auto ...
- bearophile (7/15) Apr 25 2014 Thankfully we have slices in D. So better to write little wrapper
- hane (2/2) Apr 26 2014 AES_set_decrypt_key is needed before AES_decrypt.
- Kagamin (2/3) Apr 26 2014 How do you want it to fail? C code doesn't throw exceptions.
hi everyone. I'm trying to symmetrically encrypt some text using the openssl bindings. My code compiles and fails silently. Clearly there is something very wrong with it - it could be my novice D skills, or my misuse of the openssl binding. auto chunk = new ubyte[](16); foreach(ref x; chunk) x = uniform!"[]"(ubyte.min, ubyte.max); AES_KEY wctx; AES_set_encrypt_key(chunk.ptr,128,&wctx); string s = "virident"; ubyte[] b; b = cast(ubyte[]) s; ubyte[] e; AES_encrypt(b.ptr,e.ptr,&wctx); ubyte[] d; AES_decrypt(e.ptr,d.ptr,&wctx); writefln("%s",d); Any clues? I am a D novice, so any spoonfeeding you could provide would be helpful :) thanks! Brad
Apr 25 2014
On Fri, 25 Apr 2014 19:06:31 +0000, brad clawsie wrote:hi everyone. I'm trying to symmetrically encrypt some text using the openssl bindings. My code compiles and fails silently. Clearly there is something very wrong with it - it could be my novice D skills, or my misuse of the openssl binding. auto chunk = new ubyte[](16); foreach(ref x; chunk) x = uniform!"[]"(ubyte.min, ubyte.max); AES_KEY wctx; AES_set_encrypt_key(chunk.ptr,128,&wctx); string s = "virident"; ubyte[] b; b = cast(ubyte[]) s; ubyte[] e; AES_encrypt(b.ptr,e.ptr,&wctx); ubyte[] d; AES_decrypt(e.ptr,d.ptr,&wctx); writefln("%s",d); Any clues? I am a D novice, so any spoonfeeding you could provide would be helpful :) thanks! BradIt doesn't look like you're allocating space for `e` or `d`, e.g. `auto e = new ubyte[](256);`, so those arrays have a length of 0, in which case the encrypt/decrypt functions are just trashing their way through memory.
Apr 25 2014
Justin Whear:brad clawsie:Better to use std.string.representation.b = cast(ubyte[]) s;It doesn't look like you're allocating space for `e` or `d`, e.g. `auto e = new ubyte[](256);`, so those arrays have a length of 0, in which case the encrypt/decrypt functions are just trashing their way through memory.Thankfully we have slices in D. So better to write little wrapper functions, make them the only public functions in a module and use them only. Bye, bearophile
Apr 25 2014
AES_set_decrypt_key is needed before AES_decrypt. AES_set_decrypt_key(chunk.ptr, 128, &wctx);
Apr 26 2014
On Friday, 25 April 2014 at 19:06:33 UTC, brad clawsie wrote:My code compiles and fails silently.How do you want it to fail? C code doesn't throw exceptions.
Apr 26 2014