Guest User

Untitled

a guest
Mar 5th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // basic character-int conversions, the underlying idea behind most crypto
  2.  
  3. #include <iostream.h>
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <fstream.h>
  7.  
  8. #define CYPHER 999999
  9. #define MAXLENGTH 1000
  10.  
  11. void encrypt();
  12. void decrypt();
  13. void show();
  14.  
  15. const char * filename = "msg.txt";
  16.  
  17. int main()
  18. {
  19. char corn='5';
  20.  
  21. while (corn != '4')
  22. {
  23. clrscr();
  24. cout << "\n-----------------------------------" << endl;
  25. cout << " " << MAXLENGTH << " max chars " << endl;
  26. cout << "-----------------------------------" << endl;
  27. cout << "1) Encrypt text " << endl;
  28. cout << "2) Decrypt text " << endl;
  29. cout << "3) Show encrypted string" << endl;
  30. cout << "4) EXIT " << endl;
  31. cout << "\nselection : ";
  32. corn = getch();
  33. cout << corn;
  34. if (corn == '1')
  35. encrypt();
  36. else if (corn == '2')
  37. decrypt();
  38. else if (corn == '3')
  39. show();
  40. }
  41.  
  42. return 0;
  43. }
  44.  
  45. void encrypt()
  46. {
  47. char dummy;
  48. char encrypt[MAXLENGTH];
  49. for (long x=0;x<=MAXLENGTH;x++)
  50. encrypt[x] = NULL;
  51. fflush(stdin);
  52. cout << "\nEnter text to be encrypted: ";
  53. gets(encrypt);
  54. long stat=0;
  55. cout << endl << "processing your request..";
  56. ofstream file;
  57. file.open (filename);
  58. for (x=0;x<=MAXLENGTH;x++) {
  59. if ((long)encrypt[x] != NULL)
  60. file << (long)(encrypt[x] * CYPHER) << " ";
  61. }
  62. file << "0";
  63. file.close();
  64. cout << "\n\n\nencrypted text stored in " << filename;
  65. dummy = getch();
  66. }
  67.  
  68. void decrypt()
  69. {
  70. long l,m,key;
  71. ifstream filein;
  72. filein.open (filename);
  73. l = filein.tellg();
  74. filein.seekg (0, ios::end);
  75. m = filein.tellg();
  76. // cout << "\n..begining g: " << l << " bytes, ..end g: " << m << " bytes.\n";
  77. cout << "\nimported file is " << (m-l) << " bytes.\n\n";
  78. filein.seekg (0, ios::beg);
  79. long decrypt[MAXLENGTH];
  80. long count=0;
  81.  
  82. for (long i=0;filein.good();i++) {
  83. filein >> key;
  84. decrypt[i] = key;
  85. count++;
  86. }
  87. for (i=0;i<count-2;i++)
  88. cout << (char)(decrypt[i] / CYPHER);
  89. filein.close();
  90. cout << "\n\n\nencrypted text stored in " << filename;
  91. char dummy = getch();
  92. }
  93.  
  94. void show()
  95. {
  96. long l,m,key;
  97. ifstream filein;
  98. filein.open (filename);
  99. l = filein.tellg();
  100. filein.seekg (0, ios::end);
  101. m = filein.tellg();
  102. cout << "\nimported file is " << (m-l) << " bytes.\n";
  103. filein.seekg (0, ios::beg);
  104.  
  105. for (long i=0;filein.good();i++) {
  106. filein >> key;
  107. cout << key << " " << endl;
  108. }
  109.  
  110. filein.close();
  111. cout << "\n\n\nencrypted text stored in " << filename;
  112. char dummy = getch();
Advertisement
Add Comment
Please, Sign In to add comment