javascript - CryptoJS to OpenSSL encryption conversion -
i need port javascript code c99 backend. in javascript code use cryptojs perform aes encryption:
var key = cryptojs.md5(math.random().tostring()).tostring(); key = cryptojs.enc.hex.parse(key); var iv = cryptojs.md5(math.random().tostring()).tostring(); iv = cryptojs.enc.hex.parse(iv); var data = "testdata"; var encrypted = cryptojs.aes.encrypt(data, key, { iv:iv, mode:cryptojs.mode.cbc, padding:cryptojs.pad.zeropadding });
now need implement same code in c openssl library. have created random md5 strings without problem cannot see how implement aes encryption 0 padding. have:
bool crypto_aes_encrypt(const char* data, unsigned char *key, unsigned char *iv, unsigned char **out_buf, int *out_buf_len) { err_load_crypto_strings(); openssl_add_all_algorithms_conf(); /* initialize cipher context */ evp_cipher_ctx *ctx = evp_cipher_ctx_new(); if(!ctx) { return null; } /* initialize aes encryption */ if(evp_encryptinit_ex(ctx, evp_aes_256_cbc(), null, key, iv) != 1) { return null; } /* encrypt data */ *out_buf = (unsigned char*) malloc(2048 * sizeof(char)); *out_buf_len = 0; if(evp_encryptupdate(ctx, *out_buf, out_buf_len, (const unsigned char*) data, strlen(data)) != 1) { free(out_buf); return null; } if(evp_encryptfinal(ctx, *out_buf + *out_buf_len, out_buf_len) != 1) { free(out_buf); return null; } evp_cipher_ctx_free(ctx); evp_cleanup(); err_free_strings(); return true; }
how can implement zeropadding in openssl used in cryptojs?
Comments
Post a Comment