Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

C++ ENCRYPTION (how to encrypt a text)

Question posted by: jupi13 (Newbie) on July 25th, 2008 03:49 AM
i got a problem wtih my encryption...
would you be kind to help me with this...i'm new to this c++ programing...

i have this expansion encryption method..where you will add a char in between the original text..example >>>>. josh..this could be >>>> jxoxsxhx...like that...

my program goes like this

main()
{
char string[10],count;
int x,y;

cout<<"ENTER TEXT:";
cin>>string;
count=strlen(string);

for(x=0;x<count;x++)

cout<<string[x]<<string[2];

//if i'll enter "JOSH" the output would be "JSOSSSHS"
//if i'll enter "jupi" the output would be "jpupppip"

getche();
}



//what i need to do is to store the output in another array.
example. array[] will be equal to the cout in the program above which is
string[x] and string[2]...>>>>> array[x]="string[x]" and "string[2]"

how will i do that?.....
how will i do it?
please help everyone..
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
03:56 AM
#2

Re: C++ ENCRYPTION (how to encrypt a text)
you could try Something like
Expand|Select|Wrap|Line Numbers
  1. // Obviously you have to make sure that the original string is less than 10 chars....
  2.  
  3. char string2[sizeof(string*2)];
  4. int count2;
  5.  
  6. for(int x = 0; x<count; x++)
  7. {
  8.   count2 = 2*x;
  9.   string2[count2]=string[x];
  10.   string2[coount2 + 1] = string[2];
  11. }
  12.  


Although I'm sure there are far more elegant ways to do this

Reply
gpraghuram's Avatar
gpraghuram
Expert
1,120 Posts
July 25th, 2008
04:02 AM
#3

Re: C++ ENCRYPTION (how to encrypt a text)
This dosent look encryption as your original characters remains as it is with newly added characters.
Try using Blow-Fish for encrypting the string and the blow-fish is available on the net.

Raghu

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
04:12 AM
#4

Re: C++ ENCRYPTION (how to encrypt a text)
Quote:
you could try Something like
Expand|Select|Wrap|Line Numbers
  1. // Obviously you have to make sure that the original string is less than 10 chars....
  2.  
  3. char string2[sizeof(string*2)];
  4. int count2;
  5.  
  6. for(int x = 0; x<count; x++)
  7. {
  8.   count2 = 2*x;
  9.   string2[count2]=string[x];
  10.   string2[coount2 + 1] = string[2];
  11. }
  12.  


Although I'm sure there are far more elegant ways to do this





what will i output in this code?
string2?
how will i do it?
please help?

if you don't mind..can you give the whole code?
im just a newbie to C++;;


thanks..

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
04:24 AM
#5

Re: C++ ENCRYPTION (how to encrypt a text)
If you want to output something at the end of it, you can still use cout....

eg
Expand|Select|Wrap|Line Numbers
  1. cout << string2;


This code shoudl fit nicely into your current code...
Where you declare string and count, also declare string and count2...

where your forloop is, try the forloop in the original response.

Last edited by Brosert : July 25th, 2008 at 04:25 AM. Reason: Wrong info re count2
Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
04:42 AM
#6

Re: C++ ENCRYPTION (how to encrypt a text)
ehhyyy....thanks lot...it worked..

hehehe
...thanks man...

ahm,,if you don't mind,can you help me with this also?

if i'll enter "brosert" the output would be "boet" and the missing letters ("rsr")
would be stored in another array....

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
04:53 AM
#7

Re: C++ ENCRYPTION (how to encrypt a text)
This time...

Set up 3 arrays (you could probably do it with 2, but that would be more complex....), loop through, if we are on an even iteration store to one array, otherwise store to the other.
I'll assume you have three equal sized arrays called string1, string2, string3...
string1 recieves input (as before) in a null terminated string
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Create a pointer to the location in the string that we're up to -> this means we
  3. // will still be able to access the beginning of any string we need.
  4. char *strin1ptr = string1;
  5. char *string2ptr = string2;
  6. char *string3ptr = string3; 
  7.  
  8. bool is2 = true;
  9. //loop while we have more characters in string1
  10. while(string1ptr)
  11. {
  12.   // if we are on an even character
  13.   if(is2)
  14.   {
  15.     // set this location to contain the character in string1
  16.     *string2ptr = *string1ptr;
  17.      //bump string2ptr along
  18.      string2ptr++;
  19.   }
  20.   else
  21.     //likewise for string3
  22.     *string3ptr = string1ptr;
  23.      string3ptr++;
  24.   }
  25.   //bump string1ptr along too
  26.   string1ptr++;
  27. }
  28.  
  29. cout << string1 <<endl;
  30. count << string2 <<endl;
  31. count << string3 <<endl;
  32.  

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
05:07 AM
#8

Re: C++ ENCRYPTION (how to encrypt a text)
can you do it without using a pointer?

we're not yet using pointers...

we are only allowed to use array..and loop..

can you do it?
please...

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
05:22 AM
#9

Re: C++ ENCRYPTION (how to encrypt a text)
in that case, use a for loop....imnstead of the while loop

eg
Expand|Select|Wrap|Line Numbers
  1. int count = sizeof(string1);
  2. for(int x=0; x<count; x++)
  3. {
  4.   /Check whether x is a multiple of 2
  5.   if(x%2 == 0)
  6.   {
  7.     string2[x/2] = string1[x];
  8.   }
  9.   else
  10.   {
  11.     string3[x/2] = string1[x];
  12. }

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
05:33 AM
#10

Re: C++ ENCRYPTION (how to encrypt a text)
\brosert............

//the program you give me only works in a 6-letter word and a 7-letter word...


//here it is


main()
{


char string[10],count,string2[20];
int count2;

cout<<"ENTER TEXT:";
cin>>string;
count=strlen(string);

for(int x=0;x<count;x++)
{
count2 = 2*x;
string2[count2]=string[x];
string2[count2 + 1] = string[2];

}
cout<<string2;

getche();

//what's wrong with it.....my problem is that i want to expand the text...it should
work on text with 1-10 characters.....
if "i" is entered it would output.. "ii"...
if "you is entered it would output "yuouuu"...like that..
if "abcd" is enterd it would output "acbcccdcec"...

please help me...
please...

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
05:44 AM
#11

Re: C++ ENCRYPTION (how to encrypt a text)
Part of the problem is that it always takes the THIRD character....
If your string is shorter than 3 characters, it takes a (probably) NULL character, which terminates the string, and would give you a single character output....

where you get string try making the following modification :

Expand|Select|Wrap|Line Numbers
  1. int charAt =2;
  2.  
  3. count=strlen(string);
  4. //If we have a short input, take the last letter instead of the third
  5. if(count <charAt) count=charAt;
  6.  


then at the point where you actually assign the value, use
string[charAt]
instead of
string[2]

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
05:46 AM
#12

Re: C++ ENCRYPTION (how to encrypt a text)
Quote:
in that case, use a for loop....imnstead of the while loop

eg
Expand|Select|Wrap|Line Numbers
  1. int count = sizeof(string1);
  2. for(int x=0; x<count; x++)
  3. {
  4.   /Check whether x is a multiple of 2
  5.   if(x%2 == 0)
  6.   {
  7.     string2[x/2] = string1[x];
  8.   }
  9.   else
  10.   {
  11.     string3[x/2] = string1[x];
  12. }



i also have a question brosert....why is it that the index of string2 and string3
is "x/2"? why is it not x?

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
05:53 AM
#13

Re: C++ ENCRYPTION (how to encrypt a text)
because only half of the characters go in each string....
so string2 and string3 will be half the size of string1.

In c (and I think most languages) if we divide a number in a place where we can only use an integer, it will autometically round the number down....
so 0/2 = 0 (obviously)
but 1/2 = 0 (because we can only use the integer part of the answer).

this means, that if we always take the current value/2, we will always be entering into the correct box of each string (might need to think about that for a second....)

it means if string1 has 7 characters,
the ones in position 0,2,4,6 will become 0,1,2,3 in string2
the ones in position 1,3,5 will become 0,1,2 in string3

If you need a better explanation, I can try again....

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
06:22 AM
#14

Re: C++ ENCRYPTION (how to encrypt a text)
brosert.......

this one also...it only works with letters with 4 characters above..


int count = sizeof(string1);
for(int x=0; x<count; x++)
{
/Check whether x is a multiple of 2
if(x%2 == 0)
{
string2[x/2] = string1[x];
}
else
{
string3[x/2] = string1[x];
}


waht should i do?

i can't figure it out..
please help..

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
06:30 AM
#15

Re: C++ ENCRYPTION (how to encrypt a text)
Not real sure....
try changing
count = sizeof(string1) to = strlen(string1) (as you had in your first program....

Meanwhile I'll have a deeper look/think

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
06:45 AM
#16

Re: C++ ENCRYPTION (how to encrypt a text)
Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3. char string1[10],string2[10],string3[10];
  4. int x,count;
  5. cout<<"ENTER TEXT TO COMPACT:";
  6. cin>>string1;
  7. count=strlen(string1);
  8.  
  9. for(int x=0; x<count; x++)
  10.  
  11.   //Check whether x is a multiple of 2
  12.   if(x%2 == 0)
  13.   {
  14.     string2[x/2] = string1[x];
  15.   }
  16.   else
  17.   {
  18.     string3[x/2] = string1[x];
  19.     }
  20.  cout<<string2<<endl;
  21.  
  22.  
  23. getche();
  24. }



i did that but still it onlyy works to text with 4 characters above....
i couldn't figure out what to do...

Reply
Brosert's Avatar
Brosert
Member
53 Posts
July 25th, 2008
08:51 AM
#17

Re: C++ ENCRYPTION (how to encrypt a text)
Do you get the wrong output for smaller strings, or none at all?

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
10:55 AM
#18

Re: C++ ENCRYPTION (how to encrypt a text)
Quote:
Do you get the wrong output for smaller strings, or none at all?



i got the wrong output for smaller strings..like if i enter "abc" the
output is "ac@".... or "ab" i would get "aG@"....

what should i do with this?

please help..

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
11:01 AM
#19

Re: C++ ENCRYPTION (how to encrypt a text)
Quote:
Do you get the wrong output for smaller strings, or none at all?



i think the problem in this is the size of the string...but i realy can't figure ir out...

Reply
jupi13's Avatar
jupi13
Newbie
13 Posts
July 25th, 2008
11:05 AM
#20

Re: C++ ENCRYPTION (how to encrypt a text)
got a problem wtih my encryption...
would you be kind to help me with this...i'm new to this c++ programing...

i have this expansion encryption method..where you will add a char in between the original text..example >>>>. josh..this could be >>>> jxoxsxhx...like that...

my program goes like this


Expand|Select|Wrap|Line Numbers
  1. main()
  2. {
  3. char string[10],count;
  4. int x,y;
  5.  
  6. cout<<"ENTER TEXT:";
  7. cin>>string;
  8. count=strlen(string);
  9.  
  10. for(x=0;x<count;x++)
  11.  
  12. cout<<string[x]<<string[2]; 
  13.  
  14. //if i'll enter "JOSH" the output would be "JSOSSSHS"
  15. //if i'll enter "jupi" the output would be "jpupppip"
  16.  
  17. getche();
  18. }




//what i need to do is to store the output in another array.
example. array[] will be equal to the cout in the program above which is
string[x] and string[2]...>>>>> array[x]="string[x]" and "string[2]"

how will i do that?.....
how will i do it?
please help everyone..

Reply
Banfa's Avatar
Banfa
AdministratorVoR
5,039 Posts
July 25th, 2008
11:15 AM
#21

Re: C++ ENCRYPTION (how to encrypt a text)
jupi13 I can see no reason why you restarted your encryption thread. Double posting is against the forum rules and you could clearly see that your original thread was active.

Anyway please try to avoid starting more than 1 thread on the same subject in future.

Banfa
Administrator

Reply
Banfa's Avatar
Banfa
AdministratorVoR
5,039 Posts
July 25th, 2008
11:48 AM
#22

Re: C++ ENCRYPTION (how to encrypt a text)
Part of the problem will all the code samples/examples in this thread is that none of them are correctly handling strings.

You are using C strings rather than the C++ string class (I assume you have not been taught that yet). In C a string is an array of printable characters with a 0 or NULL terminator. To get the length of a string you should use the function strlen. The operator sizeof returns the length of the array that contains the string not the length of a string.

Then when using char * pointers to write characters into an array of char once you have finished if you want to use the array as a string then you should make sure that you have written a 0 terminator in the correct place.

And finally in this piece of code
Expand|Select|Wrap|Line Numbers
  1.       int charAt =2;
  2.  
  3.       count=strlen(string);
  4.       //If we have a short input, take the last letter instead of the third
  5.       if(count <charAt) count=charAt;

the assignment on line 5 is the wrong way round.

Reply
sicarie's Avatar
sicarie
Moderator
3,799 Posts
July 25th, 2008
02:39 PM
#23

Re: C++ ENCRYPTION (how to encrypt a text)
Brosert -

Please check your private messages

Thanks,

sicarie

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,873 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top C / C++ Forum Contributors