#include #include #include char base16[] = "0123456789ABCDEF"; struct Secret { unsigned char bytes[16]; char* encode() { static char s[33]; for (int i = 0; i < 16; i++) { s[i*2] = base16[(bytes[i] >> 4) & 15]; s[i*2+1] = base16[bytes[i] & 15]; } s[32] = '\0'; return s; } void decode(char* s){ for(int i = 0; i < 16; i++) { bytes[i] = ((strchr(base16, s[i*2]) - base16) << 4) | (strchr(base16, s[i*2+1]) - base16); } } void load(char* file) { FILE* f = fopen(file, "r"); fread(bytes, 1, 16, f); fclose(f); } void save(char* file) { FILE* f = fopen(file, "w"); fwrite(bytes, 1, 16, f); fclose(f); } void generate() { std::srand(std::chrono::high_resolution_clock::now().time_since_epoch().count()); int r, i, j; for (i = 0; i < 16; i += 4) { r = std::rand(); for (j = 0; j < 4; j++) { bytes[i + j] = r & 255; r >>= 8; } } } }; struct otp { }; int main() { Secret secret; secret.generate(); for (int i = 0; i < 16; i++) { printf("%u ", secret.bytes[i]); } char* s = secret.encode(); printf("\n%s\n", s); secret.decode(s); for (int i = 0; i < 16; i++) { printf("%u ", secret.bytes[i]); } s = secret.encode(); printf("\n%s\n", s); return 0; }