Skip to content

Instantly share code, notes, and snippets.

@jeremy5189
Last active August 29, 2015 13:59
Show Gist options
  • Save jeremy5189/10530347 to your computer and use it in GitHub Desktop.
Save jeremy5189/10530347 to your computer and use it in GitHub Desktop.
/*
* =====================================================================================
*
* Filename: base64.c
*
* Description: Perform Base64 String Encode & Decode
*
* Version: 1.0
* Created: 2014/04/12 19時08分52秒
* Revision: none
* Compiler: gcc -o base64 base64.c
*
* Author: Jeremy Yen ([email protected])
* Organization: NTUST-MIS
*
* =====================================================================================
*/
#include <stdio.h>
#include <string.h>
const char ch64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
void stopEncode( int equal_count ) {
if( equal_count == 1 )
printf("=");
else if( equal_count == 2)
printf("==");
printf("\n\nSelect an action (E)ncode, (D)ecode, (Q)uit : ");
}
int getCh64Index( char input ) {
int i;
for( i = 0; i <= 64; i++ ) {
if( ch64[i] == input ) {
break;
}
}
return i;
}
void displayChar( int input) {
if( input != 64 && input != 16 )
printf("%c", (char)input);
}
int main() {
char option;
printf("Select an action (E)ncode, (D)ecode, (Q)uit : ");
while( scanf( "%c", &option) ) {
if( option == 'E' ) {
// Read String
printf("\nInput String to Encode : \n");
char input[1000];
scanf("%s", input);
// Start Processing
int i, index, len = strlen(input);
printf("\nBase64 String: \n");
for( i = 0; i < len; i++ ) {
if( i % 3 == 0 ) { // 第一個字
index = input[i] >> 2; // First 6
printf("%c", ch64[index]);
if( i == len - 1 ) { // 準備停了
index = (input[i] & 0x03) << 4; // Last 2 + 4 Zero
index = index | 0x0;
printf("%c", ch64[index]);
stopEncode(2);
}
}
else if( i % 3 == 1 ) {
// Last 2 + First 4
index = input[i-1] & 0x03; // 取後面兩位
index = index << 4; // 左移四位
index = index | ((input[i] & 0xf0) >> 4 ); // 取前面四位,右移弄掉後面四個零
printf("%c", ch64[index]);
if( i == len - 1 ) { // 準備停了
index = input[i] & 0xf; // 取後四位
index = index << 2; // 左移兩位
index = index | 0x0; // 補兩個零
printf("%c", ch64[index]);
stopEncode(1);
}
} else if( i % 3 == 2 ) {
index = input[i-1] & 0xf; // 取後四位
index = index << 2; // 左移兩位
index = index | (input[i] >> 6); // 取下一個前兩位
printf("%c", ch64[index]);
index = input[i] & 0x3f; // 後六位
printf("%c", ch64[index]);
if( i == len - 1 ) { // 準備停了
stopEncode(0);
}
}
}
} else if( option == 'D' ) {
// Read String
printf("\nInput Base64 String to Decode : \n");
char input[1000];
scanf("%s", input);
// Start Processing
int i, index, len = strlen(input);
printf("\nString: \n");
for( i = 0; i < len; i++ ) {
if( i % 4 == 0 ) {
int first = getCh64Index(input[i]);
first = first << 2;
int second = getCh64Index(input[i+1]);
second = second & (0x3 << 4); // 取前兩位
second = second >> 4;
displayChar(first|second);
} else if ( i % 4 == 1 ) {
int first = getCh64Index(input[i]);
first = first & 0xf; // 後四位
first = first << 4;
int second = getCh64Index(input[i+1]);
second = second >> 2;
displayChar(first|second);
} else if( i % 4 == 2 ) {
int first = getCh64Index(input[i]);
first = first & 0x3; // 後兩位
first = first << 6;
int second = getCh64Index(input[i+1]);
displayChar(first|second);
}
}
printf("\n\n");
printf("Select an action (E)ncode, (D)ecode, (Q)uit : ");
} else if( option == 'Q' ) {
// 結束
break;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment