int main(){
double one, zero, bias;
double max = -2.0;
string plaintext, ciphertext, firstFourStr, lastFourStr;
bitset<16> V;
bitset<16> base("0000110000001011");
bitset<16> targetKey;
for(int k=0; k<16; k++){
ifstream plaintexts ("plaintexts.txt");
ifstream ciphertexts ("ciphertext07.txt");
bitset<12> tmpBs(k);
bitset<16> add(tmpBs.to_string()+"0000");
bitset<16> guessedKey1(add^base);
for(int j=0; j<16; j++){
bitset<4> tmpBs2(j);
bitset<16> add2(tmpBs2.to_string()+"000000000000");
bitset<16> guessedKey(guessedKey1^add2);
one = zero = 0.0;
for(int i=0; i<20000; i++){
getline(plaintexts, plaintext);
getline(ciphertexts, ciphertext);
bitset<16> pTxt(plaintext);
bitset<16> cTxt(ciphertext);
V = guessedKey^cTxt;
bitset<4> firstFour((V.to_string()).substr(0,4));
bitset<4> secondFour((V.to_string()).substr(4,4));
bitset<4> thirdFour((V.to_string()).substr(8,4));
bitset<4> lastFour((V.to_string()).substr(12,4));
bitset<16> U(sBoxInverse(firstFour)+sBoxInverse(secondFour)+sBoxInverse(thirdFour)+sBoxInverse(lastFour));
if((U[2]^U[6]^U[10]^U[14]^pTxt[4]^pTxt[7]^pTxt[12]^pTxt[15]) == 0) zero++;
else one++;
}
plaintexts.close();
ciphertexts.close();
bias = zero/(zero+one)-0.5;
if(bias < 0) bias *= -1;
if(max <= bias){
max = bias;
targetKey = guessedKey;
}
cout << bias << endl;
}
}
cout << targetKey << ": " << max << endl;
}
I was doing my cryptography assignment and this is the program I wrote to get the key. But it runs pretty slow. I tried to replace some lines of code with simpler ones to check if operations like to_string() or set() caused the problem but it seems that they have nothing to do with the problem. So what causes the program to run slow?
16 x 16 x 20.000 x 2 = 10.240.000
calls to getline.