gpt4 book ai didi

c++ - 在 C++ 中加密/解密 ROT

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:52 25 4
gpt4 key购买 nike

HZROT.cpp:

#include "HZROT.h"

std::string ROTEncode(std::string instring, int rot)
{
std::string result;
for (char a : instring)
{
if (a >= 'A' && a <= 'Z')
result += ((int)a + rot) % (int)'Z';
else if (a >= 'a' && a <= 'z')
result += ((int)a + rot) % (int)'z';
else
result += a;
}
return result;
}
std::string ROTDecode(std::string instring, int rot)
{
std::string result;
for (char a : instring)
{
if (a >= 'A' && a <= 'Z')
result += ((int)a - rot + (int)'Z') % (int)'Z';
else if (a >= 'a' && a <= 'z')
result += ((int)a - rot + (int)'z') % (int)'z';
else
result += a;
}
return result;
}

HZROT.h:

#include <string>
std::string ROTEncode(std::string instring, int rot);
std::string ROTDecode(std::string instring, int rot);

我使用此代码来加密/解密 ROT,但它无法正常工作:命令行:

C:\Users\adm1n\Desktop\C\HZToolkit>hztoolkit --erot 13 abcdefghijklmnopqrstuvwyz

输出:nopqrstuvwxy所以它不适用于“l”之后的字母。你能帮帮我吗?

最佳答案

工作代码是:

#include "HZROT.h"

std::string ROTEncode(std::string instring, int rot)
{
std::string result;
for (char a : instring)
{
if (IsBigLetter(a))
result += ((int)a - (int)'A' + rot) % 26 + 'A';
else if (IsSmallLetter(a))
result += ((int)a - (int)'a' + rot) % 26 + 'a';
else
result += a;
}
return result;
}
std::string ROTDecode(std::string instring, int rot)
{
std::string result;
for (char a : instring)
{
if (IsBigLetter(a))
result += ((int)a - (int)'A' - rot + 26) % 26 + 'A';
else if (IsSmallLetter(a))
result += ((int)a - (int)'a' - rot + 26) % 26 + 'a';
else
result += a;
}
return result;
}

和HZROT.h:

#include <string>

#define IsBigLetter(a) a >= 'A' && a <= 'Z'
#define IsSmallLetter(a) a >= 'a' && a <= 'z'

std::string ROTEncode(std::string instring, int rot);
std::string ROTDecode(std::string instring, int rot);

关于c++ - 在 C++ 中加密/解密 ROT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43821346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com