Hello,
I am having a problem for coping the string into a 2d array..
Also if the length of the string is more than 16 bytes how to continue to read till end of string..
Can anyone help..

int encrypt()
{
	string text = "Encryption";
	char b1[4][4];	

        text.length();

	strcpy(text,b1); // problem how to copy the string to the 2d array
        // also if the string length is more than 16 bytes long how to continue read in chunks of   16 bytes till end of string	
	
	  for ( int j=0; j<BC; j++)
	  for ( int i=0; i < 4; i++)        
	  {
		  a[i][j] = b1[j][i];
	  }

	  Encrypt(a, rk);

	return 0;	
}

this is one (not the most efficient) way:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

template< std::size_t N > inline 
void copy_string_to_array(  char (&array) [N][N], 
                           const char* cstr )
{
  std::memset( array, 0, N*N ) ;
  std::memcpy( array, cstr, std::min( std::strlen(cstr), N*N ) ) ; 
}

int main()
{
  enum { N = 4 } ;
  const std::string str = "this is the string to be copied" ;
  char array[N][N] ;

  std::size_t copied = 0 ;
  while( copied < str.size() )
  {
    copy_string_to_array( array, str.c_str()+copied ) ;
    copied += N * N ;
    // process array
  }
}

strcpy() needs two C style strings, not an STL string and a C style string. The parameters are strcpy(to, from); where to is the string to receive and from is the string to donate. There is a variation called strncpy() which takes a third parameter indicating max number of char to copy.

b1 can handle 4 C style strings with maximum length of 3.

Here's a little demo showing how t copy the string into the array. It does not account for string length, but simply demonstrates how you might start this process.

#include <iostream>
#include <string>
using namespace std;

int main( )

{
    string text = "Encryption";
    char b1[4][4];	

    size_t len =   text.length();  //not using this yet

    strcpy( (char *)b1, text.c_str( ) ); 
    for( int i = 0; i < 4; i++ )
      {
          for( int j = 0; j < 4; j++ )
            cout << b1[i][j];
         cout << endl;
      }
   return 0;
}

If your compiler (like Visual C++ 2005/2008) has the safe version of string copy, you pass the size of destination, and array overflow cannot occur.

When you know the length of the string, you simply take in in multiples of 16 till exhausted, much like the way you did your file reading. Here you'll know starting out how big the string is.

line 7 vijay's function declaration is wrong or just plain convoluted void copy_string_to_array( char array[N][N], const char* cstr ) >>I am having a problem for coping the string into a 2d array..

// the following array will hold up to 4 strings, and each string can be no more than 3 charcters plus the null terminator.  That means you would have to split the word "Encryption" up into 4 words of 3 characters each.  Is that what you are trying to do?
char b1[4][4]; 
strcpy(b1[0],"Enc");
strcpy(b1[1],"ryp");
strcpy(b1[2],"tio");
strcpy(b1[3],"n");

// you could just copy the whole word to array b1, but it would have to be treated as a single array as in the next example

// or do you want to just copy "Encryption" into a single array
char b1[16];
strcpy(b1,"Encryption");

> line 7 vijay's function declaration is wrong or just plain convoluted
it is not wrong. if it seems to be just plain convoluted, it is so for a reason

template< std::size_t N > inline
void function_one( char (&array) [N][N] )
{ array[N-1][N-1] = 0 ; }

template< std::size_t N > inline
void function_two( char array [N][N] )
{ array[N-1][N-1] = 0 ; }

int main()
{
  char a[20][20] ;
  function_one( a ) ; // ok
  function_two( a ) ; // ok

  char b[5][15] ;
  function_one( b ) ; // error: no matching function call
  function_two( b ) ; // compiles without error
}

Hello,
I have modified the code as:-

int do_encrypt(string text)
{      char b1[4][4];	
	
        strcpy( (char *)b1, text.c_str( ));	
	
   	  for (  j=0; j<BC; j++)
 	  for ( i=0; i < 4; i++)        
	  {
		  a[i][j] = b1[j][i];
	  }

	  Encrypt(a, rk);

	  printf("Plaintext  : ");
          for(j=0; j< 4; j ++)
          for ( i=0; i<4; i++)
          cout<<a[i][j];

	  	return 0;	
	}
}

int main()
{
      do_encrypt("i am using advanced encryption standard");
      return 0;
}

So how can we encrypt the full text that is the length is 39 bytes long and the code is encrypting in chunks of 16 bytes...
So how we read the full text and encrypt with the padding integrated..

How about this as a model? Note the use of strncpy( ) which limits the number of characters copied.

#include <string>
#include <cstdio>
#include <iostream>
using namespace std;

int do_encrypt(string text)
{      
   char b1[4][4];	
   char a[4][4];
   size_t len = text.length( );
   int i, j;

   //copy and encrypt full 16 char blocks
   while( len >= 16 )
   {
      strncpy( (char *)b1, text.c_str( ), 16);

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

      //Encrypt(       );
      len -= 16;
   }

   //copy and encrypt any trailing chars
   if( len > 0 )
   {
      strncpy( (char *)b1, text.c_str( ), len);

      //
      //pad out the rest of the matrix, if needed
      //

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

      //Encrypt(         );
   }

    return 0;	
}


int main()
{
   do_encrypt("i am using advanced encryption standard");
   return 0;
}

Hello,
Instead of returning 0 in the function i want it to return the encrypted text in the function

return 0;

Problem i need to return both the encrypted text + padded encrypted text...
How we do that..

Declare a string in the function, fill it in, return it. The string class can be used as a return type.

>>So how can we encrypt the full text that is the length is 39 bytes long and the code is encrypting in chunks of 16 bytes...

Be aware, this:
while( len >= 16 )
{
strncpy( (char *)b1, text.c_str( ), 16);
len -= 16;
}

will just keeping copying the first 16 char of text into b1 len/16 times. If text is longer than 16 char this won't break out or encrypt anything beyond the first 16 char.

Here's an alternate version you might be able to use as a model.

WARNING: the following code is not compiled, tested, or written to specification of the problem. It is intended as an example of a protocol only.

void encryptText(string text)
{
  //break text into chunks 10 char long
  char tempChunk[11];
  char encryptedChunck[11];

  int len = text.length();
  int j = 0;

  for(int i = 0; i < len; ++i)
  {
     if(j == 10)
     {
        //end current chunk
        tempChunk[j] = '\0'  
        
        encryptCurrentChunk(tempChunk, encryptedChunk);

        //do something with encryptedChunk
        cout << encryptedChunk << '\n';

        //reset j to 0 to start over
        j = 0;
     }

    //copy current char of text into temp
    tempChunk[j++] = text[i];
  } 
 
 //the body of the above loop will never reach the terminal null char of text so copy it here
  temp[j] = '\0';
  encryptCurrentChunk(tempChunk, encryptedChunk);
  cout << encryptedChunk << '\n';
}

void encryptCurrentChunk(char * tempChunk, char * encryptedChunk)
{
   len = strlen(tempChunk);
   for(int i = 0; i < len; ++i)
   {
      //do encryption one char at a time
      encryptedChunk[i] = temp[i] + 1;
   }
   encryptedChunk[i] = '\0';
}
commented: Yep, you caught my mistake. +3

Yep, I missed iterating through the source string. My bad.

New motto: Engage brain before activating keyboard.

Here's a correction to my earlier post, which does iterate through the source string. It also demonstrates creation of a string in the function and passing it back as the return value. (You really should have been able to figure that out;)

#include <string>
#include <cstdio>
#include <iostream>
using namespace std;

string do_encrypt(string text)
{      
    char b1[4][4];	
    char a[4][4];
    size_t len = text.length( );
    int start = 0;
    int i, j;

    string what_I_return = "some string created in this function";

    //copy and encrypt full 16 char blocks
    while( len >= 16 )
    {
        strncpy( (char *)b1, text.substr(start, 16).c_str( ), 16);

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

            //Encrypt(       );
            len -= 16;
            start += 16;
    }

    //copy and encrypt any trailing chars
    if( len > 0 )
    {
        strncpy( (char *)b1, text.substr(start, len).c_str( ), len);
    //
    //pad out the rest of the matrix, if needed
    //
    }

    return what_I_return;	
}


int main()
{
    string what_function_returns;

    what_function_returns = do_encrypt("i am using advanced encryption standard");
    cout << what_function_returns << endl;

    return 0;
}
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.