473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C++ ENCRYPTION (how to encrypt a text)

18
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..
Jul 25 '08 #1
22 3335
Brosert
57
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.  
  13.  
Although I'm sure there are far more elegant ways to do this
Jul 25 '08 #2
gpraghuram
1,275 Expert 1GB
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
Jul 25 '08 #3
jupi13
18
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.  
  13.  
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..
Jul 25 '08 #4
Brosert
57
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;
  2.  
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.
Jul 25 '08 #5
jupi13
18
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....
Jul 25 '08 #6
Brosert
57
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.  
  33.  
Jul 25 '08 #7
jupi13
18
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...
Jul 25 '08 #8
Brosert
57
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. }
  13.  
Jul 25 '08 #9
jupi13
18
\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...
Jul 25 '08 #10
Brosert
57
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.  
  7.  
then at the point where you actually assign the value, use
string[charAt]
instead of
string[2]
Jul 25 '08 #11
jupi13
18
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. }
  13.  

i also have a question brosert....why is it that the index of string2 and string3
is "x/2"? why is it not x?
Jul 25 '08 #12
Brosert
57
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....
Jul 25 '08 #13
jupi13
18
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..
Jul 25 '08 #14
Brosert
57
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
Jul 25 '08 #15
jupi13
18
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...
Jul 25 '08 #16
Brosert
57
Do you get the wrong output for smaller strings, or none at all?
Jul 25 '08 #17
jupi13
18
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..
Jul 25 '08 #18
jupi13
18
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...
Jul 25 '08 #19
jupi13
18
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. }
  19.  


//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..
Jul 25 '08 #20
Banfa
9,065 Expert Mod 8TB
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
Jul 25 '08 #21
Banfa
9,065 Expert Mod 8TB
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;
  6.  
the assignment on line 5 is the wrong way round.
Jul 25 '08 #22
sicarie
4,677 Expert Mod 4TB
Brosert -

Please check your private messages

Thanks,

sicarie
Jul 25 '08 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Phil Palmieri | last post by:
Im using md5 to encrypt and decrypt plain text, this works fine... When i try to run the same function on a binary file, it does not decrypt correctly. Is there a way to encrypt binary files...
5
by: TomB | last post by:
Anyone know of an example/tutorial for encrypting a binary file? I'm able to encrypt/decrypt simple text files, but anything more complicated craps out. Thanks TomB
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
6
by: larry mckay | last post by:
Hi, Does anyone have any simple text string encryption routines that are easy to implement? I'm trying to prevent users and system administrators from figuring out how I implement things....
4
by: pintu | last post by:
Hello everybody.. I hav some confusion regarding asymmetric encryption.As asymmetric encryption it there is one private key and one public key.So any data is encrypted using private key and the...
25
by: eggie5 | last post by:
I have a form where a user can change his password, but I'm confused on how to prevent this from being transmitted in plain text. Well, I know how not to transmit it in plain text - use any type...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
22
by: Wilson | last post by:
i am learning to program using c++ and was set a task of making a simple encryption algorithim. I choose to start with one where simply each letter is replaced with its equivilent in the alphabet...
19
by: klenwell | last post by:
Another request for comments here. I'd like to accomplish something like the scheme outlined at this page here: http://tinyurl.com/3dtcdr In a nutshell, the form uses javascript to hash...
5
by: Netwatcher | last post by:
well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.