You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

字符串加密方案咨询:基于字符映射表的加密实现及替代方案需求

字符串加密解决方案

核心逻辑说明

每个字符的加密规则是:找到它在signs二维数组中的行索引i(0-6)和列索引j(0-5),将i+1和j+1转为字符串后拼接,作为该字符的加密结果,最终把所有字符的加密结果连起来就是目标加密串。

方案一:预构建哈希映射(高效推荐)

嵌套循环查找的问题是每次查找都要遍历整个数组,时间复杂度O(n76)(n为输入字符串长度)。用ASCII码数组做映射可以将查找复杂度降到O(1),大幅提升效率。

代码实现

#include <stdio.h>
#include <string.h>
#include <ctype.h>

// 原字符映射数组
char signs[7][6] = {
    {'A', 'G', 'L', 'S', 'Z', '6'},
    {'B', 'Ğ', 'M', 'Ş', '0', '7'},
    {'C', 'H', 'N', 'T', '1', '8'},
    {'Ç', 'I', 'O', 'U', '2', '9'},
    {'D', 'İ', 'Ö', 'Ü', '3', '?'},
    {'E', 'J', 'P', 'V', '4', '!'},
    {'F', 'K', 'R', 'Y', '5', ' '}
};

// 预构建加密映射表:用ASCII码作为索引,存储对应的加密字符串(比如"11"、"24"等)
char encrypt_map[256][3]; // 每个加密结果最多2位,加结束符占3字节

// 初始化映射表
void init_encrypt_map() {
    memset(encrypt_map, 0, sizeof(encrypt_map));
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 6; j++) {
            char c = signs[i][j];
            // 生成加密串:i+1和j+1转为字符
            encrypt_map[(unsigned char)c][0] = '1' + i;
            encrypt_map[(unsigned char)c][1] = '1' + j;
            // 处理小写情况(匹配输入中的小写字符)
            if (isalpha(c)) {
                char lower_c = tolower(c);
                encrypt_map[(unsigned char)lower_c][0] = '1' + i;
                encrypt_map[(unsigned char)lower_c][1] = '1' + j;
            }
        }
    }
}

// 加密函数
void encrypt_string(const char* input, char* output) {
    output[0] = '\0';
    while (*input != '\0') {
        char* code = encrypt_map[(unsigned char)*input];
        if (code[0] != '\0') {
            strcat(output, code);
        }
        // 可选:未映射字符可添加默认处理(如跳过或标记)
        input++;
    }
}

int main() {
    init_encrypt_map();
    char input[] = "şğüö 1234?!";
    char output[100] = {0}; // 根据输入长度调整大小
    encrypt_string(input, output);
    printf("加密结果:%s\n", output);
    return 0;
}

代码说明

  • 仅需一次遍历初始化映射表,后续加密直接查表,时间复杂度降至O(n)。
  • 自动处理大小写匹配,解决输入中小写字符与数组中大写字符不匹配的问题。

方案二:优化的嵌套循环(适合小场景)

如果不想占用额外空间,可优化嵌套循环逻辑,找到字符后立即跳出循环,减少不必要的遍历:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char signs[7][6] = {
    {'A', 'G', 'L', 'S', 'Z', '6'},
    {'B', 'Ğ', 'M', 'Ş', '0', '7'},
    {'C', 'H', 'N', 'T', '1', '8'},
    {'Ç', 'I', 'O', 'U', '2', '9'},
    {'D', 'İ', 'Ö', 'Ü', '3', '?'},
    {'E', 'J', 'P', 'V', '4', '!'},
    {'F', 'K', 'R', 'Y', '5', ' '}
};

void encrypt_string(const char* input, char* output) {
    output[0] = '\0';
    while (*input != '\0') {
        char c = toupper((unsigned char)*input); // 统一转大写匹配数组
        int found = 0;
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 6; j++) {
                if (signs[i][j] == c) {
                    char code[3];
                    sprintf(code, "%d%d", i+1, j+1);
                    strcat(output, code);
                    found = 1;
                    break; // 找到后跳出列循环
                }
            }
            if (found) break; // 找到后跳出行循环
        }
        input++;
    }
}

int main() {
    char input[] = "şğüö 1234?!";
    char output[100] = {0};
    encrypt_string(input, output);
    printf("加密结果:%s\n", output);
    return 0;
}

代码说明

  • 统一将输入字符转大写,避免大小写不匹配问题。
  • 找到目标字符后立即终止两层循环,比无优化的嵌套循环减少大量无效遍历。

内容的提问来源于stack exchange,提问作者user19108367

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:45:47