Hey guys the code takes in a string,converts it into a hex decimal string and puts it into an array (52 bytes) and is then encrypted in 16 byte fragments and then goes over all 52 and decrypts them. Now it only seems to encrypt and decrypt parts and leaves out parts. Please help Code: string sensorinfo= "0 1.345000 80.000000 100.000000 Temperature,Constant" ;
cout << sensorinfo;
stringstream str1;
for(int i=0;i<sensorinfo.length();i++){
str1 << std::hex << (int)sensorinfo.at(i);
}
cout << sensorinfo;
cout << str1.str() <<"\n";
int w,l;
//for each block of 2 characters
for(int i =0 ; i< length; i=i+2)
{
//copy 2 characters and a space to the output
output += temp.substr(i, 2) + " ";
}
// partition data into 16 byte blocks before encryption
int q;
std::istringstream hex_chars_stream(output);
std::vector<unsigned char> bytes;
unsigned char databytes[52]; // 52 data bytes
unsigned char fragmentbytes[16];
unsigned char encrypteddata[52];
unsigned int c;
while (hex_chars_stream >> std::hex >> c)
{
bytes.push_back(c);
}
cout << "The size of the vector is:" << bytes.size();
for(q=0;q<51;q++){
databytes[q] = bytes[q];
//cout << "databytes7 data: " << databytes[q];
}
for(q=0;q<52;q++){ // This loop doesn't work without the above one??
databytes[q] = bytes[q];
//cout << "databytes8 data: " << databytes[q];
}
cout << "Before encryption";
for(i=0;i<=51;i++)
{
cout << databytes[i];
}
int ff,gg,hh,ik;
for(hh=0;hh<16;hh++){
ff++;
fragmentbytes[hh] = databytes[ff];
}
cout << "The value of ff is:" << ff;
aes_encrypt(fragmentbytes,key);
int k;
cout << "After encryption";
for(k=0;k<=15;k++)
{ encrypteddata[k] = fragmentbytes[k];
trace() << hex << fragmentbytes[k];
}
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = 0;
}
while(ff < 52){
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = databytes[ff];
ff++; }
cout << "The value of ff is:" << ff;
ff-16; // to be able to put the same data from fragmentbytes into encrypteddata
aes_encrypt(fragmentbytes,key);
int k;
cout << "After encryption";
for(k=0;k<=15;k++)
{
cout << hex << fragmentbytes[k];
encrypteddata[ff] = fragmentbytes[k];
ff++;
}
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = 0;
}
}
//decryption
ff=0;
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = encrypteddata[ff];
ff++; }
cout << "The value of ff is:" << ff;
ff-16;
aes_decrypt(fragmentbytes,key);
int io;
cout << "After decryption";
for(io=0;io<=15;io++)
{
cout << hex << (int)fragmentbytes[io];
encrypteddata[ff] = fragmentbytes[io];
ff++;
}
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = 0;
}
while(ff < 52){
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = encrypteddata[ff];
ff++; }
cout << "The value of ff is:" << ff;
ff-16;
aes_decrypt(fragmentbytes,key);
int k;
cout << "After decryption";
for(k=0;k<=15;k++)
{
cout << hex << (int)fragmentbytes[k];
encrypteddata[ff] = fragmentbytes[k];
ff++;
}
for(hh=0;hh<16;hh++){
fragmentbytes[hh] = 0;
Didn't actually try running your code, but look at line, uh... look at where you type "ff-16;"
This statement does not change your program. You probably meant to do some sort of assignment here.
In C++, it's preferable and ensures clarity to declare the counter variable within the for loop itself when possible.
Bad:
1 2
int i;
for (i = 0; i < N; i++) { }
Good:
for (int i = 0; i < N; i++) { }
Your code is quite mind-boggling, the redundancy of it is quite error-prone, superfluous, unnecessary, and redundant. (For example, the use of your ff/hh/kk variables).
I do salute you for seeing that you could cut it to 2 lines. I struggle a lot trying to figure out what the intent is on things like this.
This is a big part of the learning process. When I first started I wrote like a 10 page parser for a handful of (fake) language / syntax commands and rules learning to write a compiler. I got it working but the whole mess was 10x or more the required effort and code. My professor... frowned upon it greatly, but it did work, so I got most of the credit.
Later I did better -- tasked to write a greedy solution to the traveling salesman problem in excel, I did it in 1 line as a formula + a simple button to sort the input and got to laugh at my classmates with their 3 sheets of macros.
Work smarter, not harder... everyone has to make peace with that one at some point.