如何解密加密的 ASCII 二进制值?
How to decrypt an encrypted ASCII binary value?
作为一项作业,我们得到了一个可以解密和加密 ASCII 值的代码。
当将值 0
赋给 set_cipher
时,您可以解密 ASCII 二进制值,例如:
如果我们将 H
的 ASCII 二进制值与 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
,ASCII table中的230给出字符µ
。现在的问题是,你已经知道你应该得到的字符是a
。
字母 a
的 ASCII 值为 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 的合理解释?
调试的时候,可以看到加密二进制的输出。你得到的值是-26
,我们需要的字母的值是97
。这两个数之差就是我们需要的数,即123
。这就是用来加密字母 abc
的密码
作为一项作业,我们得到了一个可以解密和加密 ASCII 值的代码。
当将值 0
赋给 set_cipher
时,您可以解密 ASCII 二进制值,例如:
如果我们将 H
的 ASCII 二进制值与 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
,ASCII table中的230给出字符µ
。现在的问题是,你已经知道你应该得到的字符是a
。
字母 a
的 ASCII 值为 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 的合理解释?
调试的时候,可以看到加密二进制的输出。你得到的值是-26
,我们需要的字母的值是97
。这两个数之差就是我们需要的数,即123
。这就是用来加密字母 abc