首页 > 解决方案 > 如何解密加密的 ASCII 二进制值?

问题描述

因此,作为一项任务,我们得到了一个可以解密和加密 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,ASCII 表中的 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 是否有合理的解释?

标签: cclion

解决方案


调试时,可以看到加密二进制的输出。你得到-26的值是 和我们需要的字母的值是97。这两个数之差就是我们需要的数,即123。这就是用来加密字母的密码abc


推荐阅读