我该怎么做才能导致这个程序的输出不一致?

时间:2011-12-20 21:28:41

标签: c++ encryption bit-manipulation xor bitwise-xor

所以我制作了一个基本的xor加密程序,要求输入密码字符串并用它来播种ISAAC

然后它要求加密字符串,并使用ISAAC的输出xor。 由于某种原因,这对于相同的两个字符串产生不定的结果。 这是我的代码的问题,还是它被输出到控制台? 我做了什么让它明显变得明显吗? 这是我的代码,我正在使用ISAAC网站上提供的模板类:

#include <iostream>
#include <cstdlib>
using std::malloc;
using std::free;
#include "isaac.hpp"
#include <time.h>
#include <cmath>
#include <string>
#include <sstream>

using namespace std;

QTIsaac<8,int> derp;



char trand(){
    int rando=0;
    rando=derp.rand();
    rando=abs(rando);
    rando=rando%256;
    char re=rando;
    return re;
}

int main()
{
    cout << "What is your password string?\n";
    string get;
    getline(cin,get);
    int length=get.size();
    int seedWithMe[length];
    for(int i=0;i<length;i++){
        seedWithMe[i]=(int)get[i];
    }
    derp.srand(1,1,1,seedWithMe);
    cout << "What is your string to be encrypted? \n";
    getline(cin,get);
    length=get.size();
    char table[length];
    char table3[length];
    for(int i=0;i<length;i++){
        table[i]=trand();
    }
    for(int i=0;i<length;i++){
        table3[i]=(char)table[i]^(char)get[i];
    }

    cout << table3 << "\n";



    return 0;
}

编辑:没关系,所有这一切只是我愚蠢

  

所以我尝试了大卫施瓦茨给我的修复,但是当我尝试时   哈希密码,它再次退出工作。我正在使用的功能   hash输入字符串是:

string shaOfPass(string digestThis){
  string digest;
  CryptoPP::SHA256 hash;
     
 CryptoPP::StringSource foo(digestThis, true,
   new CryptoPP::HashFilter(hash,
      new CryptoPP::HexEncoder (
         new CryptoPP::StringSink(digest))));
  return digest;
} And this code results in inconstant output:
  
cout << "What is your password string?\n";
  string get;
  getline(cin,get);
  string hashOfPass=shaOfPass(get);
  int seedWithMe[256];
  for(int i=0;i<256;i++){
      seedWithMe[i]=(int)get[i%256];
  }

1 个答案:

答案 0 :(得分:1)

{1}}函数在给定指针时,要求指针包含至少2 ^ N个字节的数据(您将N设置为8)。因此,当您的字符串长度小于256个字符时,您将播放一些随机的垃圾数据。

这是一个修复:

srand