導航:首頁 > 文檔加密 > c語言電文加密az

c語言電文加密az

發布時間:2022-08-14 19:08:38

1. C語言 文件加密解密

根據你的需要,修改了之前的代碼。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

constunsignedintMAX_KEY_LENGTH=1000;

intencode(charconst*datafile,charconst*keyfill);
intdecode(charconst*datafile,charconst*keyfile);
intloadKey(charconst*keyfile,int*keys,unsignedintsize);
intsaveKey(charconst*keyfile,int*keys,unsignedintsize);
intgenerateKey(int*keys,unsignedintsize);

intmain(intargc,charconst*argv[])
{
chardatafile[]="encrypted.txt";
charkeyfile[]="key.txt";
intretcode,choice,loop=1;
charch[5]={''};

while(1)
{
printf("1.Encryption. ");
printf("2.Decryption. ");
printf("3.Exit. ");
printf("Selection(1,2,3):");
fgets(ch,sizeof(ch),stdin);
sscanf(ch,"%d",&choice);
switch(choice)
{
case1:
retcode=encode(datafile,keyfile);
if(retcode!=0)printf("error,%d",retcode);
break;

case2:
retcode=decode(datafile,keyfile);
if(retcode!=0)printf("error,%d",retcode);
break;

case3:
loop=0;
break;
default:
;
break;
}
if(0==loop)break;
}
return0;
}


intgenerateKey(int*keys,unsignedintsize)
{
charstr[]=",./;"'<>?";
size_tstr_len=sizeof(str)/sizeof(str[0]);
inti;

srand(time(NULL));
for(i=0;i<size;++i)
keys[i]=str[rand()%str_len];

return0;
}

intloadKey(charconst*keyfile,int*keys,unsignedintsize)
{
inti=0;
FILE*pfile;
intretcode=0;

pfile=fopen(keyfile,"r");
if(pfile==NULL)return-1;

while(!feof(pfile)){
if(i<size)
fscanf(pfile,"%d",&keys[i++]);
else
break;
}
fclose(pfile);
returni;
}

intsaveKey(charconst*keyfile,int*keys,unsignedintsize)
{
FILE*pfile;
inti;

pfile=fopen(keyfile,"w");
if(pfile==NULL)return-1;

for(i=0;i<size;++i){
fprintf(pfile,"%d",keys[i]);
}
fclose(pfile);
return0;
}

intencode(charconst*datafile,charconst*keyfile)
{
charoriginal[MAX_KEY_LENGTH]={''};
charencrypted[MAX_KEY_LENGTH]={''};
inti,size;
intkeys[MAX_KEY_LENGTH];
FILE*pdatafile,*pkeyfile;

pkeyfile=fopen(keyfile,"w");
if(NULL==pkeyfile)return-1;
fclose(pkeyfile);

puts("inputmessage:");
gets(original);
size=strlen(original);
if(0!=generateKey(keys,size))return-2;
if(0!=saveKey(keyfile,keys,size))return-3;

pdatafile=fopen(datafile,"w");
if(NULL==pdatafile)return-4;

for(i=0;i<size;++i){
encrypted[i]=original[i]+keys[i];
fputc(encrypted[i],pdatafile);
fputc(encrypted[i],stdout);
}
printf(" ");
fclose(pdatafile);
return0;
}

intdecode(charconst*datafile,charconst*keyfile)
{
FILE*pdatafile,*pkeyfile;
intkeys[MAX_KEY_LENGTH]={0};
charoriginal[MAX_KEY_LENGTH]={''};
charencrypted[MAX_KEY_LENGTH]={''};
inti,size;

pkeyfile=fopen(keyfile,"r");
if(NULL==pkeyfile)return-1;
fclose(pkeyfile);
pdatafile=fopen(datafile,"r");
if(NULL==pdatafile)return-2;

fscanf(pdatafile,"%s",encrypted);
fclose(pdatafile);

size=loadKey(keyfile,keys,MAX_KEY_LENGTH);
if(size<1)return-3;

for(i=0;i<strlen(encrypted);++i){
original[i]=encrypted[i]-keys[i];
fputc(original[i],stdout);
}
printf(" ");
return0;
}

運行結果:

1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):1
input message:
this is A test!
╓┐»╞Lñ╗ù|t▄╬╢╒è
1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):2
this is A test!
1. Encryption.
2. Decryption.
3. Exit.
Selection (1,2,3):3

2. 用c語言設計了一個加密演算法:用a代替z,用b代替y,用c代替x,……,用z代替a。

#include <stdio.h>

int main()
{
char s[100],*p;
printf("請輸入字元串 : ");
gets(s);
p = s;
while(*p)
{
if((*p >= 'a') && (*p <= 'z')) /*處理小寫*/
{
*p ='z' - *p + 'a';
}
if((*p >= 'A') && (*p <= 'Z')) /*處理大寫,同理處理數字亦一樣..自己例推*/
{
*p ='Z' - *p + 'A';
}

p++;
}
printf("轉換後的字元串為 : %s\n\n",s);
return 0;
}

3. 用C語言編程:對一行電文進行加密,每個字元轉換為字母表中循環右移的第三的字母,如:a-b,b-e.....大寫字

#include<stdio.h>

voidchange(chars[]){
inti;
for(i=0;s[i];++i){
if(s[i]>='a'&&s[i]<='z')
s[i]=(s[i]+2-'a')%26+'a';
elseif(s[i]>='A'&&s[i]<='Z')
s[i]=(s[i]+2-'A')%26+'A';
}
}

intmain(){
chars[256];
printf("輸入一個字元串:");
scanf("%s",s);
change(s);
printf("轉換後為:%s ",s);
return0;
}

4. 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");
}

5. c語言編程 電報密碼問題

int main()
{
char ch;
char c[100];
int i=0;
int AZ = 'A' + 'Z';
int az = 'a' + 'z';
printf("Please input:\n");
while((ch=getch()) != 13)
{
putch(ch);
if(ch>='A' && ch<='Z')
{
c[i++] = AZ - ch;
continue;
}
if(ch>='a' && ch<='z')
{
c[i++] = az - ch;
continue;
}
c[i++] = ch;
}
c[i] = '\0';
printf("\n%s\n",c);
}

6. C語言加密請將電文中所有字母按A→F,B→G,……V→A,W→B,X→C,Y→D,Z→E,a→f,b→g加密

char CTestDlg::code(char org)
{
if (org<'A')
return org; /*小於A,不加密*/
else if(org<='Z')
return 'A'*((org+5)/('Z'+1))+((org+5)%('Z'+1));
else if(org<='a')
return org;
else if(org<='z')
return 'a'*((org+5)/('z'+1))+((org+5)%('z'+1));
else
return org;
}
我剛才測試了,沒問題,直接用

7. c語言 a-z加密問題

假設原來的字元串數組中的元素為ch1,加密後的字元為ch2可以看出加密的過程是比原來的字元多3,可以採用取模運算進行循環,即Z之後可以用同樣的方式計算到A,公式:ch2=ch1%26+3;其他加密計算方式相同

8. C語言文件加密

#include<stdio.h>
intmain()
{charch;
FILE*fp1,*fp2;
fp1=fopen("d:\file1.txt","r");
fp2=fopen("d:\file2.txt","w");
printf("加密後的內容: ");
while((ch=fgetc(fp1))!=EOF)
{ch^=0x6a;putchar(ch);fputc(ch,fp2);}
fclose(fp1);
fclose(fp2);
printf(" 解密後的內容: ");
fp2=fopen("d:\file2.txt","r");
while((ch=fgetc(fp2))!=EOF)
{ch^=0x6a;putchar(ch);}
return0;
}

9. c語言 文本文件加密

哥們,我想說你瘋了吧,誰有時間給你寫這個程序啊,程序寫出來還要驗證,不驗證又不負責,你自己想想這需要多少的工作量?1小時?2小時?根本搞不定。
個人感覺很少有人願意花時間給你寫這個程序,建議你放棄,如果不會,找朋友或花錢。

閱讀全文

與c語言電文加密az相關的資料

熱點內容
電腦如何實現跨網段訪問伺服器 瀏覽:549
模塊化網頁源碼位元組跳動 瀏覽:485
梯度下降演算法中遇到的問題 瀏覽:605
伺服器連接電視怎麼接 瀏覽:323
phploop語句 瀏覽:500
交叉編譯工具鏈里的庫在哪 瀏覽:781
安卓手q換號怎麼改綁 瀏覽:399
nba球星加密貨幣 瀏覽:789
命令看網速 瀏覽:124
java堆分配 瀏覽:161
linuxbuiltin 瀏覽:560
cstpdf 瀏覽:941
texstudio編譯在哪 瀏覽:352
國家反詐中心app注冊登記表怎麼注冊 瀏覽:972
加密機默認埠 瀏覽:101
有哪個網站有免費的python源代碼 瀏覽:304
蘋果手機如何導入安卓電話 瀏覽:915
奧利奧雙重解壓 瀏覽:388
安卓賬號怎麼在蘋果手機上玩 瀏覽:798
畫畫用什麼安卓ipad好 瀏覽:693