// affine caesar.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int a,b; //define a and b
char p,c; //define plaintext p, and cipher text c
int input1() //input for encryption
{
cout<<"Input a:";
cin>>a;
cout<<"Input b:";
cin>>b;
cout<<"Input plaintext p:";
cin>>p;
return 0;
}
int input2() //input for decryption
{
cout<<"Input a:";
cin>>a;
cout<<"Input b:";
cin>>b;
cout<<"Input ciphertext c:";
cin>>c;
return 0;
}
int encrypt(int a, int b, char p) //Encryption function
{
int pp=p-97; //change letter into number
int cc; //define cipher number
cc=(a*pp+b)%26; //function: C=[a,b],p=(a*p+b) mod 26
cc=cc+97;
char c= (char)cc; //change cipher number to cipher letter
cout<<"Cipher text is: "<<c<<endl; //output cipher text
return c;
}
int decrypt(int a, int b, char c) //Decryption function
{
int cc=c-97; //chage letter into number
int aa=0, r=0;
do //find inverse a=aa
{
aa=aa+1;
r=(a*aa) %26;
}while(r!=1);
int pp; //define plaintext number
pp=(aa*(cc-b+26))% 26; //function: p=a^-1*(C-b) mod 26
pp=pp+97;
char p= (char)pp; //change plaintext number into plaintext letter
cout<<"Plaintext p is: "<<p; //output plaintext
return p;
}
int choose() //choose funtions
{
cout<<"a. Encryption. \nb. Decryption.\n";
char input=1;
cout<<"Input: ";
cin>>input; cout<<endl;
switch (input)
{
case 'a':
{
input1();
encrypt(a,b,p);
cout<<endl<<endl;
break;
}
case 'b':
{
input2();
decrypt(a,b,c);
cout<<endl<<endl;
break;
}
default:cout<<"Error Input! Try again!"<<endl;break;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[]) //main function
{
int rr=1;
do{
choose();
}while(rr==1);
return 0;
}
affine-caesar.rar_MOD_affine cipher_加密解密affine
版权申诉
157 浏览量
2022-09-19
18:37:19
上传
评论
收藏 861B RAR 举报
四散
- 粉丝: 49
- 资源: 1万+
最新资源
- 福袋点点.apk
- Lengyel E. - Foundations of Game Engine Development(卷一卷二合集).zip
- ### 词向量的介绍、使用技巧和优缺点的文章
- 基于STM32F103CBT6单片机GC65+MP2625+CC1101 GPSTrack模块板硬件(原理图+PCB)工程文件
- ### 通道处理过程模拟概念、优缺点和使用技巧
- ### MyBatis动态SQL介绍说明、使用技巧和优缺点
- 上传下载仿163网盘无刷新文件上传 for Jsp-fileupload-jsp.rar
- VMware Workstation业界非常稳定且安全的桌面虚拟机软件-计算机上运行多个操作系统,支持Windows、DOS等
- 基于STM8L101F3P6单片机+LY2508A33P+CC1100遥控器硬件(原理图+PCB)工程文件.zip
- 上传下载WAP图铃下载系统-unimg.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈