Caesar密码程序在c ++ [英] Caesar cipher program in c++

查看:136
本文介绍了Caesar密码程序在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在c ++中写一个caesar密码程序。我使用四个函数,一个用于选择移位键,两个用于加密和解密,最后一个用于实现凯撒密码,使用输入文件读取文本,并将加密或解密的文本输出到输出文件中。我试图运行代码,它正在崩溃。我认为问题是凯撒功能内的某处。但我不知道究竟是什么。有任何想法吗?这是我的代码:

I am trying to write a caesar cipher program in c++. I use four functions, one for choosing shift key , two for encryption and decryption and the last is for implement the caesar cipher, using an inputfile for reading the text and an ouput the encrypted or the decrypted text into the output file. I am trying to run the code and it is being crashed. I think the problem is somewhere inside Caesar function. But i don't know what exactly is. Any ideas? This is my code:

#include <iostream>
#include <fstream>
using namespace std;

int chooseKey()
{
    int key_number;
    cout << "Give a number from 1-26: ";
    cin >> key_number;

    while(key_number<1 || key_number>26)
    {
        cout << "Your number have to be from 1-26.Retry: ";
        cin >> key_number;  

    }
    return key_number;
}

void encryption(char *w, char *e, int key){

    char *ptemp = w;


    while(*ptemp){

        if(isalpha(*ptemp)){

        if(*ptemp>='a'&&*ptemp<='z')  
         {  
             *ptemp-='a';  
             *ptemp+=key;  
             *ptemp%=26;  
             *ptemp+='A';  
         }  


        }
        ptemp++;
    }

    w=e;

}

void decryption (char *e, char *w, int key){
    char *ptemp = e;


    while(*ptemp){

        if(isalpha(*ptemp))
        {

            if(*ptemp>='A'&&*ptemp<='Z')  
            {  
             *ptemp-='A';  
             *ptemp+=26-key;  
             *ptemp%=26;  
             *ptemp+='a';  
            }  


        }
        ptemp++;
    }

    e=w;

}

void Caesar (char *inputFile, char *outputFile, int key, int mode)
{
     ifstream input;
     ofstream output;
     char buf, buf1;  
     input.open(inputFile);  
     output.open(outputFile);  
     buf=input.get();
     while(!input.eof())  
     {  
        if(mode == 1){
            encryption(&buf, &buf1, key);
        }else{
            decryption(&buf1, &buf, key);
        }
        output << buf;
        buf=input.get();
     }
     input.close();  
     output.close();
}

int main(){

    int key, mode;
    key = chooseKey();
    cout << "1 or 0: ";
    cin >> mode;
    Caesar("test.txt","coded.txt",key,mode);
    system("pause");
}






我的程序。我的代码模板是我的帖子的顶部。


Here are some details for my program.The template of my code is on top of my post.

参数 w是开头的文字,par e 是解密的文字。加密正在进行,如果我们在w *文本中添加shift键,我们得到* e。相反的过程使解密功能,我有加密文本(* e)和我想取得解密的文本(* w),从我从chooseKey函数取的键移位。在Caesar函数中,我使用一个包含一些数据的输入文件,我想在输出文件中提取加密或解密的文本。我已经实现加密和解密函数,我的问题是如何使用它们在Caesar函数。我不采取任何理想的结果,我的程序是崩溃。

The parameter w is the text in the beginning and the par e is the decrypted text. The encryption is being made, if we add the shift key in the w* text and we get the *e. The reverse procedure makes the decryption function, i have the encrypted text(*e) and i want to take the the decrypted text(*w), subtracking the key shift which i take from chooseKey function. In the Caesar function, i use an input file which contain some data and i want to extract in the output file the encrypted or the decrypted text instead. I have already implement encryption and decryption functions and my problem is how to use them in the Caesar function.I don't take any desirable result, my program is being crashed. Please mention my mistakes.Any solution?

推荐答案

您将单个字符的地址传递给解密

You pass the address of a single character into decryption

buf=input.get();
 // ...
        encryption(&buf, &buf1, key);

然后将其视为指向一个以null结尾的字符串。

You then treat it as if it points to a null-terminated string.

while(*ptemp){
    // ...
    ptemp++;
}

这不会很好,因为 ptemp 不指向一个以null结尾的字符串。现在你做 ptemp ++ 你在未定义的行为土地。

That's not going to work well, because ptemp doesn't point to a null-terminated string in the first place. The moment you do ptemp++ you're in Undefined Behaviour land.

除此之外还有一个其他陷阱(不要循环 .eof(),eg)和潜在的改进。

Apart from this there are a host of other pitfalls (don't loop on .eof(), e.g.) and potential improvements.

哦,和作业

w = e;

e = w;

没有任何效果(您将指针值赋给局部函数参数;

have no effect whatsoever (you're assigning pointer values to local function arguments; upon returning, these variables don't even exist anymore).

这里是一个清理过的,我会期望做你想要的: Live on coliru

Here's a cleaned up take, that I'd expect does what you want: Live on coliru

#include <iostream>
#include <fstream>
using namespace std;

int chooseKey() {
    cout << "Give a number from 1-26: ";
    int key_number;
    while((!(cin >> key_number)) || (key_number < 1 || key_number > 26)) {
        cout << "Your number have to be from 1-26 (" << key_number << "). Retry: ";
        cin.clear();
    }
    return key_number-1;
}

template <typename InIt, typename OutIt>
void encryption(InIt f, InIt l, OutIt out, int key) {
    for (InIt ptemp = f; ptemp != l; ++ptemp)
    {
        if(isalpha(*ptemp))
        {
            char base = islower(*ptemp)? 'a' : 'A';
            *out++ = base + (*ptemp - base + key) % 26;
        } else
            *out++ = *ptemp;
    }
}

void Caesar(const char *inputFile, const char *outputFile, int key, int mode) {
    ifstream input(inputFile);
    ofstream output(outputFile);

    istreambuf_iterator<char> init(input), end;
    ostreambuf_iterator<char> outit(output);

    encryption(init, end, outit, mode? key : 26-key);
}

int main() {
    int key, mode;
    key = chooseKey();
    cout << "1 or 0: ";
    cin >> mode;
    Caesar("test.cpp", "coded.txt", key, mode);
}

这篇关于Caesar密码程序在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆