CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    Aug 2018
    Posts
    5

    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

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
  •  





Click Here to Expand Forum to Full Width

Featured