C++内嵌汇编代码,简单文件加密

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]){
    if(argc < 3){
        cout<<"Usage: Project infile outfile"<<endl;
        return -1;
    }
    const int BUFSIZE = 2000;
    char buf[BUFSIZE];
    unsigned int count;    // char count
    unsigned int encryptMask;
    cout<<"Encryption code[0-255]?";
    cin>>encryptMask;
    if(encryptMask<0 || encryptMask > 255){
        cout<<"between 0 and 255."<<endl;
        return -1;
    }    
    unsigned char encryptCode = (unsigned char)encryptMask;
    ifstream infile(argv[1],ios::binary);
    ofstream outfile(argv[2],ios::binary);

    cout << "Reading "<<argv[1]<<"and createing"<<argv[2]<<endl;
    while(!infile.eof()){
        infile.read(buf,BUFSIZE);
        count = infile.gcount();
        if(count==0)break;
        __asm{
            lea esi, buf
            mov ecx,count
            mov al,encryptCode
L1:
            xor [esi],al
            inc esi
            loop L1
        }
        outfile.write(buf,count);
    }
    infile.close();
    outfile.close();
    return  0;
}

 

posted @ 2015-05-05 18:25  庚武  Views(489)  Comments(0Edit  收藏  举报