加密字符串小写字母循环后错两位(加密字符串,小写字母循环后错两位)

2023-03-01 3:29:39 摩斯密码知识 思思

字符串加密。将字符串中的英文字母转换成其后面的第二个字母,其余字母不变,即A—C,B—D,……,

#include "stdio.h"

int main()

{

char a[100];

int b;

printf("请输入字符串:");

scanf("%s",a);

for(b=0;a[b]!='\0';b++)

{

if(a[b]='a'a[b]='x'||a[b]='A'a[b]='X')

a[b]+=2;

else if(a[b]=='y'||a[b]=='z'||a[b]=='Y'||a[b]=='Z')

a[b]-=24;

}

printf("加密后的字符串:%s\n",a);

}

加密字符串小写字母循环后错两位(加密字符串,小写字母循环后错两位) 第1张

文件的加密. 使用“二战”时简单的加密算法,即将英文的大写字母或小写字母循

#include stdio.h

#include stdlib.h

#include string.h

#define MAX_FILE_NAME 128

#define MAX_STRING_SIZE 256

void encrypt(void);

void decode(void);

void upper(void);

void lower(void);

void fix(void);

int main(int argc, char **argv)

{

int command=1;

    while(command!=0)

    {

        printf("%-16s%-16s%-16s%-16s%-16s%-16s\n","1.encrypt","2.decode","3.to upper","4.to lower","5.auto fix","0.exit");

        printf("enter commond\'s number: ");

        scanf("%d",command);

        switch(command)

        {

            case 1: encrypt();  break;

            case 2: decode();  break;

            case 3: upper();  break;

            case 4: lower();  break;

            case 5: fix();  break;

            case 0: break;

        }

    }

return 0;

}

void encrypt(void)

{

    char source[MAX_FILE_NAME];

    char destination[MAX_FILE_NAME];

    printf("enter the file\'s path you want to encrypt: ");

    scanf("%s",source);

    printf("enter the path you want to save the result: ");

    scanf("%s",destination);

    FILE *input=fopen(source,"r");

    

    if(input==NULL)

    {

        printf("open file fail\n");

        system("pause");

        exit(1);

    }

    FILE *output=fopen(destination,"w");

    if(output==NULL)

    {

        printf("create file fail\n");

        system("pause");

        exit(1);

    }

    int inch=fgetc(input);

    while(!feof(input))

    {

        

        if(inch='a')

            fputc((char)('a'+(inch+4)%'a'),output);

        else

            fputc((char)('A'+(inch+4)%'A'),output);

        inch=fgetc(input);

    }

    fclose(input);

    fclose(output);

    printf("complete\n");

}

void decode(void)

{

    char source[MAX_FILE_NAME];

    char destination[MAX_FILE_NAME];

    printf("enter the file\'s path you want to encrypt: ");

    scanf("%s",source);

    printf("enter the path you want to save the result: ");

    scanf("%s",destination);

    FILE *input=fopen(source,"r");

    

    if(input==NULL)

    {

        printf("open file fail\n");

        system("pause");

        exit(1);

    }

    FILE *output=fopen(destination,"w");

    if(output==NULL)

    {

        printf("create file fail\n");

        system("pause");

        exit(1);

    }

    int inch=fgetc(input);

    while(!feof(input))

    {

        

        if(inch='a')

            fputc((char)('a'+(inch+22)%'a'),output);

        else

            fputc((char)('A'+(inch+22)%'A'),output);

        inch=fgetc(input);

    }

    fclose(input);

    fclose(output);

    printf("complete\n");

}

void upper(void)

{

    char input[MAX_STRING_SIZE];

    printf("input a sentence: ");

    scanf("%s",input);

    char mode;

    fflush(stdin);

    printf("Do you want to convert all character to uppercase? (y/n): ");

    scanf("%c",mode);

    if(mode=='y')

        printf("%s\n",strupr(input));

    else

    {

        char *temp=strlwr(input);

        int length=strlen(temp);

        int j=length-1;

        while(j=0)

        {

            if(temp[j]=='.')

                if(j+1length)

                    if(temp[j+1]='a')

                        temp[j+1]-=32;

            j--;

        }

        printf("%s\n",temp);

    }

}

void lower(void)

{

    char input[MAX_STRING_SIZE];

    printf("input a sentence: ");

    scanf("%s",input);

    char *temp=strlwr(input);

    int length=strlen(temp);

    int j=length-1;

    while(j=0)

    {

        if(temp[j]=='.')

            if(j+1length)

                if(temp[j+1]='a')

                    temp[j+1]-=32;

        j--;

    }

    if(length!=0)

        if(temp[0]='a')

                temp[0]-=32;

    printf("%s\n",temp);

}

void fix(void)

{

    char input[MAX_STRING_SIZE];

    char output[MAX_STRING_SIZE];

    printf("input a sentence: ");

    fflush(stdin);

    gets(input);

    int length=strlen(input);

    if(input[length-1]!='.')

        input[length]='.',length++;

    int current=0,patt=0;

    if(length)

        output[current++]=toupper(input[patt++]);

    while(pattlength)

    {

        if(input[patt]==','||input[patt]=='.')

        {

            int mode=0;

            if(input[patt]=='.')

                mode=1;

            output[current++]=input[patt++];

            output[current++]=' ';

            while(pattlength(input[patt]==' '||input[patt]=='\t'))

                patt++;

            if(pattlengthmode)

                output[current++]=toupper(input[patt++]);

        }

        else if(input[patt]==' '||input[patt]=='\t')

        {

            output[current++]=' ';

            while(pattlength(input[patt]==' '||input[patt]=='\t'))

                patt++;

        }

        else

            output[current++]=input[patt++]; 

    }

    printf("%s\n",output);

}

电文加密算法:输入一个小写字母,将字母循环后移5个位置后输出。从键盘输入一电文密码,输入其相应的密码

char jiami(char ch)

{

/* 加密小写字符,其他字符不管 */

if(ch='a' ch='z')

return ('a' + (ch-'a'+5) % 26);

else

return ch;

}