简单问题:如何对一个文本文件进行数据加密???

番茄家 2003-04-25 10:46:48
我想对自己程序中使用的几个文本文件进行一下简单的加密,以防止他人查看到文件中的内容。不知道哪位兄弟可以给出一个比较简单的加密算法?最好能够同时给出加密函数与解密函数,以便我在程序中直接调用!谢谢啦!
...全文
714 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghtsao 2003-04-26
  • 打赏
  • 举报
回复
我倒有一个RC4加密的源代码,经过RSA公司网验证的,我觉得不错。大家上RSA网去看一下,加密的权威。XOR之类自己写的算法太烂了,看看专家怎么用的吧。
jiezhuang  2003-04-25
  • 打赏
  • 举报
回复
簡單加密最好的方法是對每個BYTE XOR 你的KEY
有證明這種加密基本上不易破解。
yushulei 2003-04-25
  • 打赏
  • 举报
回复
自己写一个了,可以简单一些吗!
比如你可以依次取出字符,然后可以加一固定的值,再输出,
自己看的时候再反其道行之不就得了。
hongyanzaiyun 2003-04-25
  • 打赏
  • 举报
回复
实现文件太长.......
hongyanzaiyun 2003-04-25
  • 打赏
  • 举报
回复
/* d3des.h -
*
* Headers and defines for d3des.c
* Graven Imagery, 1992.
*
* Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge
* (GEnie : OUTER; CIS : [71755,204])
*/

#define D2_DES /* include double-length support */
#define D3_DES /* include triple-length support */

#ifdef D3_DES
#ifndef D2_DES
#define D2_DES /* D2_DES is needed for D3_DES */
#endif
#endif

#define EN0 0 /* MODE == encrypt */
#define DE1 1 /* MODE == decrypt */

/* A useful alias on 68000-ish machines, but NOT USED HERE. */

typedef union {
unsigned long blok[2];
unsigned short word[4];
unsigned char byte[8];
} M68K;

extern void deskey(unsigned char *, short);
/* hexkey[8] MODE
* Sets the internal key register according to the hexadecimal
* key contained in the 8 bytes of hexkey, according to the DES,
* for encryption or decryption according to MODE.
*/

extern void usekey(unsigned long *);
/* cookedkey[32]
* Loads the internal key register with the data in cookedkey.
*/

extern void cpkey(unsigned long *);
/* cookedkey[32]
* Copies the contents of the internal key register into the storage
* located at &cookedkey[0].
*/

extern void des(unsigned char *, unsigned char *);
/* from[8] to[8]
* Encrypts/Decrypts (according to the key currently loaded in the
* internal key register) one block of eight bytes at address 'from'
* into the block at address 'to'. They can be the same.
*/

#ifdef D2_DES

#define desDkey(a,b) des2key((a),(b))
extern void des2key(unsigned char *, short);
/* hexkey[16] MODE
* Sets the internal key registerS according to the hexadecimal
* keyS contained in the 16 bytes of hexkey, according to the DES,
* for DOUBLE encryption or decryption according to MODE.
* NOTE: this clobbers all three key registers!
*/

extern void Ddes(unsigned char *, unsigned char *);
/* from[8] to[8]
* Encrypts/Decrypts (according to the keyS currently loaded in the
* internal key registerS) one block of eight bytes at address 'from'
* into the block at address 'to'. They can be the same.
*/

extern void D2des(unsigned char *, unsigned char *);
/* from[16] to[16]
* Encrypts/Decrypts (according to the keyS currently loaded in the
* internal key registerS) one block of SIXTEEN bytes at address 'from'
* into the block at address 'to'. They can be the same.
*/

extern void makekey(char *, unsigned char *);
/* *password, single-length key[8]
* With a double-length default key, this routine hashes a NULL-terminated
* string into an eight-byte random-looking key, suitable for use with the
* deskey() routine.
*/

#define makeDkey(a,b) make2key((a),(b))
extern void make2key(char *, unsigned char *);
/* *password, double-length key[16]
* With a double-length default key, this routine hashes a NULL-terminated
* string into a sixteen-byte random-looking key, suitable for use with the
* des2key() routine.
*/

#ifndef D3_DES /* D2_DES only */

#define useDkey(a) use2key((a))
#define cpDkey(a) cp2key((a))

extern void use2key(unsigned long *);
/* cookedkey[64]
* Loads the internal key registerS with the data in cookedkey.
* NOTE: this clobbers all three key registers!
*/

extern void cp2key(unsigned long *);
/* cookedkey[64]
* Copies the contents of the internal key registerS into the storage
* located at &cookedkey[0].
*/

#else /* D3_DES too */

#define useDkey(a) use3key((a))
#define cpDkey(a) cp3key((a))

extern void des3key(unsigned char *, short);
/* hexkey[24] MODE
* Sets the internal key registerS according to the hexadecimal
* keyS contained in the 24 bytes of hexkey, according to the DES,
* for DOUBLE encryption or decryption according to MODE.
*/

extern void use3key(unsigned long *);
/* cookedkey[96]
* Loads the 3 internal key registerS with the data in cookedkey.
*/

extern void cp3key(unsigned long *);
/* cookedkey[96]
* Copies the contents of the 3 internal key registerS into the storage
* located at &cookedkey[0].
*/

extern void make3key(char *, unsigned char *);
/* *password, triple-length key[24]
* With a triple-length default key, this routine hashes a NULL-terminated
* string into a twenty-four-byte random-looking key, suitable for use with
* the des3key() routine.
*/

#endif /* D3_DES */
#endif /* D2_DES */

/* d3des.h V5.09 rwo 9208.04 15:06 Graven Imagery
********************************************************************/
snipersu 2003-04-25
  • 打赏
  • 举报
回复
roll 13.
yxc2008 2003-04-25
  • 打赏
  • 举报
回复
up
shishiXP 2003-04-25
  • 打赏
  • 举报
回复
写个加密函数。用个文件保存密码。
以下是以前写的一个,有些不够简化。
后来写的一个没带来,不过这个也能用。
加点分!!

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int passwd_enable;

int passwd_treat(const char *pwd,int mod)
{
if(mod==0) //解密
{
FILE *pfrk=fopen("user.dcc","r"); //取密码
if(pfrk==NULL)return -1;
char key_buf[20];
fgets(key_buf,20,pfrk);

char key[20];
int i=0;
int j;
int n=strlen(key_buf);
if(n%2==0)j=n/2;
else j=n/2+1;
for( ;j>0; j--,i++)key[i*2]=key_buf[i]+15-i; //单数位
for(j=0;j<n/2;j++,i++)key[j*2+1]=key_buf[i]+15-i; //双数位
key[n]='\0';

if(strcmp(key,pwd)==0) //test
{
fclose(pfrk);
return 0;
}
else
{
fclose(pfrk);
return -1;
}
}

else if(mod==1) //加密
{
char key[20];
int i=0;
int j;
int n=strlen(pwd);
if(n%2==0)j=n/2;
else j=n/2+1;
for( ;j>0; j--,i++)key[i]=pwd[i*2]-15+i; //单数位
for(j=0;j<n/2;j++,i++)key[i]=pwd[j*2+1]-15+i; //双数位
key[n]='\0';

FILE *pfwk=fopen("user.dcc","w");
if(pfwk==NULL)return -1;
fputs(key,pfwk);
if(n==0)passwd_enable=-1;
}
else return -1;
}

void passwd_login()
{
passwd_enable=0;
FILE *pfrkn=fopen("user.dcc","r");
if(pfrkn==NULL)passwd_enable=-1;
else
{
char enable[2];
fgets(enable,2,pfrkn);
if(enable[0]=='\0')passwd_enable=-1;
fclose(pfrkn);
}
if(passwd_enable==-1)return;
char passwd[20];
cout<<"\n密 码:";
cin.get(passwd,20);
while(cin.get()!='\n');
if(passwd_treat(passwd,0)==0)return;
for(int i=3;i>0;i--)
{
cout<<"\n密码错误!请重输:";
cin.get(passwd,20);
while(cin.get()!='\n');
if(passwd_treat(passwd,0)==0)return;
}
exit(0);
}

int passwd_test(const char *pwd)
{
while(*pwd!='\0')
{
char a=*pwd;
if((a>='0'&&a<='9')||(a>='a'&&a<='z')||(a>='A'&&a<='Z')||a=='_')pwd++;
else return -1;
}
return 0;
}

void passwd_set()
{
char passwd[20];
if(passwd_enable==0)
{
cout<<"\n请输入原密码:";
cin.get(passwd,20);
while(cin.get()!='\n');
if(passwd_treat(passwd,0)==-1)
{
cout<<"\n密码错误!";
getch();
return;
}
}
cout<<"\n\n请输入新密码:";
cin.get(passwd,20);
while(cin.get()!='\n');
if(passwd_test(passwd)==0)
{
if(passwd_treat(passwd,1)==0)
{
cout<<"\n新密码设置成功!";
passwd_enable=0;
getch();
return;
}
}
cout<<"新密码错误!";
getch();

}

void main()
{
char u;
passwd_login();
cout<<"\n\n 进入";
cout<<"\n\n 要设置密码吗?";
cin>>u;
if(u=='y'||u=='Y'){cin.get();passwd_set();}
cout<<"\n\n 返回";
//////////////////////end//////////////////////

}
bky2003 2003-04-25
  • 打赏
  • 举报
回复
/*程序: jm.c*/
/*加密、解密都用这个文件对应的exe文件*/
//用法:jm 源文件 密码本
#include<stdio.h>
void main(int argc,char* argv[]){
//这里没有判断命令行参数,你可以加上
FILE fp=fopen(argv[1],"rb"); //第一个参数是源文件
FILE ft=fopen(argv[2],"rb");
FILE ftmp=fopen("tmp","wb");//存入一个临时文件
//判断文件是否打开了,略
char c1,c2;
while(!feof(fp)){//假设密码本比源文件大
c1=fgetc(fp);
c2=fgetc(ft);
c1!^=c2;
fputc(ftmp,ftmp);
}
fcloseall();
//删除源文件,把临时文件改名为源文件名
//就完成了
}
hongyanzaiyun 2003-04-25
  • 打赏
  • 举报
回复
对其进行二进制处理,比如移位,异或,求反等等
解密就反过来就是
aooooooo 2003-04-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

main()
{
FILE *fp_from,*fp_to;
char *buf,*head;
char tmp;
if((fp_from=fopen("text1.txt","rb"))==NULL)
{
printf("text1.txt error!");
exit(0);
}

if((fp_to=fopen("text2.txt","wb"))==NULL)
{
printf("text2.txt error!");
exit(0);
}
head=buf;
while(!feof(fp_from))
{
tmp=fgetc(fp_from);
buf=tmp^8;
buf++;
fp_from++;
}
while(head!=buff)
{
fputc(*head,fp_to);
head++;
}
printf("saved!");
getchar();
fclose(fp_from);
fclose(fp_to);
}
text1.txt为原文件,8为KEY,TEXT2。TXT为处理后文件。
想还原就把text2.txt文件名改为text1.txt在用此程序处理一次就成原文件了。
bky2003 2003-04-25
  • 打赏
  • 举报
回复
要简单的,你找一个大文件,比如MP3或MTV的文件(密码本),
对你的文件和Mp3逐个Byte进行异或(XOR),得到密文
加密和解密算法一样,再异或一下就得到明文了。
不过,你要保存住密码本,这个应该容易。
你不说是哪个文件,基本上没办法破解密码本方法加密的密文。

luckysky 2003-04-25
  • 打赏
  • 举报
回复
和一个关键字进行异或,这样解密只要在调用一次就好了。
番茄家 2003-04-25
  • 打赏
  • 举报
回复
大家说的方法都很好……不过,有没有哪位可以给出代码来呢?
diabloqin 2003-04-25
  • 打赏
  • 举报
回复
des算法,源码网上有
seadreamer 2003-04-25
  • 打赏
  • 举报
回复
可以用一些数据压缩算法,比如lzss等,也能起到加密的作用,还能进行压缩。
chegtion 2003-04-25
  • 打赏
  • 举报
回复
同意楼上的。
而且很方便:加密和解密函数是一样的

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧