中程在线信息产业培训网(www.miiceic.org.cn)
以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信号,但因不知解密的方法,仍然无法了解信息的内容。
加密建立在对信息进行数学编码和解码的基础上。 我们使用的加密类型分为两种密钥 -- 一种是公共密钥,一种是私人密钥。 您发送信息给我们时,使用公共密钥加密信息。 一旦我们收到您的加密信息,我们则使用私人密钥破译信息密码。 同一密钥不能既是加密信息又是解密信息。 因此,使用私人密钥加密的信息只能使用公共密钥解密,反之亦然,以确保您的信息安全。
举例如下:
代码如下:
/* Secure.c
Copyright (c) 2002, 2006 by ctu_85
All Rights Reserved.
*/
#include "stdio.h"
#include "string.h"
#define right 5
void Create();
void Load();
char secure(char);
char desecure(char);
void main()
{
int choice;
printf("Please enter your choice:\n");
printf("0:To quit;\n");
printf("1:To create a security file;\n");
printf("2:To load a security file .\n");
cir:
printf("Your choice:");
scanf("%d",&choice);
if(choice==0)
return;
if(choice==1)
{
Create();
printf("\n");
goto cir;
}
else
if(choice==2)
{
Load();
printf("\n");
goto cir;
}
else
{
printf("Invalid input!\n");
goto cir;
}
}
void Create()
{
FILE *fp;
char *p,ch,*s;
recre:
printf("Please enter the path where you wanna the file to be:");
scanf("%s",p);
if(*p<'C'||*p>'F'||*(p+1)!=':'||*(p+2)!=92||strlen(p)>30||strlen(p)<4)
{
printf("Invalid input!\n");
goto recre;
}
if((fp=fopen(p,"wb"))==NULL)
{
printf("Error!");
return;
}
an:
printf("Please set the password:");
scanf("%s",s);
if(strlen(s)>16||strlen(s)<6)
{
printf("The password is too long or too short,please reinput!\n");
goto an;
}
while(*s!='\0')
{
ch=*s;
ch=secure(ch);
s++;
fputc(ch,fp);
}
ch='\n';
ch=secure(ch);
fputc(ch,fp);
printf("Please enter the information,end with char '#':");
ch=getchar();
ch=getchar();
while(ch!='#')
{
ch=secure(ch);
fputc(ch,fp);
ch=getchar();
}
ch=secure(ch);
fputc(ch,fp);
fclose(fp);
}
void Load()
{
FILE *fp;
char *p,ch,*s,temp[18],pass[18],sign=secure('\n');
int i=0,t=0,lenth=0;
rece:
printf("Please enter the path where you wanna to load:");
scanf("%s",p);
if(*p<'C'||*p>'F'||*(p+1)!=':'||*(p+2)!=92||strlen(p)>30||strlen(p)<4)
{
printf("Invalid input!\n");
goto rece;
}
if((fp=fopen(p,"rb"))==NULL)
{
printf("Error!");
return;
}
ant:
printf("Please input the password:");
scanf("%s",s);
lenth=strlen(s);
if(lenth>16||lenth<6)
{
printf("The password is obviously incorrect!\n");
goto ant;
}
while(*s!='\0')
{
temp=secure(*s);
s++;
i++;
}
temp='\0';
ch=fgetc(fp);
while(ch!=sign)
{
pass[t]=ch;
t++;
ch=fgetc(fp);
}
pass[t]='\0';
ch=desecure(ch);
if(!strcmp(temp,pass))
{
while(ch!='#')
{
ch=fgetc(fp);
ch=desecure(ch);
if(ch!='#')
putchar(ch);
}
}
else
printf("The password is incorrect!\n");
fclose(fp);
}
char secure(char c)
{
if(c+right>254)
return c-255+right;
else
return c+right;
}
char desecure(char c)
{
if(c<right)
return 255-right;
else
return c-right;
}