密码溢出

标签 c

我正在像下面这样加密,但不知道如何防止大写字母移出范围后进入其他符号,类似地,小写字母超出小写字母的范围。我怎样才能让他们围成一圈并停止溢出?谢谢

    int size = strlen(plain_text);
int arrayelement = 0;
for(arrayelement = 0; arrayelement < size; arrayelement++)
{
    if (islower(plain_text[arrayelement]))
    {
        ciphered_text[arrayelement] = (int)(plain_text[arrayelement] + shiftkey);
    }
    else if (isupper(plain_text[arrayelement]))
    {
        ciphered_text[arrayelement] = (int)(plain_text[arrayelement] + shiftkey);
    }
}

ciphered_text[size] = '\0';
printf("%s", ciphered_text);

最佳答案

我猜你使用了像 char 这样的类型,所以一个简单的解决方案就是不溢出

int tmp_ciphered = (my_char + shift) % 0xff;
char ciphered = (char)(tmp_ciphered);

然后你转身不溢出,这是一个环

关于密码溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13407879/

相关文章:

c - 如果使用字符串不起作用

c++ - 不要在 GCC 中使用 -O3 标志优化特定循环

c - 如何测量我的全局变量的总大小?

c - 在 C 中,给定一个可变的参数列表,如何使用它们构建函数调用?

c - 使用 strcpy() 的结构的 char 数组成员出现段错误

c - 为什么我无法获取输入值?

c - For循环在一个循环后退出-MPI

c - 二维数组(指针到指针)

c - 将表示有符号整数的字节数组转换为整数的公式

c - 如何在 Cython 中动态声明二维 c 数组