Hello,
I am able to locate the error... The code

while(myfile1.read( (char *)buff, 16 ))

is reading from the file plaintext.txt in chunks of 16 bytes... but there are remaining last 3 bytes left therefore the code is not reading the remaining 3 last bytes for encryption that is why it is not displaying the last 3 bytes when decrypted...
So therefore I need to do a padding so that it can take the last 3 bytes + 13 spaces...
Can anyone help how we can do it...

Hello,
Instead of using another program to grab the pixel values of the image and save it in a file ciphertxt.txt and then using AES to encrypt and decrypt it.... I opened the image directly in binary...
It is encrypting and decrypting the image correctly but it is not displaying the encrypted image
How can we display the encrypted image...

unsigned char buff[4][4];
ifstream myfile1 ("sel.jpg", ios::binary);
ofstream myfile3  ("encrypt.jpg", ios::binary);

while(myfile1.read( (char *)buff, 16 ))
{	  
	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = buff[j][i]; // plaintext
           }
Encrypt(a, rk);

if (myfile3.is_open())
	  {
		  for(j=0; j< BC; j ++)
			  for ( i=0; i<4; i++)
				  myfile3<<a[i][j];		  
	  }   
	  else        
		cout << "Unable to open file";
}	
myfile3.close();
	myfile1.close();

unsigned char buffer[4][4];
ifstream myfile4;
		myfile4.open( "encrypt.jpg", ios::binary);
		if( !myfile4 )
		{
      cout << "error opening file, quitting" << endl;
      return -1;
		}
	ofstream myfile5 ("test.jpg", ios::binary);

while(myfile4.read( (char *)buffer, 16 ))
  {
  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {		  
		  a[i][j] = buffer[j][i]; // plaintext
}
Decrypt(a, rk);
if (myfile5.is_open())
		{
		    for(j=0; j<BC; j ++)
			for ( i=0; i<4; i++)
			myfile5<<a[i][j];
		}   
		  else        
			  cout << "Unable to open file";
  }				
	 
  	myfile5.close();
	myfile4.close();

The image is correctly decrypted but i am unable to display the encrypted image...
How can we display it...

Hello,
I am posting the code for encrypting the image as a zip file...
The code is able to encrypt/decrypt the image correctly...
But it is not able to display the encrypted image....
Can anyone help, how we can display the encrypted image...

>>how we can display the encrypted image
You can't, and that's the whole purpose of entrypting it :) -- the message has to be decrypted first before you can display it so that it makes sense.

I think what he means is that he wants to display the ciphertext as an image. Could the ciphertxt (in binary is it?) be displayed in an image format to show whatever garbage therein?

That would help me too.

Thanks,

Sundeep

Hello,
I am writing the code for padding....
The main idea is to get the file size..
If the size is not a multiple of 16 then the space is padded till it is a multiple of 16
The code is:-

long begin, end;
begin = myfile1.tellg();
myfile1.seekg(0, ios::beg);
end = myfile1.tellg();
cout<<end;

while(((end)%16)!==0))
{
     char pad=" ";
     myfile1<<pad;
}

Problem i can't get the file size... it is 0..
If i change

myfile1.seekg(0, ios::end);

I am getting the file size correct but then the code is not able to read from the file in chunks of 16 bytes since it is pointed @ the end of file...
Can anyone help how we can solve this problem...

To get back to the beginning of the file for reading, after finding size:

myfile1.seekg( 0, ios::beg );

Your padding code is a bit odd. Do you really want to modify the source file, or simply pad out the data that you're encrypting?
How about something like:

//after reading all the data in, do padding
int pad_num = 16 - (end % 16) ;  //find how many to pad
char pad = " ";
for( i = 0; i < pad_num; i++ )
{
     arr[r][c] = pad;  //assuming you're trying to fill the array out
}

Where I think this will still cause you problems is that when the encrypted file is later decrypted, how do you know that last (up to) 15 bytes might be padding and not part of the original data? To simply include those bytes in the final output might give a corrupted image.

As to displaying the encrypted image - why? You can do a dump of the file contents, byte value by byte value, but that has no meaning. You cannot use an image display program, as almost all image formats include some header data to describe the image format type and image parameters, which is now encrypted.

:)

Displaying the image is real easy if it is not encrypted ...

You can use glut + corona to easily display images.
http://www.daniweb.com/forums/thread63827.html#5

Also if you are having problems with leftover bytes at the end of the file smaller than your buffer, use the following bit of code to find the file size

// obtaining file size
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("example.txt");
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  myfile.close();
  cout << "size is: " << (end-begin) << " bytes.\n";
  return 0;
}

http://www.cplusplus.com/doc/tutorial/files.html

Divide the file size by your buffer size, run the loop with the number you get (the quotient) and read the remaining data using the remainder as the read size.

^^


Also, it possible to display encrypted data, just that it would look like a bunch of random pixels .... The standard format is RGB, so you can create a single color out of 3 bytes of data, 0-255, standard char. Read up an OpenGL tutorial on creating a texture ......

Hello,
I need to show the encrypted image.. and using a software available freely on the internet i have to draw the histogram of the original and encrypted image...
Therefore to be able to draw the histogram i need to display the encrypted image..
I am sending a paper on aes image encryption... it has displayed the encrypted image and drawn the histogram for both original and encrypted image..
Can anyone help how we can display the encrypted image using either another language, software or whatever provided it has displayed the encrypted image...

Well, now it gets more interesting. What type of image file are you using? Do you have to do the display of original and encrypted files from within your program, or simply be able to display them with some other standalone app?

Pretty much all formats have some descriptive header (image size, color depth, format version....) that must be preserved if you're to show the encrypted image. You cannot simply encrypt an entire .jpg file and expect to be able to display it in a view that handles jpg's - as the header information is also encrypted.

commented: Very good instructor and very helpful always take the opponent in the right direction +1

Hello Vmanes,
Thanks for the reply..
Hmm I have to display the encrypted image either using the AES program itself or using any applications that is able to display the encrypted images..
As far the AES program is not able to display the encrypted image... So any other application software or any other language that i can use to display the encrypted image...
So the encrypted image can be displayed using any standalone applications.. so that i can draw the histograms for the original and encrypted image... and compare the results... using the histograms..

If we have to display the encrypted image how can we preserve the image header information
before it is encrypted using the AES..
The AES program can encrypt any image file .bmp,.jpg,.gif,.png anyway any image format that is encrypted and is able to be displayed as encrypted will help me...
That is using any image format and i am able to display the encrypted image..
Ok so far i have encrypted an entire .jpg file and expect to be able to display it as encrypted which is not possible... as the header information is also encrypted.
So how can we encrypt the image so that it is able to be displayed as encrypted..
I can use any other standalone software or any other language to display the original, encrypted and decrypted image...

Well, you've now changed the scope of the problem. Before, you were just encrypting a file - any file. If you want to encrypt just the pixel bytes of an image file, your program will have to determine how big the header portion is, save that in the clear, then encrypt the pixel bytes. In this sense, the enciphered file is still a jpg or gif or bmp, but with an unintelligible image. But, knowing what type image it is, and the parameters (height and width, color depth, etc.) may provide assistance to the would-be intruder.

Can you give the exact parameters of the assignment?

Ok I want to encrypt just the pixel bytes of an image file, that is the program will have to determine how big the header portion is, save that in the clear, then encrypt the pixel bytes.
The image can be of any format... provided i am able to display it in an encrypted form...

Hello,

I am able to display the encrypted image..
I have used a .bmp image 24 bits for encryption...

The 24 bits bmp image contains 14 bytes of header + 40 bytes of info header that is 54 bytes of header info in total before the image data...

So i have saved the first 54 bytes in a buffer before encryption and saved it to encrypt.bmp then the encrypted data is then saved to the encrypt.bmp image after the 1st 54 bytes..

Therefore the image is display as encrypted...

For more info on bmp files:-
http://local.wasp.uwa.edu.au/~pbourke/dataformats/bmp/

Now I have just 1 small problem left with:- the problem of padding for reading the data that contains <16 bytes from a file/image..
I have used 200x200 bmp image resolution for the time being that is it is a multiple of 16 therefore there is no problem of padding..
So now i have to cater for padding...
Can anyone help how we can solve this problem...

I am forwarding the images...

Hello,
I have written the code for the padding.. as Vmanes has posted the code sample in the forum..
I am getting an error message...

error C2440: 'initializing' : cannot convert from 'char [2]' to 'char' @ line 6
error C2440: '=' : cannot convert from 'char' to 'unsigned char [4][4]' @ line 9

The code is:-

//after reading all the data in, do padding
	  int pad_num = 16 - (end % 16) ;  //find how many to pad
	  cout<<pad_num;
	  myfile1.read( (char *)buff, (16-pad_num) );
		//cout<<buff;
	  char pad = " ";
	  for( i = 0; i < pad_num; i++ )
		{			
			 buff = pad;  //assuming you're trying to fill the array out
		}

	  for ( j=0; j<BC; j++)
	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = buff[j][i];
	  }

		Encrypt(a, rk);
		counter1();

		if (myfile3.is_open())
	  {
		  for(j=0; j< BC; j ++)
			  for ( i=0; i<4; i++)
				  myfile3<<a[i][j];		  
	  }   
	  else        
		cout << "Unable to open file";

Can anyone help how we can solve this problem

line 6: you are attempting to assign a string to a character. Replace the double quotes with single quotes: char pad = ' '; line 9: how is buff declared? Is it a character array? then you need to index into buff like this: buff[i] = pad;

commented: Very helpful... +1

Hello,
The first problem is solved that is @ line 6;
The 2nd problem still getting error messages..
Yes the buff is declared

unsigned char buff[4][4];

Oh! buff is a two dimensional array then ? If you want to fill the entire array with spaces then the simplest way is to use memset memset( buff, pad, sizeof(buff) ); Otherwise you need two loops, which is a lot more code and a lot slower than menset().

for(int i = 0; i < 4; i++)
{
   for(int j = 0; j < 4; j++)
   {
          buff[i][j] = pad;
   }
}

Hello,
The code is working correctly but now i am unable to decrypt the last text correctly that is the text with the padding
I am posting the codes..
For e.g:- text.txt has: aaaaaaaaaaaaaaaazzzzz (21 bytes)
decrypt.txt has: aaaaaaaaaaaaaaaaº2GÌB (21 bytes) the padded text not decrypted correctly
Can anyone help how we can solve this problem

unsigned char buff[4][4];
ifstream myfile1 ("p.txt", ios::binary);
ofstream myfile3  ("encrypt.txt", ios::binary);

long begin,end,size;
begin = myfile1.tellg();
myfile1.seekg (0, ios::end);
end = myfile1.tellg();
size = (end-begin);	
cout << "File size is: " << size << " bytes.\n";
myfile1.seekg( 0, ios::beg );

while(myfile1.read( (char *)buff, 16 ))
{  
  for ( j=0; j<BC; j++)
  for ( i=0; i < 4; i++)        
  {
  	a[i][j] = buff[j][i];
  }
  Encrypt(a, rk);
  counter1();
  if (myfile3.is_open())
  {
  	for(j=0; j< BC; j ++)
	for ( i=0; i<4; i++)
		myfile3<<a[i][j];	  
  }   
  else        
	cout << "Unable to open file";
 }
 
 // Padding 
 int pad_num = 16 - (end % 16) ;
 myfile1.read( (char *)buff, (16-pad_num) );
 char pad = ' ';
 memset( buff, pad, sizeof(pad_num) );
 
 for ( j=0; j<BC; j++)
 for ( i=0; i < 4; i++)        
 	a[i][j] = buff[j][i];
 	  
	Encrypt(a, rk);
	counter1();
 
myfile3.write( (char *)a, (16-pad_num) );

myfile3.close();
myfile1.close();

// Decryption part
unsigned char buffer[4][4];
ifstream myfile4;
myfile4.open( "encrypt.txt", ios::binary);
if( !myfile4 )
{
	cout << "error opening file, quitting" << endl;
	return -1;
}
ofstream myfile5 ("test.txt", ios::binary);

long begin1,end1,size1;
begin1 = myfile4.tellg();
myfile4.seekg (0, ios::end);
end1 = myfile4.tellg();
size1 = (end1-begin1);	

myfile4.seekg( 0, ios::beg );

while(myfile4.read( (char *)buffer, 16 ))
{
  for ( j=0; j<BC; j++)
  for ( i=0; i < 4; i++)        
    a[i][j] = buffer[j][i]; // plaintext
    
    Decrypt(a, rk);
    counter2();
    
    if (myfile5.is_open())
    {		
    	for(j=0; j<BC; j ++)
	for ( i=0; i<4; i++)
	{
	myfile5<<a[i][j];
	}
    }   
    else        
	cout << "Unable to open file";
  }	
  
  //after reading all the data in, do padding
  int pad_num1 = 16 - (end1 % 16) ;
  myfile4.read( (char *)buffer, (16-pad_num1) );
  
  char pad1 = ' ';
  memset( buffer, pad1, sizeof(pad_num1) );
  
  for ( j=0; j<BC; j++)
  for ( i=0; i < 4; i++)        
  {
	a[i][j] = buffer[j][i];
  }

	Decrypt(a, rk);
	counter2();
	
	myfile5.write( (char *)a, (16-pad_num1) );
		 
	myfile5.close();
	myfile4.close();
commented: You really persevered in solving this, and you dealt well with those who helped! +2

line 36 and 95 are wrong. should be sizeof(buff) (which is 16) not sizeof(buf_num) which is an integer and more likely to be 4. OR maybe you want to set all the unread bytes to the value of pad? If that's correct then why not just set the entire buffer to spaces and the do the read?

I think all the lines 31-36 and 91-95 are suspect. It looks like it is trying to do too much work. Just tell read() to get 16 bytes, and if the file size is smaller than that then read() will get the entire file. There is no need to outguess the read() function. You can call stream's gcount() function to get the actual number of bytes read.

I am still not able to solve the problem..
I am posting the code in zip format..
The problem is the padding for the last remaining bytes..
I am unable to display the decrypted data for the remaining bytes..

<deleted>

Hello,
Yes its compiling and running successfully... but the problem is when the data is decrypted in the file text.txt the output is not as expected if you have run it check the file text.txt and the file p.txt it should be the same...
The problem is with the padding of the last remaining bytes...
I am not able to decrypt it as expected..

The problem was the way the files were being read in main(). Attached is a corrected copy of the cpp file.

Hello,
Yes I am getting the correct results now.. Thanks..
All my problem is solved now..
A special thanks goes to Vmanes, Ancient dragon, Dave Sinkula, Firenet and WaltP..and all those who have helped me in this forum...
Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.