凯撒加密和解密c++
Caesar Encryption and decryption C++
我想知道如何限制ASCII加密范围从32到126。
对于我们的任务,我们应该将字符串转换为char并加密/解密每个单独的char。
我现在使用这个来加密
int value = (value-32+shift)%95+32 //value is the current ascii value of a
//character
//the first shift is given by the user with regards
//to how many shifts he wants to do to the right
和这个用于解密
int value = (value-32-shift)%95+32
我的加密工作完美(当我引用解密函数时),但我的解密工作不像它应该的那样。
额外注意:我们只需要在编码时做一个右移,为整个程序提供一个字符串来加密和解密("这是c++")
Give shift: 3
Wklv#lv#F..
DECODE
Wklv#lv#F.. //must be 'THIS is C++'
ENCODE //shift=15
This is C++ //must be 'cwx#/x#/R::'
DECODE
EYZdpZdp4{{ //must be 'THIS is C++'
ENCODE //shift=66
cwx#/x#/R:: //must be "8LMWbMWb'mm"
DECODE
This is C++
ENCODE //shift=94
cwx#/x#/R:: //must be 'SGHR~hr~B**'
DECODE
This is C++
注:正在添加更多代码描述
您的问题在具有负值的模算子中解释。我不确定这是不是一模一样的复制品。问题是,解码像'!
int value = (value-32-shift)%95+32
= ('!'-32-3)%95+32
= (33-32-3)%95+32
= (-2)%95 + 32
= -2 + 32
= 30
哦。您需要使用:
int value = (value-32+(95-shift))%95+32
问题是(value-32-shift)
可以变为负值。模运算并不是"绕着",而是实际上"绕着"零(如果你想知道为什么,请参阅这个问题并回答)。要确保您的值为正,请在进行模运算之前加上95:
int value = (value-32-shift+95)%95+32
相关文章:
- AES加密到解密未正确输出
- 为什么这不是解密
- 通过 Openssl 命令行加密,通过 c++ 解密
- OpenSSL 解密功能无法正常工作
- 仿射密码解密,输出大小写不同
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- 私钥解密代码使用公共接口说明符到 BSAFE 库 v6.0?
- POCO C++加密/解密
- 从加密项目向量中解密任意选择的元素会导致无效的 PKCS #7 块错误
- 尝试从 Visual Studios C++ 中的加密字符串输出解密字符串时出现逻辑错误
- OpenSSL RSA 解密随机失败 C/C++
- 压缩天然气.AES.错误解密(可能加密)的文件
- C++ - 从文件解密字符串
- C++ - 如何从上到下制作 4x4 矩阵和 cout 列(需要解密字符串)
- 运行密钥密码解密知道密钥?
- 如何在c ++中使用XOR解密文件?
- 如何制作一个程序,它使用特定单词加密和解密消息?
- 基本加密和解密方法不起作用 C++ 11.
- 凯撒密码在C++中无法正确解密的问题
- 异或字符串加密/解密输出错误