Итак, в качестве задания у нас есть код, который может расшифровывать и шифровать значения ASCII. При задании значения от 0
до set_cipher
вы можете расшифровать значение двоичного файла ASCII, например:
Если мы возьмем двоичное значение ASCII H
с char message
, установленным на
char message[] = {'H', 'a', 'l', 'l', 'o'};
и byte secret_str[]
установлен на byte secret_str[] = {0b01001000};
. мы получаем сообщение H
Соответствующее присваивание дает нам 3 зашифрованных значения, 0b11100110 0b11100111 0b11101000
соответственно эти значения должны давать a b c
в качестве расшифрованного значения. для этого вам нужно будет отладить, чтобы найти, какой шифр использовался для расшифровки, сказал a b c
.
Я пытался отладить код, и все же я все еще не нашел, какое значение шифра было установлено для шифрования этих писем. Например, в приведенном ниже коде я хочу расшифровать
byte secret_str[] = 0b11100110
Когда я отлаживаю этот фрагмент кода, получаю значение 230
, значение 230 в таблице ASCII дает символµ
. Проблема в том, что вы уже знаете, что персонаж, которого вы должны получить, - a
.
Значение ASCII буквы a
равно 97
, поэтому между этими двумя значениями есть разница 133
.
После того, как я сам постоянно увеличивал шифр, я виделчто после того, как вы установите set_cipher(0)
в set_cipher(123)
, вы получите a
после запуска кода.
Есть ли кто-то, кто может объяснить мне этот процесс?
код выглядит следующим образом:
#include <stdio.h>
#include "include/utils.h"
int cipher = CIPHER_DEFAULT_VALUE;
int get_cipher() {
return cipher;
}
void set_cipher(int value) {
cipher = value;
}
char byte_to_char(byte input) {
char output = (char)(input + get_cipher());
return output;
}
char char_to_byte(char input) {
byte output = (byte)(input - get_cipher());
return output;
}
void cipher_decrypt(char * output, byte *secret_str, int secret_length) {
int i;
for (i = 0; i < secret_length; i++) {
byte byteValue = secret_str[i];
char character = byte_to_char(byteValue);
output[i] = character;
}
}
void cipher_encrypt(byte * output, char *str, int str_length) {
int i;
for (i = 0; i < str_length; i++) {
char character = str[i];
byte byteValue = char_to_byte(character);
output[i] = byteValue;
}
}
void print_byte_array(byte secret_str[], int secret_length) {
int i;
for (i = 0; i < secret_length; i++) {
printf("0b" BYTE_TO_BINARY_PATTERN " ", BYTE_TO_BINARY(secret_str[i]));
}
printf("\n");
}
int main() {
set_cipher(0);
/*
* This example shows how a message can be encrypted!
* Required: A message in a char array (message[])
* Length of message (calculated automatically)
* Byte-array, stores the encrypted message so make sure it's big enough!
*
* 'string_to_bytes' encrypts your message, output will be stored in first argument (secret_output)
*
* The 'print_byte_array'-method prints the encrypted message in binary format!
* This you can use for the decryption.
*/
char message[] = {'H', 'a', 'l', 'l', 'o'};
int message_length = sizeof(message) / sizeof(message[0]);
byte secret_output[100] = {0};
cipher_encrypt(&secret_output, message, message_length);
print_byte_array(secret_output, message_length);
/*
* This example shows how a message can be decrypted!
* Required: A encrypted message
* Length of message (calculated automatically)
* Char-array with enough space for the message.
*
* 'bytes_to_string' decrypts your message, the message will be stores in 'output_str'.
* To decrypt, your offset number must be the same as used when encrypting the message.
*
* To define an array of bytes, you have to append 0b in front of the 1's and 0's, example:
* 00111100 will become 0b00111100
* 01010101 will become 0b01010101
*
*/
byte secret_str[] = { 0b11100110};
char output_str[200] = {};
int secret_length = sizeof(secret_str) / sizeof(secret_str[0]);
cipher_decrypt(&output_str, secret_str, secret_length);
printf("Geheim: %s \n", output_str);
return 0;
}
Есть ли логическое объяснение того, как можно получить число 123?