zoukankan      html  css  js  c++  java
  • 仿射密码加密解密文件流

    #include<iostream>
    #include<string>
    #include<fstream>
    #include<Windows.h>
    using namespace std;
    class PWoper
    {
        string inpatch;
        string outpatch;
    public:
        PWoper(string in, string out)
        {
            inpatch = in;
            outpatch = out;
        }
        void encrypt()
        {
            ifstream init(inpatch, ios::binary);
            ofstream outit(outpatch, ios::binary);
            if (!init || !outit)
            {
                cout << "IN ERROR OR OUT ERROR!" << endl;
            }
            int *initp = new int();
            int *outitp = new int();
            while (init.read((char*)initp, 1)){
                //cout << *initp << endl;
                *outitp = (*initp * 177 + 135) % 311;
                //cout << "out--"<<*outitp << endl;
    
                //一个字节8位,最大256;这里的 "*outitp mod 311" 数值最大311,如果以1个字读进来运算以后write装不下。
                outit.write((char*)outitp, 2);
            }
            cout << "encrypt success!
    ";
            init.close();
            outit.close();
            //delete original data
            
            string temp = "del ";
            temp += inpatch;
            //system命令只接受一个参数,以下代码实现system("del 变量")。
            const char *link = temp.data();
            system(link);
        }
        void decrypt()
        {
            ofstream init(inpatch, ios::binary);
            ifstream outit(outpatch, ios::binary);
            if (!init || !outit)
            {
                cout << "IN ERROR OR OUT ERROR!" << endl;
            }
            int *ini = new int();
            int *outi = new int();
            while (outit.read((char*)outi, 2)){
                //cout << *outi << endl;
                *ini = ((*outi - 135 + 311) * 123) % 311;
                //cout <<"out"<< *ini <<endl;
    
                //
                init.write((char*)ini, 1);
            }
            cout << "decrypt success!
    ";
            init.close();
            outit.close();
        }
    };
    int main()
    {
    
        string inpatch, outpatch, str;
        cout << "input InPtach: ";
        cin >> inpatch;
        cout << "input OutPatch: ";
        cin >> outpatch;
        PWoper pwoper(inpatch, outpatch);
        pwoper.encrypt();
        cout << "input ok to decrypt: ";
        cin >> str;
        //system("cls");
        if (str == "ok")
            pwoper.decrypt();
        else {
            cout << "input error!";
            system("pause");
        }
        system("pause");
    
    }
  • 相关阅读:
    多线程编程:阻塞、并发队列的使用总结
    为什么阿里的程序员那么帅?---原来他们都有"编码规约扫描"神器在手
    多线程编程:多线程并发制单的开发记录【一】
    如何使用线程锁来提高多线程并发效率
    如何在分布式环境中同步solr索引库和缓存信息
    前端性能优化指南
    DOM操作方法、属性
    CSS样式手册
    XSS跨站脚本攻击
    数组和对象的使用方法
  • 原文地址:https://www.cnblogs.com/remly/p/4203237.html
Copyright © 2011-2022 走看看