Advertisement
AymenSekhri

Untitled

Apr 4th, 2020
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. // decrypt.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. bool getBit(char value, char index);
  8. char getOutput(char input, char secret);
  9.  
  10. int main() {
  11.  
  12.     char crypted_message[611];
  13.     FILE* fp;
  14.     fp = fopen("C:\\Users\\0xCC\\source\\repos\\decrypt\\Debug\\secret.enc", "r");
  15.     if (fp == NULL) {
  16.         printf("Error while opening the file.\n");
  17.         return 0;
  18.     }
  19.     int index = 0;
  20.     char ch;
  21.     while ((ch = fgetc(fp)) != EOF) {
  22.         crypted_message[index] = ch;
  23.         index++;
  24.     }
  25.     fclose(fp);
  26.     int message_len = index;
  27.  
  28.     //decrypt
  29.     for (size_t key = 0; key < 256; key++) {
  30.         char* decrypted_message = (char*)malloc(message_len);
  31.         memset(decrypted_message, 0, message_len);
  32.         for (size_t i = 0; i < message_len; i++) {
  33.             for (size_t j = 0; j < 256; j++) {
  34.                 if (getOutput(j, key) == crypted_message[i]) {
  35.                     decrypted_message[i] = (char)j;
  36.                     break;
  37.                 }
  38.             }
  39.            
  40.         }
  41.         if (strstr(decrypted_message, "shellmates") != NULL) {
  42.             printf("The message is:\n%s", decrypted_message);
  43.             free(decrypted_message);
  44.             return 0;
  45.         }
  46.         free(decrypted_message);
  47.     }
  48.  
  49.  
  50.  
  51. }
  52.  
  53. bool getBit(char value, char index) {
  54.     return value & (1 << index);
  55. }
  56. bool is_printable(char ch) {
  57.     unsigned char c = (unsigned char)ch;
  58.     if (c >= 0x20 && c <= 0x7f) {
  59.         return true;
  60.     }
  61.     if (c == 0) {
  62.         return true;
  63.     }
  64.     return false;
  65. }
  66. char getOutput(char input, char secret) {
  67.     char output = 0;
  68.     output |= (!(getBit(secret, 0) ^ (getBit(input, 6) ^ getBit(input, 1)))) << 0;
  69.     output |= (!(getBit(secret, 1) ^ (getBit(input, 7) ^ getBit(input, 0)))) << 1;
  70.     output |= (!(getBit(secret, 2) ^ (getBit(input, 4) ^ getBit(input, 3)))) << 2;
  71.     output |= (!(getBit(secret, 3) ^ (getBit(input, 5) ^ getBit(input, 2)))) << 3;
  72.     output |= (!(getBit(secret, 4) ^ getBit(input, 5))) << 4;
  73.     output |= (!(getBit(secret, 5) ^ getBit(input, 4))) << 5;
  74.     output |= (!(getBit(secret, 6) ^ getBit(input, 7))) << 6;
  75.     output |= (!(getBit(secret, 7) ^ getBit(input, 6))) << 7;
  76.  
  77.     return output;
  78. }
  79. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  80. // Debug program: F5 or Debug > Start Debugging menu
  81.  
  82. // Tips for Getting Started:
  83. //   1. Use the Solution Explorer window to add/manage files
  84. //   2. Use the Team Explorer window to connect to source control
  85. //   3. Use the Output window to see build output and other messages
  86. //   4. Use the Error List window to view errors
  87. //   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
  88. //   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