當前位置:首頁 » 編程語言 » c語言實現字母加密成字母
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言實現字母加密成字母

發布時間: 2022-12-13 09:41:54

c語言如何字母加密

//參考如下:

//先輸入要加密的字母
//再輸入往後移動幾位的參數
//最後輸出加密後的字母
//絕對簡單,又符合要求int main()

#include<stdio.h>

{
char c;
scanf("%c",&c);
int a;
scanf("%d",&a);
printf("%c ",c+a);
return 0;
}

② C語言的程序設計 電文加密,每個字母轉換為字母表中循環右移的第三個字母。

//對一行電文進行加密,每個字母轉換為字母表中循環右移的第三個字母
//大寫字母C加密後的ASCII碼值為(c-62)%26+65
//小寫字母C加密後的ASCII碼值為(c-94)%26+97
#include<stdio.h>
void main()
{
//定義數組a和b,以及控制變數i
char a[3];
char b[3];
int i;

//提示輸入三個字母
printf("請輸入三個字母不要用空格隔開\n");
//用for循環控制接受字母
for(i=0;i<3;i++)
scanf("%c",&a[i]);

//在下一循環前加入該句
printf("經加密後為:");

//用for循環計算加密後的字母的ASCII值
for(i=0;i<=2;i++)
{
if(a[i]>=97)
b[i]=(a[i]-94)%26+97;
if(a[i]>=65&&a[i]<97)
b[i]=(a[i]-62)%26+65;
//輸出結果
printf("%c",b[i]);
}
printf("\n");
}

③ 用C語言加密 字母信息加密字母按字母表,進行對調,大小寫不變(a-z,b-y,c-x,…) 急求答案!

//VC++6.0下進行編譯
#include <stdio.h>
#define N 25

void jiami(char namea[256])
{
FILE *fp_jiami,*fp_file2;
char c;
fp_jiami=fopen(namea,"rb");
fp_file2=fopen("file2.plg","wb");
while(EOF!=(fscanf(fp_jiami,"%c",&c)))
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
{
c=c+N;
if (!((c>='A'&&c<='Z')||(c>='a'&&c<='z')))c=c-26;
if(c>='a'&&c<='z')c=c-32;
}
fprintf(fp_file2,"%c",c);
}
fclose(fp_file2);
fclose(fp_jiami);
}

void jiemi(char en_name[256])
{
FILE *fp_jiemi,*fp_file3;
char c;
fp_jiemi=fopen(en_name,"rb");
fp_file3=fopen("file3.plg","wb");
while(EOF!=(fscanf(fp_jiemi,"%c",&c)))
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
{
c=c-N;
if (!((c>='A'&&c<='Z')||(c>='a'&&c<='z')))c=c+26;
if(c>='A'&&c<='Z')c=c+32;
}

fprintf(fp_file3,"%c",c);
}
fclose(fp_file3);
fclose(fp_jiemi);

}

int main()
{
char name[256];
int n;

printf("輸入你要操作的TXT文本:");
gets(name);

printf("\n請選擇需要進行的操作:\n");
printf(" 1:加密 2:解密 \n");
printf("輸入你的選擇:");

scanf("%d",&n);
switch(n) {
case 1:{jiami(name);printf("\t加密成功!!\n\n");
break;}
case 2:{jiemi(name);printf("\t解密成功!!\n\n");
break;}
default:{printf("輸入操作不存在!");}
}

return 0;

}

④ C語言簡單字母加密

#include <stdio.h>
int main()
{
char ch;
printf("Enter the char be encrypted\n");
cin>>ch;
char c=(ch-'A'+3)%26+'a';
printf("%c",c);
return 0;
}

⑤ c語言對大寫英文字母加密

#include <stdio.h>
#include <string.h>
int main()
{
char passwd[100],encrypted[100];
int i,j,k,t,move;
while(1)
{
printf("Enter message to be encrypted:");

gets(passwd);

move=3;
for(i=0; i<strlen(passwd); i++)
{
if(passwd[i] >= 'A' && passwd[i] <= 'Z')
{

passwd[i] = ((passwd[i]-'A')+move)%26+'A';

} else if(passwd[i] >= 'a' && passwd[i] <= 'z')
{

passwd[i] = ((passwd[i]-'a')+move)%26+'a';

}

}

printf("%s",passwd);

printf("\n");

}
return 0;
}
這道題實際上就是C語言版的凱撒加密(字母往後面移動1-25之間的任意一位數)

⑥ C語言對字元進行加密

if(e>='A'&&e<='W')//
e+=3;
elseif(e>='X'&&e<='Z')
e-=23;
else
{
...//donotencryptordosomethingelse
}

⑦ C語言怎麼加密字元

#include<stdio.h>
#include<string.h>
intmain()
{
charstr[]="00000",str2[]="00000",*p=str,*p2=str2;
printf("輸入5個字母:");
while(*p!=0)
{
scanf("%c",p);
if(*p==' ')
continue;
if(*p<'A'||(*p>'Z'&&*p<'a')||*p>'z')//輸入驗證,必須是字母
{
printf("只能輸入字母,請重新輸入 ");
p=str;
p2=str2;
fflush(stdin);//輸入有錯重新輸入前清空緩沖區。fflush屬於c擴展函數,正常使用沒問題,如需在linuxggc上使用,考慮多次調用getchar函數來清空
}
else
{
*p2=(*p)+4;
if(*p2>90&&*p2<97)//大寫字母加4,最大位不超出
*p2='A'+(*p2-90)-1;
if(*p2>122)//小寫字母加4,最大位不超出
*p2='a'+(*p2-122)-1;
p2++;
p++;
}
}

printf("原字元串為:%s 加密後的字元串為:%s ",str,str2);
return0;
}

⑧ C語言題編程實現對鍵盤輸入的大寫英文字母進行加密。字母

#include<stdio.h>
#include<ctype.h>
intmain()
{inti;
chars[200];
gets(s);
for(i=0;s[i];i++)
if(isalpha(s[i]))
{s[i]+=3;
if(s[i]%0x20>26)s[i]-=26;
}
puts(s);
return0;
}

⑨ C語言英文文本加密

#include "stdio.h"

#include <stdlib.h>

int main(int argc,char *argv[]){

FILE *fp,*fq;

int k,t;

fp=fopen("AAA12345678901.txt","w+");

if(!fp || (fq=fopen("tmp.txt","w"))==NULL){

printf("Failed to open the file and exit... ");

return 0;

}

printf("Please enter a short passage(letters+space+punctuation,'Enter' end)... ");

while((t=getchar())!=' ')//為文件輸入內容

fputc(t,fp);

printf("Please enter the encryption key(int >0)... k=");

while(scanf("%d",&k)!=1 || k<1){//輸入加密密鑰並判斷是否正確

printf("Input error, redo: ");

fflush(stdin);

}

rewind(fp);

while(t=fgetc(fp),!feof(fp))//加密

if(t>='A' && t<='Z')

fputc(((t-'A')+k)%26+'A',fq);

else if(t>='a' && t<='z')

fputc(((t-'a')+k)%26+'a',fq);

else

fputc(t,fq);

fclose(fp);//關閉原文件

fclose(fq);//關閉加密後的文件

remove("AAA12345678901.txt");//刪除原文件

rename("tmp.txt","AAA12345678901.txt");//將加密後的文件更換為原文件名

printf(" ");

if(fp=fopen("AAA12345678901.txt","r")){

while((t=fgetc(fp))!=EOF)

printf("%c",t);

printf(" Encryption success! ");

}

else

printf(" Failed to open the encrypted file... ");

fclose(fp);

return 0;

}

代碼格式和運行樣例圖片:

⑩ c語言字母加密

按照你的要求編寫的字母加密的C語言程序如下

(姓字母向後移兩位,名字母向後移三位)

#include<stdio.h>

#include<string.h>

int main(){

char src[30],result[]="",ch[2]={''};

int i,j,len;

fgets(src,30,stdin);

len=strlen(src);

for(i=0;src[i]!=' ';i++){

if('a'<=src[i] && src[i]<='z'){

ch[0]=(char)(((src[i]-'a')+2)%26+'a');

strcat(result,ch);

}else if('A'<=src[i] && src[i]<='Z'){

ch[0]=(char)(((src[i]-'A')+2)%26+'A');

strcat(result,ch);

}else{

ch[0]=src[i];

strcat(result,ch);

}

}

for(j=i;j<len;j++){

if('a'<=src[j] && src[j]<='z'){

ch[0]=(char)(((src[j]-'a')+3)%26+'a');

strcat(result,ch);

}else if('A'<=src[j] && src[j]<='Z'){

ch[0]=(char)(((src[j]-'A')+3)%26+'A');

strcat(result,ch);

}else{

ch[0]=src[j];

strcat(result,ch);

}

}

printf("%s ",result);

return 0;

}