c - 如何解密加密的 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 是否有合理的解释?
解决方案
调试时,可以看到加密二进制的输出。你得到-26
的值是 和我们需要的字母的值是97
。这两个数之差就是我们需要的数,即123
。这就是用来加密字母的密码abc
推荐阅读
- android - 如何在 Coordinatorlayout 中放置视图
- java - 将错误输出连接到由 System.setErr(...) 设置的错误文件
- ios - iOS (14):UMP SDK 已实现 -> AppTrackingTransparency 框架还需要吗?
- ios - Obj-C - 从 MapView 中删除特定的自定义注释?
- r - 如何删除小于某个值的数据
- terraform - 将状态中的所有内容移到层次结构中
- c - 更好的使用 Switch Case 的方法?
- sql - SQL 返回名称相同但策略不同的地方
- scala - 使用scala在spark数据帧中的一行中的值计数
- amazon-web-services - 登录后使用 Route 53 将用户重定向到应用程序?