【C++】移位密码算法/凯撒密码 加解密 类封装

移位密码算法、凯撒密码 类封装

前言:最近的作业,随便搜了一下网上没有一下质量较高成品较好的加解密算法封装,在此分享自己的代码,仅供参考
具体 加解密结果转换大小写 可以自行在代码中修改

题目原题

【实验目的】

1) 学习移位密码的原理
2) 学习移密码的实现

【实验原理】

  1. 算法原理
    a) 移位密码就是对26个字母进行移位操作,可以移动任意位数,这样就实现了对明文的加密,移位操作简单易行,因此,加密解密比较简单。
    b) 移位密码的基本思想:移位密码算法 c=m+k(mod 26),k可以使0<k<26的任意整数。加密算法:x=x+k(mod26),解密算法x=x-k(mod 26)。当K=3,时,为凯撒密码。

  2. 算法参数
    移位密码算法主要有c、m、k 三个参数。c 为密文,m 是明文,k 为密钥。

  3. 算法流程
    算法流程如下。如图所示

输入

第一行输入表明是加密还是解密,0是加密,1是解密;
第二行是加密或解密密钥,是0<k<26之间的一个整数;
第三行是明文或密文。

输出

输出是明文或密文。

输入样例

0
6
wearesztuers
1
6
CKGXKYFZAKXY

输出样例

CKGXKYFZAKXY
wearesztuers

接口使用:

构造函数CaesarPassword(原始文本:可以用来加密或解密)
加密方法encode(key:加密密钥),返回加密密文
解密方法decode(key:解密密钥),返回解密密文

主函数:

见输入,这里演示了各加密,解密一次的结果

凯撒密码/移位密码(类封装)

#include<iostream>
using namespace std;
//移位密码:或叫凯撒密码
//功能推测:题目虽然没有讲但是我们可以从输入输出发现明文和密文大小写相反,可以在

class CaesarPassword {
    string plaintext;
public:
    CaesarPassword(string text) {
        plaintext = text;
    }
    string encode(int key) {      //加密:c=m+k(mod 26)
        int len = plaintext.length();       //注意要求:加密小写输出大写,所以采用先减ascii到26以内再加回去的方法
        for (int i = 0; i < len; i++) {
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {   //密文是小写,转化密文为大写
                plaintext[i] = (plaintext[i] - 'a' + key % 26) % 26 + 'A';       //由于减法后可能是负数,模完后可能还是负的,需要手动加26在模正
            }
            else plaintext[i] = (plaintext[i] - 'A' + key % 26) % 26 + 'a';
        }
        return plaintext;
    }
    string decode(int key) {      //解密:m=c-k(mod 26)
        int len = plaintext.length();
        for (int i = 0; i < len; i++) {
            if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {   //密文是小写,转化密文为大写
                plaintext[i] = (plaintext[i] - 'a' - key % 26 + 26) % 26 + 'A';       //由于减法后可能是负数,模完后可能还是负的,需要手动加26在模正
            }
            else plaintext[i] = (plaintext[i] - 'A' - key % 26 + 26) % 26 + 'a';

        }
        return plaintext;
    }
};

int main()
{
    //cout << -10%3;        //根据模的结果可知,负数模完后不会变为正数,所以在解密(减法)需要我们手动+一个周期,强制模正
    string text;
    int type, key;
    while (cin >> type >> key >> text) {
        CaesarPassword m(text);
        if (type == 0)cout << m.encode(key) << endl;
        else cout << m.decode(key) << endl;

    }

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算法 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {
凯撒密码是一种简单的替换密码,它通过将明文中的每个字母按照一定的偏移量进行替换来实现加密和解密。在C++中,可以使用字符的ASCII码值来进行偏移和替换操作。 下面是一个简单的凯撒密码加解密C++代码示例: ```cpp #include <iostream> using namespace std; // 凯撒密码加密函数 string caesarEncrypt(string plaintext, int shift) { string ciphertext = ""; for (int i = 0; i < plaintext.length(); i++) { // 对字母进行偏移 if (isalpha(plaintext[i])) { char shiftedChar = (toupper(plaintext[i]) - 'A' + shift) % 26 + 'A'; ciphertext += shiftedChar; } else { ciphertext += plaintext[i]; } } return ciphertext; } // 凯撒密码解密函数 string caesarDecrypt(string ciphertext, int shift) { string plaintext = ""; for (int i = 0; i < ciphertext.length(); i++) { // 对字母进行逆向偏移 if (isalpha(ciphertext[i])) { char shiftedChar = (toupper(ciphertext[i]) - 'A' - shift + 26) % 26 + 'A'; plaintext += shiftedChar; } else { plaintext += ciphertext[i]; } } return plaintext; } int main() { string plaintext = "HELLO WORLD"; int shift = 3; // 加密 string ciphertext = caesarEncrypt(plaintext, shift); cout << "加密后的密文:" << ciphertext << endl; // 解密 string decryptedText = caesarDecrypt(ciphertext, shift); cout << "解密后的明文:" << decryptedText << endl; return 0; } ``` 这段代码中,`caesarEncrypt`函数用于加密明文,`caesarDecrypt`函数用于解密密文。其中,`shift`参数表示偏移量,可以是任意整数。在加密时,将明文中的每个字母按照偏移量进行替换;在解密时,将密文中的每个字母按照逆向偏移量进行替换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值