The record of base64 encoding and decoding after des CBC encryption and decryption using openssl Library under linux c
Decoding and encoding process
In the process, the encrypted xml document is sent to the server by HTTP Request, and then the encrypted message returned by the server is decrypted and decoded.
The required library is "include < OpenSSL / EVP. H >
Des CBC decryption encryption code
// Des CBC encryption int kk_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv); printf("key:%s,iv:%s\r\n",key,iv); EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len); ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } // Des CBC decryption int kk_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv); printf("key:%s,iv:%s\r\n",key,iv); EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len); ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len); ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; }
base64 decoding group code
// base64 encoding char * base64Encode(const char *buffer, int length, bool newLine) { BIO *bmem = NULL; BIO *b64 = NULL; BUF_MEM *bptr; b64 = BIO_new(BIO_f_base64()); if (!newLine) { BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); } bmem = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bmem); BIO_write(b64, buffer, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); BIO_set_close(b64, BIO_NOCLOSE); char *buff = (char *)malloc(bptr->length + 1); memcpy(buff, bptr->data, bptr->length); buff[bptr->length] = 0; BIO_free_all(b64); return buff; } //base64 decoding char * base64Decode(char *input, int length, bool newLine) { BIO *b64 = NULL; BIO *bmem = NULL; char *buffer = (char *)malloc(length); memset(buffer, 0, length); b64 = BIO_new(BIO_f_base64()); if (!newLine) { BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); } bmem = BIO_new_mem_buf(input, length); bmem = BIO_push(b64, bmem); BIO_read(bmem, buffer, length); BIO_free_all(bmem); return buffer; }
main function
Here is only the encrypted code. If you need to decrypt and decode, you can call the remaining two functions.
//====================================================================== void des_decode(char* text, int ciphertext_len,char* dest) { bool newLine = false; unsigned char key[32] = {0}; unsigned char iv[32] = {0}; unsigned char decryptedtext[1024*10]; int decryptedtext_len ; snprintf((char*)key,32,"53MP236N"); snprintf((char*)iv,32,"53MP236N"); char * decode = base64Decode(text,ciphertext_len, newLine); decryptedtext_len = kk_decrypt((unsigned char*)decode,ciphertext_len , key, iv,decryptedtext); snprintf(dest,ciphertext_len,"%s",decryptedtext); // return decryptedtext ; } char* des_encode( unsigned char* text, int len) { bool newLine = false; int ciphertext_len ; unsigned char key[32] = {0}; unsigned char iv[32] = {0}; unsigned char ciphertext[1024*10]; snprintf((char*)key,32,"53MP236N"); snprintf((char*)iv,32,"53MP236N"); ciphertext_len = kk_encrypt (text, strlen((char *)text), key, iv,ciphertext); char* encode = base64Encode((char*)ciphertext,ciphertext_len, newLine); return encode ; } int main(int argc, char **argv) { int oc; char* cfgFileName = NULL; char* serviceName = NULL; char* infile = NULL; char* defaultStYYYYMM = NULL; char* defaultEndYYYYMM = NULL; char* logFileNamePrefix = NULL; char result[2048] = {0}; FILE* fp = NULL; fp = fopen(argv[1], "r"); char xml[1024*10] = {0}; int len = fread(xml, 1, sizeof(xml), fp); // xml[len-1] = '\0'; fclose(fp); while((oc = getopt(argc, argv, "c:s:f:l:")) != -1) { switch(oc) { case 'c': cfgFileName = optarg; break; case 'f': infile = optarg; break; } } unsigned char text[1024*10] = {0}; snprintf((char*)text,sizeof(text),"%s",xml); printf("text is %s\n\r",text); // text[strlen(text) - 1] = '\0'; char*encode = des_encode(text,strlen((char*)text)); printf("encode:[[%s]]\n",encode); }
Enter command after compilation
./code base64.xml