caesar_cipher.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <iostream>
  2. using namespace std;
  3. string get_encrypted_text(string temp,int key)
  4. {
  5. int i,j,temp_integer;
  6. string op="";
  7. string alphabets="abcdefghijklmnopqrstuvwxyz",capital_alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers="0123456789";
  8. for(i=0;temp[i]!='\0';i++)
  9. {
  10. if((int)temp[i]<=122&&(int)temp[i]>=97)
  11. {
  12. temp_integer=((int)temp[i]-97+key)%26;
  13. op+=alphabets[temp_integer];
  14. }
  15. else if((int)temp[i]>=65&&(int)temp[i]<=91)
  16. {
  17. temp_integer=((int)temp[i]-65+key)%26;
  18. op+=capital_alphabets[temp_integer];
  19. }
  20. else if((int)temp[i]>=48&&(int)temp[i]<=57)
  21. {
  22. temp_integer=((int)temp[i]-48+key)%10;
  23. op+=numbers[temp_integer];
  24. }
  25. else if((int)temp[i]==32)
  26. op+=" ";
  27. else
  28. op+=(char)((int)(temp[i])+key);
  29. }
  30. return op;
  31. }
  32. int main() {
  33. string op;
  34. int key;
  35. char ip[100];
  36. cout<<"Enter the String to be encrypted\n";
  37. cin.getline(ip,sizeof(ip));
  38. cout<<"\nEnter the key\n";
  39. cin>>key;
  40. op=get_encrypted_text(ip,key);
  41. cout<<"\nEncrypted String is "<<op<<endl<<"-----------------------------------------------------";
  42. for(int j=0;j<26;j++)
  43. {
  44. op=get_encrypted_text (op,1);
  45. cout<<"\nEncrypted combinations are "<<op<<endl;
  46. }
  47. return 0;
  48. }
  49. /*
  50. #include <iostream>
  51. #include <ctime>
  52. using namespace std;
  53. int main( )
  54. {
  55. // current date/time based on current system
  56. time_t now = time(0);
  57. // convert now to string form
  58. char* dt = ctime(&now);
  59. cout << "The local date and time is: " << dt << endl;
  60. // convert now to tm struct for UTC
  61. tm *gmtm = gmtime(&now);
  62. dt = asctime(gmtm);
  63. cout << "The UTC date and time is:"<< dt << endl;
  64. }
  65. */