So as an assignment we've got a code that can decrypt and encrypt ASCII values.
When giving the value 0
to set_cipher
you can decrypt the value of an ASCII binary, for example:
If we take the ASCII binary value of H
with the char message
set to
char message[] = {'H', 'a', 'l', 'l', 'o'};
and thebyte secret_str[]
set to byte secret_str[] = {0b01001000};
. we get the message H
The assignment in question gives us 3 encrypted values, 0b11100110 0b11100111 0b11101000
these values respectively should give a b c
as the decrypted value. to do this you will have to debug around to find which cipher was used to decrypt said a b c
.
I've been trying to debug the code around and yet I still did not find which cipher value was set to encrypt those letters. For example in the code below I want to decrypt
byte secret_str[] = 0b11100110
What I get once I debug this piece of code is the value 230
, 230 in the ASCII table gives the character µ
. The problem now is, you already know that the character which you should get is a
.
The ASCII value of the letter a
is 97
, so there is a difference of 133
between those 2 values.
After steadily increasing the cipher myself I've seen that once you set set_cipher(0)
to set_cipher(123)
you get a
once you run the code.
Is there someone who can explain this process to me?
the code is as follows:
#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;
}
Is there a logical explanation on how the number 123 can be attained ?
When debugging, you can see the output of the encrypted binary. The value you get is -26
and the value of the letter we need is 97
. The difference between these two number is the number we need, which is 123
. So that is the cipher which was used to encrypt the letters abc