project-3

#include
#include
#include
#include
#include
#include

using namespace std;
map <char,char> encry_map;
string encode_letter;
string encode_num=“1234567890”;
int generate_encode_map(char const * encode_ptr,int const length_str,string option_enOde);

#include “endecrypt.h”

int generate_encode_map(char const * encode_ptr,int const length_str,string option_enOde){//使用字典型数据类型存储加密/解密的对应关系,可以增加逻辑对于两种加密码的区分读入
char letter_l=‘a’;
char letter_b=‘A’;
for(int i=0;i<26;++i){
letter_l=(char)(97+i);
encode_letter.push_back(letter_l);
}
string::size_type label_encode_l;
string::size_type label_encode_n;
string::size_type label_bit;
string judge_blank=" ";
string bit_string;
label_encode_l=encode_letter.find(encode_ptr);//判断输入码本是字符型还是数字型
label_encode_n=encode_num.find(encode_ptr);
//cout<<"label_l is “<<label_encode_l<<” label_n is "<<label_encode_n<<endl;
if(label_encode_n!=string::npos && label_encode_l==string::npos ){//输入码本为数字型
int index_num=0;
for(int i=0;i<length_str;i++){
letter_l=(char)i;
label_bit=judge_blank.find(
(encode_ptr+i));//判断当前是否为空格
if(label_bit==string::npos){//不为空格,则添加1位到位string中
bit_string.push_back(
(encode_ptr+i));
}
else {
int bit_num=stoi(bit_string);//到达空格处读取到一个有效的编码
//cout<<"bit_num “<<bit_num<<” option is "<<option_enOde<<endl;
if (option_enOde==“en”){//加密时
encry_map.insert(pair<char,char>((char)index_num,(char)bit_num));}
else if(option_enOde==“de”){//解密时
encry_map.insert(pair<char,char>((char)bit_num,(char)index_num));}
else {
cout<<“It need parameter to operate”<<endl;}
cout<<"index of string “<<i<<” index of encode number is “<<index_num<<endl;
++index_num;
bit_string.erase();
}
}
map<char,char>::iterator it_map=encry_map.begin();
/cout<<"encode map is “<<endl;
for(;it_map!=encry_map.end();++it_map){
cout<<(unsigned int)it_map->first<<” "<<(unsigned int)it_map->second<<endl;}
/
return 0;
}
else if(label_encode_l!=string::npos && label_encode_nstring::npos ){//当前为字母码本时
if (option_enOde
"en”){//加密
for(int i=0;i<length_str;i++){
letter_l=(char)(97+i);
letter_b=(char)(65+i);
encry_map.insert(pair<char,char>(letter_l,(encode_ptr+i)));
encry_map.insert(pair<char,char>(letter_b,
(encode_ptr+i)));

        }
    }
    else if(option_enOde=="de"){//解密
        for(int i=0;i<length_str;i++){
            letter_l=(char)(97+i);
            encry_map.insert(pair<char,char>(*(encode_ptr+i),letter_l));
        }
    }
    else {
        cout<<"It need parameter to operate"<<endl;
    }
    map<char,char>::iterator it_map=encry_map.begin();
    cout<<"encode map is "<<endl;
    for(;it_map!=encry_map.end();++it_map){
        cout<<it_map->first<<" "<< it_map->second<<endl;
    }
    return 1;
}
else {//意外情况,码本非字母型也非数字型
    cout<<"the encode file is wrong,please enter the right file."<<endl;
    return -1;
}

}

int main(int argc,char * argv[]){
//cout<<"number of parameter is "<< argc<< endl;
if(argc!=5){//对输入参数进行参加校验
cout<<"Please enter the right number of parameter: "<<endl;
cout<<"1.encode file that generate string to encrypt/decrypt the file "<<endl;
cout<<"2.the file you want to encrypt/decrypt "<<endl;
cout<<"3.the output file after encypt/decrypt "<<endl;
cout<<"4.option en/de ,en is encryted, and de is decrypted "<<endl;
return -2;
}//读取命令行参数
string parm_encode_file=argv[1];
string parm_input_file=argv[2];
string parm_output_file=argv[3];
string parm_ENoDE=argv[4];

if (parm_ENoDE!="en" && parm_ENoDE!="de"){
    cout<<"The fourth parameter is en/de ,the othen string is forbidden."<<endl;
    return -3;
}


ifstream encode_file(parm_encode_file,ios_base::in);
if(!encode_file.is_open()){
    cout<<"Fail to open encode file "<<parm_encode_file<<endl;
    return -1;
}
string encode_string;
getline(encode_file,encode_string);
cout<<"encode is "<<encode_string<<endl;
char const * encode_ptr=&encode_string[0];
int const length_encode_string=encode_string.length();
int label_encode_type=generate_encode_map(encode_ptr,length_encode_string,parm_ENoDE);//生成码本文件
encode_file.close();

ifstream input_en_file;
ofstream output_en_file;

int line_num=0;

string output_en_string;
if(label_encode_type==1){//当前为字母型码本
    string input_en_string;
    
    input_en_file.open(parm_input_file,ios_base::in);
    if(!input_en_file.is_open()){
            cout<<"Fail to open encode file "<<parm_input_file<<endl;
            return -1;
    }
    
    while(getline(input_en_file,input_en_string)){//按行读入加密/解密文档
        cout<<"line "<<line_num<<endl;
        cout<<"Ready to encrypt string is "<<input_en_string<<endl;
        string::iterator it_en_input=input_en_string.begin();
        string::size_type label_letter;
        for( ;it_en_input!=input_en_string.end();++it_en_input){
            
            label_letter=encode_letter.find(*it_en_input);//不为字母时就不做处理
            if(label_letter!=string::npos){
                output_en_string.push_back(encry_map[*it_en_input]);
            }
            else{output_en_string.push_back(*it_en_input);}
        }
        output_en_string.append("\n");//每行结束时添加一个换行符
        cout<<"Encrypt string is "<<output_en_string<<endl;
        ++line_num;
    }
}
else if(label_encode_type==0){
    input_en_file.open(parm_input_file,ios_base::binary | ios_base::ate);//借鉴c++ reference中内容
    if(!input_en_file.is_open()){
            cout<<"Fail to open encode file "<<parm_input_file<<endl;
            return -1;
    }
    auto size_input_file=input_en_file.tellg();//获取在文件字符中末尾位置
    string input_string(size_input_file,'\0');//构造和文件中字符大小相同的字符串
    input_en_file.seekg(0);//移动到初始位置
    input_en_file.read(&input_string[0],size_input_file);//读入文件到input
    string::iterator it_input=input_string.begin();//读入完成后,进行输出编辑
    for( ;it_input!=input_string.end();++it_input){
        output_en_string.push_back(encry_map[*it_input]);
    }
}
if(input_en_file.is_open()){input_en_file.close();}

output_en_file.open(parm_output_file,ios_base::out);
output_en_file<<output_en_string;
output_en_file.close();

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值