为什么我的简单XOR加密程序不能正确翻译字符,为什么在结尾添加更多字符? [英] Why doesn't my simple XOR encryption program translate the characters right, and why does it add more characters at the end?

查看:132
本文介绍了为什么我的简单XOR加密程序不能正确翻译字符,为什么在结尾添加更多字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个基于XOR的en / decryptor,这样工作。
您有一个纯文本字符,例如3和一个用户键,例如5.
写成位:

  3 = 00000011 

5 = 00000101

我们做XOR操作,我们得到6:

  6 = 00000110 

这可以通过说6 XOR 5,它是3来反转。



。但它是真的bug,它不翻译文本正确,它添加了很多字符在文件的末尾,根据你使用的键。

  using namespace std; 
int main(int argc,char * argv [])
{
char buffer [5001];
ifstream fin(a.txt,ifstream :: in);
ofstream fout(b.txt);
int key;
char znak;

// console
cout<< Key:;
cin>>键;
fin.get(znak);
while(!fin.eof()&& znak!='')
{
fin.get(buffer,sizeof(buffer));
}

for(int i = 0; i {
fout<函数(key,buffer [i]);
}

cout<< done<< endl;

cin.get();
return 0;
}

char函数(int键,char输入)
{
返回输入^键;
}

为什么程序不能翻译文本?为什么要在文件末尾添加字符?

解决方案

  while(!fin.eof()&& znak !='')

  fin.get(buffer,sizeof (缓冲)); 

当您这样做时,请注意不会读取 sizeof(buffer)个字符 - 只是它不会再读取任何。你通常在文件的结尾有较少的,当没有那么多的字符留下来阅读。

  for(int i = 0; i   



这里,当你尝试处理 sizeof(buffer)字符,机会是非常好的(特别是在最后一次迭代),你试图处理更多的字符比你实际阅读。您可以使用 gcount 检索您读取的数字,但我不确定我是否真的建议使用它。



个人而言,我可能会这样做:

 类函数{
char key;
public:
function(char k):key(k){}
char operator()(char input){return key ^ input; }
};

int main(){
std :: ifstream fin(a.txt);
std :: ofstream fout(b.txt);

fin.noskipws();

std :: transform(std :: istream_iterator< char>(fin),
std :: istream_iterator< char>(),
std :: ostream_iterator< char> fout),
function(key));
return 0;
}


I'm making a XOR based en/decryptor, that works like this. You have a plaintext character, for example 3, and a user key, for example 5. Written in bits:

3 = 00000011

5 = 00000101

Now if we do XOR operation, we get 6:

6 = 00000110

This can be reversed by saying 6 XOR 5, which is 3.

So I have made this program. But it's really buggy, it doesn't translate the text right, and it adds a lot of characters in the end of the file, depending which key you are using.

using namespace std;
int main(int argc, char *argv[])
{
    char buffer[5001]; 
    ifstream fin("a.txt", ifstream::in);
    ofstream fout("b.txt");
    int key;
    char znak;

    // console    
    cout << "Key: ";
    cin >> key;
    fin.get(znak);
    while(!fin.eof() && znak != ' ')
    {
       fin.get(buffer, sizeof(buffer));     
    }

    for(int i = 0; i < sizeof(buffer); i++)
    {
       fout << function(key, buffer[i]);
    }

    cout << "done" << endl;

    cin.get();
    return 0;
}

char function(int key,char input)
{
    return input ^ key;
}

Why doesn't the program translate the text right? And Why does it add characters to the end of the file?

解决方案

From the looks of things, the encryption has almost nothing to do with anything (or at least with the problem) here.

while(!fin.eof() && znak != ' ')

A loop of the form while (!whatever.eof()) is pretty much guaranteed to work incorrectly.

    fin.get(buffer, sizeof(buffer)); 

When you do this, note that it does not guarantee that it will read sizeof(buffer) characters -- only that it won't read any more than that. You typically get fewer at the end of the file, when there simply aren't that many characters left to read. [Edit: I should also mention that you can read fewer in other places as well -- e.g., if you're reading from a network connection, it's fairly common to receive a partial buffer, but more later when it arrives.]

for(int i = 0; i < sizeof(buffer); i++)

So here, when you attempt to process sizeof(buffer) characters, chances are pretty good that (especially on the last iteration) you're attempting to process more characters than you actually read. You can retrieve the number you read with gcount, though I'm not sure I'd really recommend using it.

Personally, I'd probably do something like this:

class function { 
    char key;
public:
    function(char k) : key(k) { }
    char operator()(char input) { return key ^ input; }
};

int main() { 
    std::ifstream fin("a.txt");
    std::ofstream fout("b.txt");

    fin.noskipws();

    std::transform(std::istream_iterator<char>(fin),
                   std::istream_iterator<char>(),
                   std::ostream_iterator<char>(fout),
                   function(key));
    return 0;
}

这篇关于为什么我的简单XOR加密程序不能正确翻译字符,为什么在结尾添加更多字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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