c++简单加密系统设计

img

/*
 * @Author: jjk 
 * @Date: 2019-05-25 21:43:41 
 * @Last Modified by: jjk
 * @Last Modified time: 2019-05-25 22:05:28
 *  程序功能:
 *          字符串的加密和解密
 */

/*

思路:
    1、while无线循环,声明两个字符数组:保存明文和密文
    2、首次用户输入字符串,进行明文加密成密文操作
    3、之后用户输入命令字符实现判断:输入:“1”加密新的明文,输入:“2”对刚加密的密文进行解密,输入:“3”退出
*/

#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[])
{

    int count = 0;
    int result = 1;
    int i;
    char Text[128] = {'\0'};       // 定义一个明文字符数组
    char crytograph[128] = {'\0'}; // 定义一个密文字符数组
    while (1)
    {
        /* code */
        if (result == 1)
        {
            /* code */
            printf("请输入要加密的明文:");
            scanf("%s", &Text);
            count = strlen(Text);
            for (i = 0; i < count; i++) // 遍历明文
            {
                /* code */
                crytograph[i] = Text[i] + i + 5; //设置加密字符
            }
            crytograph[i] = '\0'; // 设置字符串结束标记
            /*输出密文信息*/
            printf("加密后的密文是:%s\n", crytograph);
        }
        else if (result == 2) // 如果是解密字符串
        {
            count = strlen(Text);
            for (i = 0; i < count; i++) // 遍历密文字符串
            {
                /* code */
                Text[i] = crytograph[i] - i - 5; // 设置解密字符
            }
            Text[i] = '\0';
            /*输出明文信息*/
            printf("解密后的明文是:%s\n", Text);
        }
        else if (result == 3)
        {
            break; // 跳出循环
        }
        else
        {
            printf("请输入命令符:"); // 输出字符串
        }

        /*输出字符串*/
        printf("输入1加密新的明文,输入2对刚加密的密文进行解密,输入3退出系统:\n");
        printf("请输入命令符:\n");
        scanf("%d", &result);
    }

    return 0;
}
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdexcept>
#include <sstream>

class Text
{
public:
    Text() {}
    Text(const std::string text) : _text(text) {}
    virtual ~Text() {}

    void load(const std::string &fileName)
    {
        std::ifstream file(fileName, std::ios_base::in | std::ios_base::binary);
        if (!file)
        {
            std::ostringstream ss;
            ss << "failed to open file " << fileName;
            throw std::runtime_error(ss.str());
        }
        file.seekg(0, std::ios::end);
        _text.reserve(file.tellg());
        file.seekg(0, std::ios::beg);
        _text.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
    }

    void save(const std::string &fileName)
    {
        std::ofstream file(fileName, std::ios_base::out | std::ios_base::binary);
        if (!file)
        {
            std::ostringstream ss;
            ss << "failed to open file " << fileName;
            throw std::runtime_error(ss.str());
        }
        file.write(_text.c_str(), _text.length());
    }

    virtual void print() const = 0;

protected:
    std::string _text;
};

class CypherText;

class PlainText : public Text
{
public:
    PlainText() {}
    PlainText(const std::string &text) : Text(text) {}

    CypherText operator+(const std::string &password) const;

    void print() const
    {
        std::cout << "The plain text is as follows:\n";
        std::cout << _text << std::endl;
    }
};

class CypherText : public Text
{
public:
    CypherText() {}
    CypherText(const std::string &text) : Text(text) {}

    PlainText operator-(const std::string &password) const;

    void print() const
    {
        std::cout << "The cypher text is as follows:\n";
        std::size_t n = _text.length();
        for (std::size_t i = 0; i < n; i++)
        {
            std::cout << std::hex << std::setw(2) << std::setfill('0') << std::uppercase << int(_text[i]) << ' ';
            if ((i + 1) % 16 == 0)
                std::cout << std::endl;
        }
        if (n % 16 != 0)
            std::cout << std::endl;
    }
};

CypherText PlainText::operator+(const std::string &password) const
{
    std::size_t n = _text.length();
    std::size_t m = password.length();
    std::string text;
    text.resize(n);
    for (std::size_t i = 0; i < n; i++)
        text[i] = _text[i] ^ password[i % m];
    return CypherText(text);
}

PlainText CypherText::operator-(const std::string &password) const
{
    std::size_t n = _text.length();
    std::size_t m = password.length();
    std::string text;
    text.resize(n);
    for (std::size_t i = 0; i < n; i++)
        text[i] = _text[i] ^ password[i % m];
    return PlainText(text);
}

int main()
{
    try
    {
        {
            PlainText pt;
            pt.load("test.txt");
            pt.print();
            CypherText ct;
            ct = pt + "thisiscode";
            ct.print();
            ct.save("test.txt.cyt");
        }
        {
            CypherText ct;
            ct.load("test.txt.cyt");
            ct.print();
            PlainText pt = ct - "thisiscode";
            pt.print();
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what();
    }
    return 0;
}