Base64 encoding (C language Implementation)

Source: Internet
Author: User
Tags base64

base64-Wikipedia

Online Encryption and Decryption tool

#include <stdio.h> #include <stdint.h> #include <string.h> #include <malloc.h>char base64_ Table[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' Y '  , ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' + ', '-', ' + '};void base64_map (uint8_t *in_block, int len) {for (int i = 0; i < Len;        ++i) {In_block[i] = base64_table[in_block[i]];    printf ("%d%c", In_block[i], base64_table[in_block[i]);    } if (len% 4 = = 3) In_block[len] = ' = ';    else if (len% 4 = = 2) In_block[len] = in_block[len+1] = ' = '; return;}    void Base64_unmap (char *in_block) {int i;    Char *c;        for (int i = 0; i < 4; ++i) {c = in_block + i;            if (*c>= ' a ' && *c<= ' Z ') {*c-= ' a ';        Continue } if (*c>= ' a ' && *c<= ' Z ') {*c-= ' a ';            *c + = 26;        Continue            } if (*c = = ' + ') {*c = 62;        Continue            } if (*c = = '/') {*c = 63;        Continue            } if (*c = = ') {*c = 0;        Continue        } *c-= ' 0 ';    *c + = 52;    }}int Base64_encode (char *in, int inlen, uint8_t *out) {char *in_block;    uint8_t *out_block;    Char temp[3];    Out_block = out;    In_block = in;        for (int i = 0; i < inlen; i + = 3) {memset (temp, 0, 3);        memcpy (temp, In_block, i + 3 < Inlen 3:inlen-i);        memset (Out_block, 0, 4);        Out_block[0] = (Temp[0] >> 2) & 0x3f; OUT_BLOCK[1] = ((Temp[0] << 4) & 0x30) |        ((Temp[1] >> 4) & 0x0f); OUT_BLOCK[2] = ((Temp[1] << 2) & 0x3c) |        ((Temp[2] >> 6) & 0x03);        OUT_BLOCK[3] = (temp[2]) & 0x3f; printf ("%.2x%.2x%.2x\n", temp[0], temp[1], temp[2]);        printf ("%.2x%.2x%.2x%.2x\n", out_block[0], out_block[1], out_block[2], out_block[3]);        Out_block + = 4;    In_block + = 3; } Base64_map (Out, ((Inlen * 4)-1)/3 + 1);}    int Base64_decode (char *in, int inlen, uint8_t *out) {char *in_block;    uint8_t *out_block;    Char temp[4];    Out_block = out;    In_block = in;        for (int i = 0; i < inlen; i + = 4) {if (*in_block = = ' = ') return 0;        memcpy (temp, in_block, 4);        memset (Out_block, 0, 3);        Base64_unmap (temp); Out_block[0] = ((temp[0]<<2) & 0XFC) |        ((temp[1]>>4) & 3); OUT_BLOCK[1] = ((temp[1]<<4) & 0xf0) |        ((temp[2]>>2) & 0xf); OUT_BLOCK[2] = ((temp[2]<<6) & 0xc0) |        ((Temp[3]) & 0x3f);        Out_block + = 3;    In_block +=4; } return 0;}    int main () {char cipher_text[64];        while (scanf ("%s", cipher_text)! = EOF) {printf ("%s\n", Cipher_text); uint8_t *traN_cipher = (uint8_t *) malloc (sizeof (uint8_t) * 64);        memset (tran_cipher, 0, sizeof (uint8_t) * 64);        #define ENCODE #define DECODE #ifdef ENCODE printf ("----------------ENCODE-----------------");        Base64_encode (Cipher_text, strlen (Cipher_text), tran_cipher);        int len = (strlen (cipher_text) * 4-1)/3 + 1; Len = len% 4 = = 3?        Len + 1:len + 2;        for (int i = 0; i < len; ++i) printf ("%c", Tran_cipher[i]);        printf ("\ n");        #endif//ENCODE #ifdef DECODE printf ("----------------DECODE-----------------");        Base64_decode (Cipher_text, strlen (Cipher_text), tran_cipher);        Len = strlen (Cipher_text);        int n = len;        while (cipher_text[--n] = = ');        if (n = = len-2) len = (len >> 2) * 3-1;        else if (n = = len-3) len = (len >> 2) * 3-2;        else if (n = = len-1) len = (len >> 2) * 3; for (int i = 0; i < Len;        ++i) printf ("%c", Tran_cipher[i]);        printf ("\ n"); #endif//DECODE} return 0;}

  

Base64 encoding (C language Implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.