Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // decrypt.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- bool getBit(char value, char index);
- char getOutput(char input, char secret);
- int main() {
- char crypted_message[611];
- FILE* fp;
- fp = fopen("C:\\Users\\0xCC\\source\\repos\\decrypt\\Debug\\secret.enc", "r");
- if (fp == NULL) {
- printf("Error while opening the file.\n");
- return 0;
- }
- int index = 0;
- char ch;
- while ((ch = fgetc(fp)) != EOF) {
- crypted_message[index] = ch;
- index++;
- }
- fclose(fp);
- int message_len = index;
- //decrypt
- for (size_t key = 0; key < 256; key++) {
- char* decrypted_message = (char*)malloc(message_len);
- memset(decrypted_message, 0, message_len);
- for (size_t i = 0; i < message_len; i++) {
- for (size_t j = 0; j < 256; j++) {
- if (getOutput(j, key) == crypted_message[i]) {
- decrypted_message[i] = (char)j;
- break;
- }
- }
- }
- if (strstr(decrypted_message, "shellmates") != NULL) {
- printf("The message is:\n%s", decrypted_message);
- free(decrypted_message);
- return 0;
- }
- free(decrypted_message);
- }
- }
- bool getBit(char value, char index) {
- return value & (1 << index);
- }
- bool is_printable(char ch) {
- unsigned char c = (unsigned char)ch;
- if (c >= 0x20 && c <= 0x7f) {
- return true;
- }
- if (c == 0) {
- return true;
- }
- return false;
- }
- char getOutput(char input, char secret) {
- char output = 0;
- output |= (!(getBit(secret, 0) ^ (getBit(input, 6) ^ getBit(input, 1)))) << 0;
- output |= (!(getBit(secret, 1) ^ (getBit(input, 7) ^ getBit(input, 0)))) << 1;
- output |= (!(getBit(secret, 2) ^ (getBit(input, 4) ^ getBit(input, 3)))) << 2;
- output |= (!(getBit(secret, 3) ^ (getBit(input, 5) ^ getBit(input, 2)))) << 3;
- output |= (!(getBit(secret, 4) ^ getBit(input, 5))) << 4;
- output |= (!(getBit(secret, 5) ^ getBit(input, 4))) << 5;
- output |= (!(getBit(secret, 6) ^ getBit(input, 7))) << 6;
- output |= (!(getBit(secret, 7) ^ getBit(input, 6))) << 7;
- return output;
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement