当前位置:首页 » 密码管理 » 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);

}

热点内容
咪咕音乐linux 发布:2025-02-08 02:53:04 浏览:409
我的世界手机版大陆练习服务器 发布:2025-02-08 02:50:43 浏览:212
php的特点与优势 发布:2025-02-08 02:43:16 浏览:718
微信公众号怎么上传pdf 发布:2025-02-08 02:42:41 浏览:347
安卓如何查看通话总时长 发布:2025-02-08 02:27:49 浏览:578
快速dct算法 发布:2025-02-08 02:19:04 浏览:623
淘宝交易密码如何改 发布:2025-02-08 02:17:32 浏览:775
php的进阶 发布:2025-02-08 02:17:28 浏览:677
服务器关闭中或IP地址错误 发布:2025-02-08 02:16:55 浏览:480
节目脚本是什么 发布:2025-02-08 02:08:54 浏览:144