當前位置:首頁 » 密碼管理 » c級加密

c級加密

發布時間: 2023-05-22 04:33:21

1. c語言 文件加密

c語言文件加密和解密方法如下:

1、首先打開VC++6.0;

voidDecryptFile(FILE*sfp,FILE*dfp,charpwd)
{
charch;
while((ch=fgetc(sfp))!=EOF)
{
if((ch>='a')&&(ch<='z'))
{
ch=ch^pwd;
ch=(ch-'a'+25)%26+'a';
}
if((ch>='A')&&(ch<='Z'))
{
ch=ch^pwd;
ch=(ch-'A'+25)%26+'A';
}
fputc(ch,dfp);
}
}

輸出函數,輸出文件內容
voidOutputFile(FILE*fp)
{
charch;
while((ch=fgetc(fp))!=EOF)
putchar(ch);
}
主函數,主要調用這幾個函數
int哪銀main()
{
/*用戶輸入的要加密的文件名*/
charsfilename[20];
/*用戶輸入加密後保存的文件名*/
chardfilename[20];
/*用來保存密碼字元*/
charpwd;
FILE*sfp,*dfp;

printf(": ");
/*得到要加密的文件名*/
gets(sfilename);
/*得到加密後你要的文件名*/
printf(": ");
gets(dfilename);
/*得到加密字元*/
printf("PleaseinputyourPassword: ");
//scanf("%c",&pwd);
pwd=getch();
/*屏幕以*來表示輸入的加密字元*/
printf("* ");
/*以只讀方式打開要加密的文件*/
if((sfp=fopen(sfilename,"r"))==0)
{
printf("Can'topenthefile:%s ",sfilename);
exit(0);
}
/*輸出要加密的文件*/
printf(": ");
OutputFile(sfp);
/*建立加密後的文件*/
if((dfp=fopen(dfilename,"w+"))==0)
{
printf("Can'topenorcreatethefile:%s ",dfilename);
//exit(0);
}
/*文件加密*/
fseek(sfp,0L,SEEK_SET);
EncryptFile(sfp,dfp,pwd);
printf(" Encryptedthefilesuccessfully! ");
/*輸出加密後的文件*/
printf(": ");
fseek(dfp,0L,SEEK_SET);
OutputFile(dfp);
fclose(sfp);
fclose(dfp);
李梁宴getch();
return0;
}


2. C語言簡單加密

#include "stdio.h"
#define N 80
void main()
{
char str[N];
int i;
printf("Please input string:\n");
gets(str);
for(i=0;str[i]!='枝遲\0';i++)
{
if((str[i]>=97)&&(str[i]<=112))
{
str[i]+=10;
}
else if((str[i]>112)&&(str[i]<=122))
{
str[i]-=16;
}
}
str[i]='\猛橡李0';
printf("Result:\n"如襲);
puts(str);
getch();
}

3. C語言加密演算法(簡單)

MD5是HASH演算法,他不能用來解密的,他主要是用來校驗信息的完整型,也就是我們常說的數值簽名,你可以去RFC文檔上收索,上邊有他具體的演算法,代碼也是封裝好了的,可以去研究研究

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

5. c語言的簡單加密

#include <stdio.h>#include <math.h>
int main()
{
char a[10];
int i=0;
while(getchar()!='\n')
{
a[i++]=getchar(); //錯誤在這里,上一句while(getchar()!='\n'腔枯喚),如果讀入字元不是回車
} //那麼再讀入一個字元,賦給數組敗物里的元素。也就是說,每兩個字元
a[i]='\n'; //能讀入第二個字元。如果讀入奇數個字元,則無法結束程序。
i=0;
do
{
printf("%c",a[i++]+4);
}
while(a[i]!='\n');
}

修改如下,運行OK:
#include <stdio.h
int main()
{
char a;
int i=0;
while((a=getchar())!='\n')
{
printf("%c"伍凱,a+4);
}
printf("\n");
}

6. 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

7. 關於C語言的加密

'a'的ASC碼是97
'b'的ASC碼是98
'c'的ASC碼是99
... ...
'z'的ASC碼是122

明文和密文相對應
如明文:"abc"
密文:"zyw"

當ch1 = 'a'時,ch2 = 'z'
ch1 - 97 = 97 + 25 - ch2
97 - 97 = 97 + 25 - 122 = 0

當ch1 = 'b'時,ch2 = 'y'
ch1 - 97 = 97 + 25 - ch2
98 - 97 = 97 + 25 - 121 = 1

當ch1 = 'c'時,ch2 = 'w'
ch1 - 97 = 97 + 25 - ch2
99 - 97 = 97 + 25 - 120 = 2

ch1 - 97 = 97 + 25 - ch2這實際上是明文與密文的對應關系,也就是他的演算法

你可以對明文去設定一個對應關系,就可以得到不同的密文

8. c語言 加密

#include <stdio.h>
void jiami(char *str)
{
int i;
for(i=0;str[i]!='\0';i++)
{
str[i]=str[i]+3;
if((str[i]>'z')||(str[i]>'Z'&&str[i]<'a')) str[i]-=26;
if(str[i]>='a') str[i]-='a'-'A'模陪;
else str[i]+='a'-'A';
}

}
void jiemi(char *str)
{
int i;
for(i=0;str[i]!='\0';i++)
{
str[i]=str[i]-3;
if((str[i]<'A')||(str[i]>'Z'&&str[i]<'a')) str[i]+=26;
if(str[i]>='a') str[i]-='a'-'A';
else str[i]+='斗碼殲a'-'A';
}
}

void main()
{
char a[]="空沖Text";
jiami(a);
printf("%s\n",a);
jiemi(a);
printf("%s\n",a);

}

熱點內容
外網訪問黑群暉 發布:2025-02-08 05:45:59 瀏覽:559
中央存儲伺服器公司地址 發布:2025-02-08 05:38:48 瀏覽:821
伺服器如何查詢表空間的文件路徑 發布:2025-02-08 05:38:00 瀏覽:162
宏基4741g哪個配置好 發布:2025-02-08 05:37:56 瀏覽:810
混合料運輸車的配置是如何計算的 發布:2025-02-08 05:31:35 瀏覽:293
android紅包插件 發布:2025-02-08 05:31:34 瀏覽:365
ea伺服器怎麼連接 發布:2025-02-08 05:16:45 瀏覽:463
更加密更改 發布:2025-02-08 05:15:20 瀏覽:786
倉儲資源配置都需要開展哪些任務 發布:2025-02-08 05:13:51 瀏覽:676
探針資料庫 發布:2025-02-08 05:13:35 瀏覽:80