免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 797 | 回复: 0
打印 上一主题 下一主题

解密求help! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-01-01 12:23 |只看该作者 |倒序浏览
我写的一个异或加密解密的小程序,但是为什么解密的时候多开头比源文件多了一个乱码呢?

#include<fstream>;
#include<iostream>;
#include<iomanip>;
#include<cstdlib>;
using namespace std;
void add_key(char *source_file,char* d_file,char* pwd)
        {
                char ch;int i=0,t=0;
                int j=0;
                ifstream fin1(source_file,ios::in|ios::binary);
                ofstream fout2(d_file,ios::app|ios::binary);
                if(fin1.is_open()&&fout2.is_open())
                {
                       
                 while(pwd[j++]){};
                 while(fin1.read((char*)&ch,sizeof ch))
                // for(int t=0;t<3;t++)
                 { //fin1.read((char*)&ch,sizeof ch);
                        // t++;
                  ch=(ch^pwd);
                    if(i==(j-2)) i=0;
                //        if(t>;1)
                        fout2.write((char*)&ch,sizeof ch);
                 }
                 if(fin1.eof())
                         fin1.clear();
                 fin1.close();fout2.close();
                }
                else
                {
                        cerr<<"Open the file failed!";
                        exit(1);
                }
        }
void exact_key(char *source_file,char* d_file,char* pwd)
        {
                char ch;int i=0,t=0;
                int j=0;
                ifstream fin1(source_file,ios::in|ios::binary);
                ofstream fout2(d_file,ios::app|ios::binary);
                if(fin1.is_open()&&fout2.is_open())
                {
                       
                 while(pwd[j++]){};
                 while(fin1.read((char*)&ch,sizeof ch))
                // for(int t=0;t<3;t++)
                 { //fin1.read((char*)&ch,sizeof ch);
                        // t++;
                  ch=(ch^pwd);
                    if(i==(j-2)) i=0;
                //        if(t>;1)
                        fout2.write((char*)&ch,sizeof ch);
                 }
                 if(fin1.eof())
                         fin1.clear();
                 fin1.close();fout2.close();
                }
                else
                {
                        cerr<<"Open the file failed!";
                        exit(1);
                }
        }
int main()
{
        char source_file[40];
        char target_file[20];
//        char third_file[20]="g:\\13.txt";
    char pwd[8];
//        char ch;

    cout<<"lease input source_file path you want to keep password:";
        cin.get(source_file,40);
                while(cin.get()!='\n')
                        continue;
        cout<<"lease input target_file path you want to keep save:";
        cin.get(target_file,40);
                        while(cin.get()!='\n')
                        continue;
cout<<"lease input the password:";
            cin.get(pwd,;
                while(cin.get()!='\n')
                        continue;//最多八位,多了忽略!

        add_key(source_file,target_file,pwd);

        exact_key(target_file,third_file,pwd);
                return 0;

}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP