⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 caesar_encode.cpp

📁 实现移位密码(扩展凯撒密码)
💻 CPP
字号:
#include<stdio.h>
#include<string.h>
#define Max 81
char *Caesar_encode(char *Plaintext,int Key)   //凯撒密码加密算法
{
	int lenth,i;
    lenth=strlen(Plaintext);
    for(i=0;i<lenth;i++)   //根据字母的大小写来判断使用的算法
	{
		if(Plaintext[i]>='A'&&Plaintext[i]<='Z')   
			Plaintext[i]=('a'+(Plaintext[i]-'A'+Key)%26);
        else if(Plaintext[i]>='a'&&Plaintext[i]<='z')   
	        Plaintext[i]=('A'+(Plaintext[i]-'a'+Key)%26);
	    else
            Plaintext[i]=Plaintext[i];
	}
	return Plaintext;
}

void main()
{
    int K;   //K表示密钥
    char P[Max],*C;  //明文存放在数组P中,密文存放在指针变量C所指向的存储单元

    printf("--------------------------Welcome to the Caesar Encode!-----------------------\n");
    printf("Please input the Plaintext:  ");
    gets(P);
    printf("Input the Key: ");
    scanf("%d",&K);

	rewind(stdin);   //清空输入的缓冲区
    while(K<0||K>25)   //判断,使密钥为0~25的数字,如果不是则重新输入
    {
    	printf("\n Wrong!  Please Input the Key again:  ");
        scanf("%d",&K);
	    rewind(stdin);   //清空输入的缓冲区
    } 

	printf("-------------------------------------------------------------------\n");
	printf("The Ciphertext is:  ");
	C=Caesar_encode(P,K);
	puts(C);
	printf("--------------------------------------------------------------------\n");
}
    	
    


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -