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..
|
|
July 25th, 2008 03:56 AM
# 2
|
Re: C++ ENCRYPTION (how to encrypt a text)
you could try Something like
-
// Obviously you have to make sure that the original string is less than 10 chars....
-
-
char string2[sizeof(string*2)];
-
int count2;
-
-
for(int x = 0; x<count; x++)
-
{
-
count2 = 2*x;
-
string2[count2]=string[x];
-
string2[coount2 + 1] = string[2];
-
}
-
Although I'm sure there are far more elegant ways to do this
|
|
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
|
|
July 25th, 2008 04:12 AM
# 4
|
Re: C++ ENCRYPTION (how to encrypt a text)
Quote:
you could try Something like
-
// Obviously you have to make sure that the original string is less than 10 chars....
-
-
char string2[sizeof(string*2)];
-
int count2;
-
-
for(int x = 0; x<count; x++)
-
{
-
count2 = 2*x;
-
string2[count2]=string[x];
-
string2[coount2 + 1] = string[2];
-
}
-
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..
|
|
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
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
|
|
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....
|
|
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
-
-
//Create a pointer to the location in the string that we're up to -> this means we
-
// will still be able to access the beginning of any string we need.
-
char *strin1ptr = string1;
-
char *string2ptr = string2;
-
char *string3ptr = string3;
-
-
bool is2 = true;
-
//loop while we have more characters in string1
-
while(string1ptr)
-
{
-
// if we are on an even character
-
if(is2)
-
{
-
// set this location to contain the character in string1
-
*string2ptr = *string1ptr;
-
//bump string2ptr along
-
string2ptr++;
-
}
-
else
-
//likewise for string3
-
*string3ptr = string1ptr;
-
string3ptr++;
-
}
-
//bump string1ptr along too
-
string1ptr++;
-
}
-
-
cout << string1 <<endl;
-
count << string2 <<endl;
-
count << string3 <<endl;
-
|
|
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...
|
|
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
-
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];
-
}
|
|
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...
|
|
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 :
-
int charAt =2;
-
-
count=strlen(string);
-
//If we have a short input, take the last letter instead of the third
-
if(count <charAt) count=charAt;
-
then at the point where you actually assign the value, use
string[charAt]
instead of
string[2]
|
|
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
-
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];
-
}
|
i also have a question brosert....why is it that the index of string2 and string3
is "x/2"? why is it not x?
|
|
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....
|
|
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..
|
|
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
|
|
July 25th, 2008 06:45 AM
# 16
|
Re: C++ ENCRYPTION (how to encrypt a text)
- main()
-
{
-
char string1[10],string2[10],string3[10];
-
int x,count;
-
cout<<"ENTER TEXT TO COMPACT:";
-
cin>>string1;
-
count=strlen(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];
-
}
-
cout<<string2<<endl;
-
-
-
getche();
-
}
i did that but still it onlyy works to text with 4 characters above....
i couldn't figure out what to do...
|
|
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?
|
|
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..
|
|
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...
|
|
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
-
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..
|
|
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
|
|
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
-
int charAt =2;
-
-
count=strlen(string);
-
//If we have a short input, take the last letter instead of the third
-
if(count <charAt) count=charAt;
the assignment on line 5 is the wrong way round.
|
|
July 25th, 2008 02:39 PM
# 23
|
Re: C++ ENCRYPTION (how to encrypt a text)
Brosert -
Please check your private messages
Thanks,
sicarie
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
|