用C语言对移位加密进行改进

本文介绍使用C语言实现移位加密的三种改进方法:支持大小写混合输入、允许输入包含空格并自动忽略、支持多种非字母字符并保持其不变。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用C语言对移位加密进行改进

#include <stdio.h>
#include <ctype.h>//该头文件中有两个函数:tolower,toupper可以实现单个字符的大小写转换

#define MAX 100
#define YIWEI 3

/**
*改进程序1,使输入的明码字母可以大小写混合
*/
void ModifyNo1()
{
    char M[MAX];
    char C[MAX];
    int K=YIWEI,i;
    printf("请输入明文M(不可输入空白串)\n");
    gets(M);
    for(i=0; M[i]!='\0'; i++)
    {
        M[i]=tolower(M[i]);//利用tolower函数将大写字母转换为小写字母
        C[i]=(M[i]-'a'+K)%26+'a';
    }
    C[i]='\0';
    printf("加密后的密文是:\n%s\n",C);
}
/**
*改进程序2,使输入明码时可以带空格,加密时自动去掉空格
*/
void ModifyNo2()
{
    char M[MAX];
    char C[MAX];
    char space=' ';
    int K=YIWEI,i;
    printf("请输入明文M(不可输入空白串)\n");
    gets(M);
    int j=0;                                    //表示加密字符串的下标
    for(i=0; M[i]!='\0'; i++)
    {
        if(M[i]!=space)                         //比较每一个字符,若是空格则跳过并不对其进行加密
        {
            C[j]=(M[i]-'a'+K)%26+'a';
            j++;
        }
    }
    C[j]='\0';
    printf("加密后的密文是:\n%s\n",C);
}
/**
*改进程序3,使输入明码时可以带空格、英文逗号、句号、分号、问号等5中非字母,加密时符号不变
*/
void ModifyNo3()
{
    char M[MAX];
    char C[MAX];
    int K=YIWEI,i;
    printf("请输入明文M(不可输入空白串)\n");
    gets(M);
    for(i=0; M[i]!='\0'; i++)
    {
        if(M[i]>='a'&&M[i]<='z')
        {
            C[i]=(M[i]-'a'+K)%26+'a';
        }
        else
        {
            C[i]=M[i];
        }
    }
    C[i]='\0';
    printf("加密后的密文是:\n%s\n",C);
}

int main()
{
    printf("改进程序1,输入的明码字母可以大小写混合\n");
    ModifyNo1();

    printf("改进程序2,输入明码时可以带空格\n");
    ModifyNo2();

    printf("改进程序3,使输入明码时可以带空格、英文逗号、句号、分号、问号等5中非字母,加密时符号不变\n");
    ModifyNo3();

    return 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 {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值