Need Help with AES-256-CBC

So I'm trying to create a wrapper function for handling AES-256-CBC on a const char* value. I'm kinda new to C++ and trying to read docs from various sites such has Crypto++ has been a real pain as nobody really seems to know how to actually write good doccumentation for it.

anyways I'm trying to make a function that takes in 3 arguments and returns a const char* value.

I'll post what I have so far bellow but what I want the function to do is:

Use an int variable to determin if it should encrypt or decrypt.
Use a const char* for the key used in decrypting/encrypting.
Use a const char* for the data I want to decrypt/encrypt.

and then it returns a const char* of the data after its gone through AES-256-CBC.

1
2
3
4
5
6
7
8
9
10
const char* Cryptor_Wrapper(int mode,const char* key,const char* string){
	char* output;

	if(mode == 1){
		// Encrypt string and return it as output
	}else{
		// Decrypt string and return it as output
	}
	return output;
}


So far I've tried looking into using Crypto++, tiny-AES-c and have tried getting help with figuring out the doccumentation from people on stackoverflow. so far all attempts at finding somebody who can just explain how to actually do this have failed. along with every attempt to find a site that has easy to understand doccumentation or a library thats simple to use have also failed.
One obvious problem is that output is a local variable that goes out of scope when the function exits. You need to dynamically allocate output. You may have been planing to do that, but did not show it. The problem with doing that is that it places the burden of releasing the buffer upon the caller. It would be better to have the caller provide the output buffer and pass it as an argument.

I suspect you're running into argument differences between your wrapper and the encryption routines. I'm not familiar with the specific routines you're using. I use the SHA512 routines.
The calling sequences are:
1
2
3
4
5
6
7
extern int SHA256Reset(SHA256Context *);
extern int SHA256Input(SHA256Context *, const uint8_t *bytes,
                       unsigned int bytecount);
extern int SHA256FinalBits(SHA256Context *, const uint8_t bits,
                           unsigned int bitcount);
extern int SHA256Result(SHA256Context *,
                        uint8_t Message_Digest[SHA256HashSize]);

There are also SHA384 and SHA512 routines. The calling sequences are the same.
The SHAxxx routines are a one-way hash. There is no decrypting the result.
I would expect that your AES-256-CBC routines would have similar calling sequences.

Note that Context is passed to each call. You call SHAxxxReset to clear the context before you start. Note that these routines need to know how long the input message is. You don't provide that.

edit:
A quick google came up with the following calling sequence, pretty much identical to the above:
 
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx,uint8_t* buf, uint32_t length)


Last edited on
Topic archived. No new replies allowed.