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");
    
    }
  • 相关阅读:
    docker指令汇总
    springboot(八) 嵌入式Servlet容器自动配置原理和容器启动原理
    RabbitMQ 消息确认机制
    RabbitMQ 最常用的三大模式
    RabbitMQ 核心概念
    RabbitMQ 之简单队列
    Spring 详解(三)------- SpringMVC拦截器使用
    slf4j 搭配 log4j2 处理日志
    Spring 详解(二)------- AOP关键概念以及两种实现方式
    Spring 详解(一)------- AOP前序
  • 原文地址:https://www.cnblogs.com/remly/p/4203237.html
Copyright © 2011-2022 走看看