Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 20th, 2007, 07:15 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Strange problem...

Greetings to all programming folks out there! ;)

I have a problem. When I try to use the data_decrypt function (described below), and when the data to be decrypted is larger then 14b, it don´t work.
The Data[0] gives a wrong value after the decrypt stage. I have no idea why this happens. I would be thankful if someone could help me.

(You can use the aes.h code freely but give credit where credit is due)

test.cpp
Code:
#include <iostream>
#include "aes.h"
using namespace std;
int main()
{
    int key[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int ekey[176];
    KeySchedule(key, 128, ekey);
    char data[16+15] = "ABCDEFGHIJKLMNOP";
    int lenght = 16;

    //for(int i = 0; i < lenght; i++)
    //   cout << data[i] << " ";
    //cout << endl  << endl;

    data_encrypt(data, lenght, ekey, 128);
    cout << endl;
    //for(int i = 0; i < lenght+15; i++)
    //   cout << data[i] << " ";
    //cout << endl  << endl;

    data_decrypt(data, lenght+15, ekey, 128);

    //for(int i = 0; i < lenght; i++)
    //   cout << data[i] << " ";
    //cout << endl  << endl;

    system("pause");
    return 0;
}

aes.h
Code:
//By: Mattias Festin
//E-mail: mattias.festin@tesseract-code.se
//Website: http://www.tesseract-code.se
using namespace std;
int KeySchedule(int *key,  int bit, int *keye);
int AddRoundKey(int *a, int *subkey);
int SubBytes(int *a,  bool inv);
int ShiftRows(int *a, int bit, bool inv);
int MixColumns(int *a, bool inv);

int Round(int *a, int *subkey, bool inv, int bit);
int encrypt(int *data, int *keye, int bit);
int decrypt(int *data, int *keye, int bit);

int data_encrypt(char *buf, int lenght, int *ekey, int bit);
int data_decrypt(char *buf, int lenght, int *ekey, int bit);

int BitRotate(int *a);

int mix_c(int *a, bool inv);
int gmul(int a, int b);
int gf_m(int a, int b);

int vlog[256] = {
0x00, 0xff, 0xc8, 0x08, 0x91, 0x10, 0xd0, 0x36, 
0x5a, 0x3e, 0xd8, 0x43, 0x99, 0x77, 0xfe, 0x18, 
0x23, 0x20, 0x07, 0x70, 0xa1, 0x6c, 0x0c, 0x7f, 
0x62, 0x8b, 0x40, 0x46, 0xc7, 0x4b, 0xe0, 0x0e, 
0xeb, 0x16, 0xe8, 0xad, 0xcf, 0xcd, 0x39, 0x53, 
0x6a, 0x27, 0x35, 0x93, 0xd4, 0x4e, 0x48, 0xc3, 
0x2b, 0x79, 0x54, 0x28, 0x09, 0x78, 0x0f, 0x21, 
0x90, 0x87, 0x14, 0x2a, 0xa9, 0x9c, 0xd6, 0x74, 
0xb4, 0x7c, 0xde, 0xed, 0xb1, 0x86, 0x76, 0xa4, 
0x98, 0xe2, 0x96, 0x8f, 0x02, 0x32, 0x1c, 0xc1, 
0x33, 0xee, 0xef, 0x81, 0xfd, 0x30, 0x5c, 0x13, 
0x9d, 0x29, 0x17, 0xc4, 0x11, 0x44, 0x8c, 0x80, 
0xf3, 0x73, 0x42, 0x1e, 0x1d, 0xb5, 0xf0, 0x12, 
0xd1, 0x5b, 0x41, 0xa2, 0xd7, 0x2c, 0xe9, 0xd5, 
0x59, 0xcb, 0x50, 0xa8, 0xdc, 0xfc, 0xf2, 0x56, 
0x72, 0xa6, 0x65, 0x2f, 0x9f, 0x9b, 0x3d, 0xba, 
0x7d, 0xc2, 0x45, 0x82, 0x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x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb};

int a_sbox[256] =   {
    //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, //0
    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, //1
    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, //2
    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, //3
    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, //4
    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, //5
    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, //6
    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, //7
    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, //8
    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, //9
    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, //A
    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, //B
    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, //C
    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, //D
    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, //E
    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; //F

int sbox[256] =   {
    //0     1    2      3     4    5     6     7      8    9     A      B    C     D     E     F
    0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, //0
    0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, //1
    0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, //2
    0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, //3
    0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, //4
    0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, //5
    0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, //6
    0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, //7
    0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, //8
    0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, //9
    0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, //A
    0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, //B
    0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, //C
    0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, //D
    0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, //E
    0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; //F


int Round(int *a, int *subkey, bool inv, int bit)
{
    if(inv == false)
    {
       AddRoundKey(a, subkey);
       SubBytes(a, inv);
       ShiftRows(a, bit, inv);
       MixColumns(a, inv);
    }
    if(inv == true)
    {
       MixColumns(a, inv);
       ShiftRows(a, bit, inv);
       SubBytes(a, inv);
       AddRoundKey(a, subkey);
    }
}

int encrypt(int *data, int *ekey, int bit)
{
    int c;
    if(bit == 128)
       c = 176;
    else
    {
        if(bit == 196)
           c = 208;
        else
           c = 240;
    }
    int  subkey[16], subkey2[16];

    for(int j = 0; j < c/16-2; j++)
    {
    for(int i = 0; i < 16; i++)
       subkey[i] = ekey[i+j*16];
    Round(data, subkey, false, bit);
    }
    for(int i = 0; i < 16; i++)
    {
       subkey[i] = ekey[i+c-33];
       subkey2[i] = ekey[i+c-17];
    }
    AddRoundKey(data, subkey);
    SubBytes(data, false);
    ShiftRows(data, bit, false);
    AddRoundKey(data, subkey2);
}

int decrypt(int *data, int *ekey, int bit)
{
    int c;
    if(bit == 128)
       c = 176;
    else
    {
        if(bit == 196)
           c = 208;
        else
           c = 240;
    }
    int subkey[16], subkey2[16];

    for(int i = 0; i < 16; i++)
    {
       subkey[i] = ekey[i+c-33];
       subkey2[i] = ekey[i+c-17];
    }

    AddRoundKey(data, subkey2);
    ShiftRows(data, bit, true);
    SubBytes(data, true);
    AddRoundKey(data, subkey);
    for(int j = c/16-3; j > -1; j--)
    {
    for(int i = 0; i < 16; i++)
       subkey[i] = ekey[i+j*16];
    Round(data, subkey, true, bit);
    }
}

//----------------------------------------------
int AddRoundKey(int *a, int *subkey)
{
    for(int i = 0; i < 16; i++)
       a[i] ^= subkey[i];
    return 0;
}

int KeySchedule(int *key,  int bit, int *keye)
{
    int c;
    int b;
    int t[4];
    int i = 1;

    if(bit == 128)
    {
       c = 16;
       b = 176;
       i = 1;
    }
    else
    {
       if(bit == 192)
       {
          c = 24;
          b = 208;
          i = 1;
       }
       else
       {
          c = 32;
          b = 240;
          i = 1;
       }    
    }
    int n = c;

    int ut[b];
    for(int j = 0; j < c; j++)
    {
       ut[j] = *key;
       key++;

       if(c-j < 4)
          t[c-j] = ut[j];
    }

       while(c < b)
       {
          for(int j = 0; j < 4; j++) 
             t[j] = ut[j + c - 4];
          if(c % n == 0)
          {
            BitRotate(t);
            for(int j = 0; j < 4; j++) 
                t[j] = sbox[t[j]];
            t[0] ^= Rcon[i];
            i++;
          }
          if(bit == 256)
          {
             if(c % 32 == 16)
             {
                for(int j = 0; j < 4; j++) 
                   t[j] = sbox[t[j]];
             }
          }
          for(int j = 0; j < 4; j++)
          {
             ut[c] = ut[c - n] ^ t[j];
             c++;
          }
        }

    for(int j = 0; j < b; j++)
    {
       keye[j] = ut[j];
    }
    return 0;
}


int SubBytes(int *a, bool inv)
{
    if(inv)
       for(int i = 0; i < 16; i++)
          a[i] = a_sbox[a[i]];
    else

       for(int i = 0; i < 16; i++)
          a[i] = sbox[a[i]];

    return 1;
}



int MixColumns(int *a, bool inv)
{  
    for(int j = 0; j < 4; j++)
    {
       int xb[4];
       for(int i = 0; i < 4; i++)
       {
          xb[i] = a[i*4+j];
       }
       mix_c(xb, inv);
       for(int i = 0; i < 4; i++)
       {
          a[i*4+j] = xb[i];
       }
    }
    return 1;
}

int ShiftRows(int *a, int bit, bool inv)
{
    int times;
    if(bit == 128)
    {
       times = 16;
    }
    else
    {
        if(bit == 192)
        {
           times = 24;
        }
        else
           times = 32;
    }

    int xb[times];
    for(int i = 0; i < times; i++)
       xb[i] = a[i];

    if(inv)
    {
    if(bit == 128)
    {
       a[5] = xb[4];
       a[6] = xb[5];
       a[7] = xb[6];
       a[4] = xb[7];

       a[10] = xb[8];
       a[11] = xb[9];
       a[8] = xb[10];
       a[9] = xb[11];

       a[15] = xb[12];
       a[12] = xb[13];
       a[13] = xb[14];
       a[14] = xb[15];
    }

    if(bit == 192)
    {
       a[7] = xb[6];
       a[8] = xb[7];
       a[9] = xb[8];
       a[10] = xb[9];
       a[11] = xb[10];
       a[6] = xb[11];

       a[14] = xb[12];
       a[15] = xb[13];
       a[16] = xb[14];
       a[17] = xb[15];
       a[12] = xb[16];
       a[13] = xb[17];

       a[21] = xb[18];
       a[22] = xb[19];
       a[23] = xb[20];
       a[18] = xb[21];
       a[19] = xb[22];
       a[20] = xb[23];
    }

    if(bit == 256)
    {
       a[9] = xb[8];
       a[10] = xb[9];
       a[11] = xb[10];
       a[12] = xb[11];
       a[13] = xb[12];
       a[14] = xb[13];
       a[15] = xb[14];
       a[8] = xb[15];


       a[19] = xb[16];
       a[20] = xb[17];
       a[21] = xb[18];
       a[22] = xb[19];
       a[23] = xb[20];
       a[16] = xb[21];
       a[17] = xb[22];
       a[18] = xb[23];

       a[28] = xb[24];
       a[29] = xb[25];
       a[30] = xb[26];
       a[31] = xb[27];
       a[24] = xb[28];
       a[25] = xb[29];
       a[26] = xb[30];
       a[27] = xb[31];
    }
    }
    else
    {
    if(bit == 128)
    {
       a[4] = xb[5];
       a[5] = xb[6];
       a[6] = xb[7];
       a[7] = xb[4];

       a[8] = xb[10];
       a[9] = xb[11];
       a[10] = xb[8];
       a[11] = xb[9];

       a[12] = xb[15];
       a[13] = xb[12];
       a[14] = xb[13];
       a[15] = xb[14];
    }

    if(bit == 192)
    {
       a[6] = xb[7];
       a[7] = xb[8];
       a[8] = xb[9];
       a[9] = xb[10];
       a[10] = xb[11];
       a[11] = xb[6];

       a[12] = xb[14];
       a[13] = xb[15];
       a[14] = xb[16];
       a[15] = xb[17];
       a[16] = xb[12];
       a[17] = xb[13];

       a[18] = xb[21];
       a[19] = xb[22];
       a[20] = xb[23];
       a[21] = xb[18];
       a[22] = xb[19];
       a[23] = xb[20];
    }

    if(bit == 256)
    {
       a[8] = xb[9];
       a[9] = xb[10];
       a[10] = xb[11];
       a[11] = xb[12];
       a[12] = xb[13];
       a[13] = xb[14];
       a[14] = xb[15];
       a[15] = xb[8];


       a[16] = xb[19];
       a[17] = xb[20];
       a[18] = xb[21];
       a[19] = xb[22];
       a[20] = xb[23];
       a[21] = xb[16];
       a[22] = xb[17];
       a[23] = xb[18];

       a[24] = xb[28];
       a[25] = xb[29];
       a[26] = xb[30];
       a[27] = xb[31];
       a[28] = xb[24];
       a[29] = xb[25];
       a[30] = xb[26];
       a[31] = xb[27];
    }
    }

    return 1;
}

//--------------------------------------------

int BitRotate(int *a)
{
    int temp[4];
    for(int i = 0; i < 4; i++)
       temp[i] = a[i];

    for(int i = 1; i < 5; i++)
       a[i] = temp[i%4];
}

int mix_c(int *a, bool inv)
{
    int r[4];
    int b[4];
    int h;

    if(inv == false)
    {
          for(int i = 0; i < 4; i++)
          {
             h = a[i] & 0x80;
             b[i] = a[i] << 1;
             if(h == 0x80) 
                b[i] ^= 0x1b;
          }

          r[0] = (b[0] ^ a[3] ^ a[2] ^ b[1] ^ a[1])%256;
          r[1] = (b[1] ^ a[0] ^ a[3] ^ b[2] ^ a[2])%256;
          r[2] = (b[2] ^ a[1] ^ a[0] ^ b[3] ^ a[3])%256;
          r[3] = (b[3] ^ a[2] ^ a[1] ^ b[0] ^ a[0])%256;
    }
    else
    {
        r[0] = (gmul(a[0], 14) ^ gmul(a[3], 9) ^ gmul(a[2], 13) ^ gmul(a[1], 11))%256;
        r[1] = (gmul(a[1], 14) ^ gmul(a[0], 9) ^ gmul(a[3], 13) ^ gmul(a[2], 11))%256;
        r[2] = (gmul(a[2], 14) ^ gmul(a[1], 9) ^ gmul(a[0], 13) ^ gmul(a[3], 11))%256;
        r[3] = (gmul(a[3], 14) ^ gmul(a[2], 9) ^ gmul(a[1], 13) ^ gmul(a[0], 11))%256;
    }

    for(int i = 0; i < 4; i++)
       a[i] = r[i];
    return 1;
}

int gmul(int a, int b)
{
       if(a==0 || b==0)
          return 0;
       else
       {
          if((vlog[a]+vlog[b])>255)
          {
             return alog[(vlog[a]+vlog[b])%256+1];
          }
          else
             return alog[(vlog[a]+vlog[b])];
       }
}

//--------------------------------------------

//DATA ENCRYPTION/DECRYPTION

int data_encrypt(char *buf, int lenght, int *ekey, int bit)
{
   int data[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
   for(int j = 0; j < lenght; j++)
   {
       for(int i = 0; i < 15; i++)
          data[i] = data[i+1];
       data[15] = (int)buf[j];
       if(data[15] < 0)
          data[15] += 256;

       for(int i = 0; i < 16; i++)
          cout << data[i] << " ";
       cout << endl;

       encrypt(data, ekey, 128);

       for(int i = 0; i < 16; i++)
          cout << data[i] << " ";
       cout << endl;

       if(data[0] > 127)
          data[0] -= 256;
       buf[j] = (char)data[0];
   }
   for(int j = 1; j < 16; j++)
   {
       if(data[j] > 127)
          data[j] -= 256;
       buf[lenght+2+j] = data[j];
   }
   return 0;
}

int data_decrypt(char *buf, int lenght, int *ekey, int bit)
{
   int data[16];
   for(int i = 0; i < 16; i++)
   {
      data[i] = (int)buf[i+lenght-12];
      if(data[i] < 0)
         data[i] += 256;
   }
   for(int j = lenght-16; j >= 0; j--)
   {
      for(int i = 14; i >= 0; i--)
         data[i+1] = data[i];
      data[0] = (int)buf[j];
      if(data[0] < 0)
         data[0] += 256;

      for(int i = 0; i < 16; i++)
          cout << data[i] << " ";
       cout << endl;

      decrypt(data, ekey, 128);

      for(int i = 0; i < 16; i++)
          cout << data[i] << " ";
      cout << endl;

     if(data[15] > 127)
          data[15] -= 256;
      buf[j] = (char)data[15];
   }
    return 0;
}
Reply With Quote
  #2 (permalink)  
Old October 25th, 2007, 03:56 PM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have no idea what the problem was. But it is solved;)

There was no error in the code, but it seems that the compiler were to blame.:(

So use this code as you please...

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange IE problem sully7 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 September 19th, 2008 08:44 AM
Strange ListBox Problem mirandaym ASP.NET 2.0 Basics 1 August 2nd, 2006 05:00 AM
strange problem with ASP dhx10000 BOOK: Beginning ASP 3.0 0 June 12th, 2006 05:10 PM
kind of Really strange problem kalchev ASP.NET 2.0 Basics 1 March 22nd, 2006 03:32 PM
very strange problem! please help! raybristol ASP.NET 1.0 and 1.1 Basics 8 December 15th, 2005 06:46 AM



All times are GMT -4. The time now is 05:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.