加密和解密ASCII表的一部分
Encrypting and decrypting part of the ASCII table
我编写了一个小的加密程序,使用两个密钥rot7和rot13。一切正常,除了两个6个字母的uvwxyz。
如果我输入ABCDEFGHIJKLMNOPQRSTUVWXYZ,它加密和解密没有问题。但是,如果我以小写字母输入相同的内容,则uvwxyz不起作用。
说到这里,我已经允许ascii表中的所有可写字符作为有效范围,如下所示:
// allow all writable characters from 32 to 255
if ((str[i] >= 32 ) && (str[i] <=255))
{
str[i] -= key;
}
加密过程如下:
cout << endl;
cout << "Encrypting process started " << endl << endl;
cout << "--------------------------- " << endl;
//get the string length
int i = 0;
int length = str.length();
int key = rot13 ;
int k = 5;
int multiple = 0;
int count = 0;
cout << "the text to encrypt is: " << str << endl;
cout << "text length is: " << length << endl;
cout << "using rot13"<<endl;
cout <<"---------------------------" << endl;
cout << "using rot13" << endl;
//traverse the string
for(i = 0; i < length; i++)
{
count ++;
cout << left;
//if it is a multiple of 5 not the first character change the key
if((multiple = (( i % 5 ) == 0)) && (count != 1) && (key == rot13)){
key = rot7;
}
//if it is a multiple of 5 not the first character change the key
else if((multiple = (( i % 5 ) == 0)) && (count != 1) && (key == rot7) ) {
key = rot13;
}
// Capital letters are 65 to 90 (a - z)
if ((str[i] >= 32) && (str[i] <= 255))
{
str[i] += key;
}
}
return str;
如果我允许这个范围,大写字母怎么可能工作而不是小写字母?会不会是别的原因?我已经添加了这些捕获与什么发生一步一步…希望这对你有所帮助
在你的代码中:
if ((str[i] >= 32) && (str[i] <= 255))
{
if (str[i] + key > 255)
str[i] = ((str[i] + key) % 255 )+ 32;
else
str[i] += key;
}
如果key
的值为13且str[i]
的值大于等于'u',则str[i]
的值大于255。
在这种情况下,你应该使用模%
算子,这是旋转,而不仅仅是移位
相关文章:
- C++:如何使函数只返回作为列表一部分的字符串
- std::sort()函数无法对向量的一部分进行排序
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 有没有办法通过使用十进制 ASCII 代码自动类型扣除来获取字符?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- ascii 和 unicode 在处理级别有什么区别吗?
- 在C++中使用 ASCII 代码将输入从小写转换为大写
- 弄清楚如何在C++中将整数读入 ASCII
- 将类型声明为类型模板参数的模板参数的一部分是否合法?
- 如何修复艺术ASCII,我点击一个字母就可以了,但输入一个阶段艺术出来了
- 使用指针将 ASCII 值添加到整数
- 用于 ASCII 的环绕式 C++
- 反转整数的一部分(一半)的函数
- RE2 不匹配非 ASCII 字符
- 打印从 0 到 255 的所有 ASCII 字符
- 选择基于另一个垫子的非零像素的cv::Mat的一部分?
- C 指针和对象作为不同类的一部分
- C++ 电话号码字母拨号程序 - 语法错别字?还是非 ASCII 字符?
- 加密和解密ASCII表的一部分