Help needed with Caesar Cipher

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 7
Reputation: Gewalop is an unknown quantity at this point 
Solved Threads: 0
Gewalop Gewalop is offline Offline
Newbie Poster

Help needed with Caesar Cipher

 
0
  #1
Apr 23rd, 2009
Hi, I'm trying to write a code for Caesar Cipher, I don't seem to be able to get it right, I'm having no compiling errors, but there're some real logical errors, the encryption function is working fine, but I'm having troubles with the decryption.
I'd love it if someone helps me, but please if you can make the decryption function work the same way the encryption one works, I mean "mathematically".

thx very much in advance


  1. #include<iostream.h>
  2. #include<string.h>
  3. #include<windows.h>
  4. void CaesarEncrypt(char word[],int key,int size);
  5. void CaesarDecrypt(char word[],int key,int size);
  6. void GetWord(char word[],int &size);
  7. void PrintArray(char array[],int n);
  8. int main()
  9. {
  10. int key,size=-1;
  11. char word[100];
  12. char choice;
  13. cout << "Enter the word please:\n";
  14. GetWord(word,size);
  15. cout << "Please enter the key\n";
  16. cin >>key;
  17. cout << "Enter \"e\" to encrypt, \"d\" to decrypt\n";
  18. cin>>choice;
  19. switch (choice)
  20. {
  21. case 'E':case 'e':CaesarEncrypt(word,key,size);break;
  22. case 'D':case 'd':CaesarDecrypt(word,key,size);break;
  23. }
  24. PrintArray(word,size);
  25. return 0;
  26. }
  27.  
  28. void CaesarEncrypt(char word[],int key,int size)
  29. {
  30. for (int i=0;i<size;i++)
  31. {
  32. if (word[i]>64&&word[i]<91)
  33. {
  34. word[i]=(char)(((word[i]+key-65)%26)+65);
  35. }
  36. if (word[i]<123&&word[i]>96)
  37. {
  38. word[i]=(char)(((word[i]+key-95)%26)+95);
  39. }
  40.  
  41. }
  42. }
  43.  
  44. void CaesarDecrypt(char word[],int key,int size)
  45. {
  46. for (int i=0;i<size;i++)
  47. {
  48. if (word[i]>64&&word[i]<91)
  49. {
  50. word[i]=(char)(((word[i]-key-65)%26)+65);
  51. }
  52. if (word[i]<123&&word[i]>96)
  53. {
  54. word[i]=(char)(((word[i]-key-95)%26)+95);
  55. }
  56.  
  57. }
  58. }
  59.  
  60. void GetWord(char word[],int &size)
  61. {
  62. do
  63. {
  64. size++;
  65. word[size]=cin.get();//cin.get() was added by Maya Shallouf
  66. }
  67. while (word[size]!='\n');//'\n' was added by Maya Shallouf
  68. }
  69.  
  70. void PrintArray(char array[],int n)
  71. {
  72. for (int i=0;i<n;i++)
  73. {
  74. cout << array[i];
  75. }
  76. cout <<endl;
  77. }
Last edited by Gewalop; Apr 23rd, 2009 at 1:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Help needed with Caesar Cipher

 
0
  #2
Apr 23rd, 2009
I think you should send the argument "word" as a reference and not copies of the argument.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Help needed with Caesar Cipher

 
0
  #3
Apr 23rd, 2009
i actually worked at a place that used a Caesar Cipher to encrypt user passwords for the operators who ran the production equipment.

and the "encrypted" password file was stored locally on every production computer.

no joke.

my first day there, i was like.... "LOL, WUT?"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: Gewalop is an unknown quantity at this point 
Solved Threads: 0
Gewalop Gewalop is offline Offline
Newbie Poster

Re: Help needed with Caesar Cipher

 
0
  #4
Apr 24th, 2009
Originally Posted by Sky Diploma View Post
I think you should send the argument "word" as a reference and not copies of the argument.
My friend... oh my friend
word is (as you see) an array, and the name "word" as a fixed pointer at the first item in the array ,so when you send it to the function , you do it like this.
  1. func(int array[])
  2. {
  3. }
  4. int array[50];
  5. func(array);

----------------------

People please... I need help
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Help needed with Caesar Cipher

 
0
  #5
Apr 24th, 2009
>>I think you should send the argument "word" as a reference and not copies of the argument.
He is passing a pointer actually.

>>the encryption function is working fine, but I'm having troubles with the decryption.
Even Encryption is not good. Here is the output:
  1. siddhant3s@Xion:~/Documents$ ./testofcallbyreference
  2. Enter the word please:
  3. mathematically
  4. Please enter the key
  5. 5
  6. Enter "e" to encrypt, "d" to decrypt
  7. e
  8. rf_mjrf_nhfqqd

So, what is the error?
I think you may have guessed.
Check if the Resultant character exceeds 'z' or 'Z' , If it does do the needfull
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 794
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Help needed with Caesar Cipher

 
0
  #6
Apr 24th, 2009
Look, I coded the function myself and it is working great;
I will give you the code of the encrypter to get you started, you should write the decrypter yourself:
  1. void CaesarEncrypt(char word[],int key,int size)
  2. {
  3. for (int i=0;i<size;i++)
  4. {
  5. if (word[i] >='A' && word[i] <='Z')
  6. {
  7. int OV=word[i] + key;//overflow
  8.  
  9. word[i]=(OV <= 'Z')? OV :'A' + (OV-'Z'-1);
  10. }
  11.  
  12. if (word[i]<='z'&&word[i]>='a')
  13. {
  14. int OV=word[i] + key;//overflow
  15.  
  16. word[i]=(OV <= 'z')? OV :'a' + (OV-'z'-1);
  17. }
  18.  
  19. }
  20. }
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC