单表代换密码(凯撒密码)

  • 单表代换密码概述
    • 对所有的明文字母都用一个固定的代换进行加密 ,因而称为
    • 单表代换密码。加密过程中是从明文字母表到密文字母表的一一映射。例:
    • 恺撒(Caesar)密码。
    • 缺点:不能抗击字母频度分析,容易被破译
    • 单表密码的弱点:明文和密文字母之间的一一代替关系。这使得明文中的一些固有特性和规律(比如语言的各种统计特性)必然反映到密文中去。
  • 凯撒密码加解密过程(C实现)

    • #include <iostream>
      #include "string.h"
      using namespace std;
      
      char* CaesarEncrypt(char* plaintext);
      char* CaesarDecrypt(char* ciphertext);
      char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
      char b[26]={'X','N','Y','A','H','P','O','G','Z','Q','W','B','T','S','F','L','R','C','V','M','U','E','K','J','D','I'};
      
      
      int main()
      {
      	char plaintext[105];
      	printf("please input a string:\n");
      	gets(plaintext);
      	char *ciphertext1=CaesarEncrypt(plaintext);
      	CaesarDecrypt(ciphertext1);
      	getchar();
      	getchar();
      	return 0;
      }
      
      
      char* CaesarEncrypt(char plaintext[]){
      	char ciphertext[105];
      	int i=0;
      
      	int sizeofplaintext=strlen(plaintext);
      	for(i=0;i<sizeofplaintext;i++){
      		int flag=0;
      		for(int j=0;j<26;j++){
      			if(plaintext[i]==a[j]){
      				ciphertext[i]=b[j];
      				flag=1;
      			}
      		}
      		if(flag!=1){
      			ciphertext[i]=' ';
      		}
      	}
      	ciphertext[i]='\0';
      	printf("the ciphertext is:" );
      	printf("%s",ciphertext);
      	char *str = new char[strlen(ciphertext) + 1];		 //分配存储空间  
      	strcpy_s(str, strlen(ciphertext) + 1,ciphertext);	 //将s中字符串复制到str,最后一个空间为'\0'结束符  
      	return str;
      }
      
      char* CaesarDecrypt(char* ciphertext){
      	int sizeofciphertext=strlen(ciphertext);
      	int i;
      	char plaintext[105];
      	for(i=0;i<sizeofciphertext;i++){
      		int flag=0;
      		for(int j=0;j<26;j++){
      			if(ciphertext[i]==b[j]){
      				plaintext[i]=a[j];
      				flag=1;
      			}
      		}
      		if(flag!=1){
      			plaintext[i]=' ';
      		}
      	}
      	plaintext[i]='\0';
      	cout<<"\nthe plaintext is:"<<plaintext<<endl;
      	return plaintext;
      }
  • 结果演示
    • 结果演示

  • 凯撒密码密码表
    • 单表置换表



  • 7
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值