凯撒密码加密、解密过程(python、C++实现)

您所在的位置:网站首页 凯撒dom 凯撒密码加密、解密过程(python、C++实现)

凯撒密码加密、解密过程(python、C++实现)

2023-10-13 10:20| 来源: 网络整理| 查看: 265

凯撒密码 单表替代密码 ——凯撒(Caesar)密码,又叫循环移位密码。它的加密方法就是将明文中的每个字母用字母表中该字母后的第R个字母来替换,达到加密的目的。 加密过程可以表示为下面的函数: E(x) = (x + key) mod n 其中,E表示加密函数,x表示为明文;n为字母表中的字母个数;key为密钥, 为密文字母在字母表中对应的位置数。 解密过程可以表示为下面函数: D(y) = (y - key) mod n 其中,D表示解密函数,y为密文,n为字母表中的字母个数;key为密钥, 为密文字母在字母表中对应的位置数。 加密示意图 假设key为3 在这里插入图片描述 注:解密是加密的反过程

加密函数

def Encryption(plaintext, key): print("加密后为:") for i in range(len(plaintext)): #ord():把字符转ASCII码,chr():把ASCII码转字符 if ord('a') bool isPhontic = false; //先假设为非字母组成 for (int i = 0; i //遍历过程中只要出现了一个非字母字符,则返回非字母字符(false) return false; //返回false,表示出现了非字母字符 } } return true; //遍历完后没有出现非字母字符,返回true } bool isNum(string str) { for (int i = 0; i cout int k = 0 - key; //k=负key代表解密,其他与加密类似 string plaintext="";//plaintext表示明文 for (int i = 0;i //如果字符串中的某个字符是小写字母 c += k % 26;//移动key%26位 if (c 'z') c -= 26;//向右超界 }else if (c >= 'A' && c string::iterator pos = str.begin(); while(1){ pos = find(pos, str.end(), ' '); if(pos == str.end()) break; str.erase(pos); } return str; } int main() { int operateType = 0; while (operateType != 3){ cout //判断plaintext明文是否全为字母字符 cout cout //如果秘钥key非数字 cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3