Thread: AES encryption and decryption not going over whole array

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    5

    AES encryption and decryption not going over whole array

    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:
    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];
      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;
    
      }
    }
    Last edited by Salem; 08-02-2018 at 04:52 AM. Reason: Removed train-wreck tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you tried running the code in a debugger?
    Now is a good time to start practising.

    In particular, you might want to breakpoint on ff - 16; and decide if it does what you think it does.

    Finally, next time you post, make sure you either 'copy as text' or 'paste as text'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your code is massively over-complicated.
    You didn't show the aes_encrypt or aes_decrypt function so I just made a simple one as an example (just adds the "key", which I set to 1).
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    using namespace std;
     
    void print16(const unsigned char *buf) {
        cout << hex << setfill('0');
        for (size_t i = 0; i < 16; i++)
            cout << setw(2) << (int)buf[i] << ' ';
        cout << '\n';
    }
     
    void aes_encrypt(unsigned char *buf, int key) {
        for (size_t i = 0; i < 16; i++)
            buf[i] += key;
    }
     
    int main() {
        string info = "0 1.345000 80.000000 100.000000 Temperature,Constant";
     
        vector<unsigned char> bytes(info.begin(), info.end());
     
        // Ensure bytes size is a multiple of 16.
        bytes.resize(bytes.size() + (16-bytes.size()%16)%16, ' ');
     
        int key = 1;
        for (size_t i = 0; i < bytes.size(); i += 16) {
            aes_encrypt(bytes.data() + i, key);
            print16(bytes.data() + i);
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption and Decryption
    By dbz8gokugohan in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 07:40 AM
  2. encryption / decryption
    By ghatoz in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2010, 06:45 AM
  3. Encryption/Decryption Help
    By coolage in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2008, 01:53 AM
  4. Ask about Encryption and Decryption
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 12:55 AM
  5. Encryption/Decryption -- Correction
    By Abdi in forum C++ Programming
    Replies: 0
    Last Post: 04-28-2002, 08:00 AM

Tags for this Thread