|
-
August 2nd, 2018, 05:02 AM
#1
AES encryption and decryption not going over whole array
***** 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:
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;
const std::string tmp = str1.str();
std::string temp;
for(int w=0;w<tmp.length();w++){
temp += tmp.at(w);
}
int length = temp.size();
string output = "";
//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];
cout << 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;
}
}
Last edited by 2kaud; August 2nd, 2018 at 05:58 AM.
Reason: Added code tags, changed opening comment
-
August 2nd, 2018, 05:53 AM
#2
Re: AES encryption and decryption not going over whole array
[When posting code, please use code tags so that the code is readable. Go advanced, select the formatted code and click '#'. Also when referring to readers of these forums, please note that there are at least two sexes].
Cheers!
Note that this code has no effect as the result of subtracting 16 from ff is not used.
Last edited by 2kaud; August 2nd, 2018 at 05:59 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|