Skip to content

Instantly share code, notes, and snippets.

@Mroik
Last active October 24, 2024 00:43
Show Gist options
  • Save Mroik/9fd91bc1fa978c14e48b1499664ea39a to your computer and use it in GitHub Desktop.
Save Mroik/9fd91bc1fa978c14e48b1499664ea39a to your computer and use it in GitHub Desktop.
Expected output `Y2lhbyBjb21lIHN0YWk/`
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void encode(unsigned char data[21])
{
unsigned char base[15];
memcpy(base, data, 15);
for(int x = 0; x < 20; x++) {
unsigned char temp = 0b11111100;
temp &= base[0];
temp = temp >> 2;
if(temp < 26) {
data[x] = temp + 'A';
} else if(temp >= 26 && temp <= 51) {
data[x] = temp - 26 + 'a';
} else if(temp >= 52 && temp <= 61) {
data[x] = temp - 52 + '0';
} else if(temp == 62) {
data[x] = '+';
} else if(temp == 63) {
data[x] = '/';
}
// Shift everything left
for(int y = 0; y < 14; y++) {
base[y] = base[y] << 6;
temp = 0b11111100;
temp &= base[y + 1];
temp = temp >> 2;
base[y] |= temp;
}
base[14] = base[14] << 6;
}
data[20] = '\0';
}
int main()
{
unsigned char data[21] = "ciao come stai?";
encode(data);
printf("%s\n", data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment