Guest User

Untitled

a guest
Dec 22nd, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. char* base64_encode(char* plain) {
  2.  
  3.     char counts = 0;
  4.     char buffer[3];
  5.     char* cipher = malloc(strlen(plain) * 4 / 3 + 4);
  6.     int i = 0, c = 0;
  7.  
  8.     for(i = 0; plain[i] != '\0'; i++) {
  9.         buffer[counts++] = plain[i];
  10.         if(counts == 3) {
  11.             cipher[c++] = base46_map[buffer[0] >> 2];
  12.             cipher[c++] = base46_map[((buffer[0] & 0x03) << 4) + (buffer[1] >> 4)];
  13.             cipher[c++] = base46_map[((buffer[1] & 0x0f) << 2) + (buffer[2] >> 6)];
  14.             cipher[c++] = base46_map[buffer[2] & 0x3f];
  15.             counts = 0;
  16.         }
  17.     }
  18.  
  19.     if(counts > 0) {
  20.         cipher[c++] = base46_map[buffer[0] >> 2];
  21.         if(counts == 1) {
  22.             cipher[c++] = base46_map[(buffer[0] & 0x03) << 4];
  23.             cipher[c++] = '=';
  24.         } else {                      // if counts == 2
  25.             cipher[c++] = base46_map[((buffer[0] & 0x03) << 4) + (buffer[1] >> 4)];
  26.             cipher[c++] = base46_map[(buffer[1] & 0x0f) << 2];
  27.         }
  28.         cipher[c++] = '=';
  29.     }
  30.  
  31.     cipher[c] = '\0';   /* string padding character */
  32.     return cipher;
  33. }
Add Comment
Please, Sign In to add comment