<div dir="ltr">This and some other recent posts (`Is this a bug?`, `Hopefully a simple question...`). If you want help (and help other ppl who search for similar issues), could you please make the subject more descriptive? <div><br><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 20, 2017 at 12:19 AM, Chris M. via Digitalmars-d-learn <span dir="ltr"><<a href="mailto:digitalmars-d-learn@puremagic.com" target="_blank">digitalmars-d-learn@puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have no idea if this is an issue with D, or OpenSSL, or if I'm just doing something completely wrong. I'm writing a program that will either encrypt or decrypt a string using AES in ECB mode (for a school assignment) and it's giving me a very strange bug.<br>
<br>
encrypt and decrypt are both bools, either one or the other is set based on command-line arguments passed to the program. aesKey is a 128-bit value hashed from user input, and is (obviously) used as the key for encryption. inputStr is either the string to be encrypted or decrypted (also passed in as a command-line argument). I grabbed the OpenSSL bindings from Deimos to do this.<br>
<br>
if(encrypt)<br>
{<br>
    AES_KEY encKey;<br>
    auto encOut = new ubyte[inputStr.length];<br>
<br>
    // Encrypt and convert to base64<br>
    AES_set_encrypt_key(aesKey.ptr<wbr>, aesKey.sizeof * 8, &encKey);<br>
    AES_ecb_encrypt(inputStr.ptr, encOut.ptr, &encKey, AES_ENCRYPT);<br>
    writeln(Base64.encode(encOut))<wbr>;<br>
}<br>
else if(decrypt) // I'd leave this as else, but it's here for explanation purposes<br>
{<br>
    AES_KEY decKey;<br>
    auto decLength = Base64.decodeLength(inputStr.l<wbr>ength);<br>
    auto decB64 = new ubyte[decLength], decOut = new ubyte[decLength];<br>
<br>
    // convert back from base64 and decrypt<br>
    decB64 = Base64.decode(inputStr); // Yes I checked, and decB64 has exact the same contents as encOut from the if block<br>
    AES_set_decrypt_key(aesKey.ptr<wbr>, aesKey.sizeof * 8, &decKey);<br>
    AES_ecb_encrypt(decB64.ptr, decOut.ptr, &decKey, AES_DECRYPT);<br>
    writeln(cast(char[]) decOut);<br>
}<br>
<br>
However, this isn't working for a very strange reason (spits back garbage instead of the string I originally encrypted).<br>
<br>
Here's the problem. I tried running this without the if-else statements (i.e. encrypting and decrypting all in one run of the program, code below). If I leave in the base64 encoding and decoding, and use decB64 as the input to decrypt, it still doesn't work. However, if I decrypt with encOut directly, or assign encOut to decB64, it somehow works.<br>
<br>
AES_KEY encKey;<br>
auto encOut = new ubyte[inputStr.length];<br>
<br>
// Encrypt and convert to base64<br>
AES_set_encrypt_key(aesKey.ptr<wbr>, aesKey.sizeof * 8, &encKey);<br>
AES_ecb_encrypt(inputStr.ptr, encOut.ptr, &encKey, AES_ENCRYPT);<br>
<br>
auto decLength = Base64.decodeLength(Base64.enc<wbr>ode(encOut).length);<br>
AES_KEY decKey;<br>
auto decB64 = new ubyte[decLength], decOut = new ubyte[decLength];<br>
<br>
// convert back from base64 and decrypt<br>
decB64 = Base64.decode(Base64.encode(en<wbr>cOut));<br>
// doesn't work unless I uncomment out the following line, or just use encOut directly<br>
//decB64 = encOut;<br>
AES_set_decrypt_key(aesKey.ptr<wbr>, aesKey.sizeof * 8, &decKey);<br>
AES_ecb_encrypt(decB64.ptr, decOut.ptr, &decKey, AES_DECRYPT);<br>
writeln(cast(char[]) decOut);<br>
<br>
tl;dr The decryption doesn't work unless I pass it the exact same buffer (including not only contents, but apparently the exact same memory address) that I used to receive output from encryption<br>
<br>
Does anyone have any idea where the issue may lie, or how I could fix this?<br>
<br>
Here's the full program if you want to take a look<br>
<a href="http://pastebin.com/KyY103Ac" rel="noreferrer" target="_blank">http://pastebin.com/KyY103Ac</a><br>
</blockquote></div><br></div>