Advertisement
darkn3tc0d3r

Encryption

Dec 22nd, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. // Create a new .cpp file .. name it encryption_table
  2. //+ source
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7. const int NSYMS = 95;
  8. const int NSHUFFLES = 1000;
  9. const int kSTART = 0;
  10. const int kINCREMENT = 1;
  11. static int substitutions[] = {
  12. 'U', 'L', 'd', '{', 'p', 'Q', '@', 'M',
  13. '-', ']', 'O', 'r', 'c', '6', '^', '#',
  14. 'Y', 'w', 'W', 'v', '!', 'F', 'b', ')',
  15. '+', 'h', 'J', 'f', 'C', '8', 'B', '7',
  16. '.', 'k', '2', 'u', 'Z', '9', 'K', 'o',
  17. '3', 'x', ' ', '\'', '=', '&', 'N', '*',
  18. '1', 'z', '(', '`', 'R', 'P', ':', 'l',
  19. '4', '0', 'e', '$', '_', '}', 'j', 't',
  20. '?', 'S', 'q', '>', ';', 'T', 'y', 'i',
  21. '\\', 'A', 'D', 'V', '5', '|', '<', '/',
  22. 'E', 'g', 'm', ',', '[', 'H', '%', 'a',
  23. 's', 'n', 'I', 'X', '~', '"', 'G'
  24. };
  25. const int len = sizeof(substitutions) / sizeof(int);
  26. int main()
  27. {
  28. cout << "Enter seed for random number generator" << endl;
  29. int s;
  30. cin >> s;
  31. srand(s);
  32. char table[NSYMS];
  33. for(int i = 0; i < NSYMS; i++)
  34. table[i] = i + ' ';
  35. // shuffle
  36. for(int j = 0; j < NSHUFFLES; j++ ) {
  37. int ndx1 = rand() % NSYMS;
  38. int ndx2 = rand() % NSYMS;
  39. char temp = table[ndx1];
  40. table[ndx1] = table[ndx2];
  41. table[ndx2] = temp;
  42. }
  43. // Output
  44. int j = 0;
  45. for(int i = 0; i < NSYMS; i++) {
  46. cout << "'" << table[i] << "', ";
  47. j++;
  48. if(j == 8) { cout << endl; j = 0; }
  49. }
  50. cout << endl;
  51. return 0;
  52. }
  53.  
  54.  
  55. //+ Now create another .cpp file named encrytpion
  56. //+ source
  57.  
  58. #include <iostream>
  59. #include <cstdlib>
  60. #include <fstream>
  61. #include <string.h>
  62. using namespace std;
  63. const int NSYMS = 95;
  64. const int NSHUFFLES = 1000;
  65. const int kSTART = 0;
  66. const int kINCREMENT = 1;
  67. static int substitutions[] = {
  68. 'U', 'L', 'd', '{', 'p', 'Q', '@', 'M',
  69. '-', ']', 'O', 'r', 'c', '6', '^', '#',
  70. 'Y', 'w', 'W', 'v', '!', 'F', 'b', ')',
  71. '+', 'h', 'J', 'f', 'C', '8', 'B', '7',
  72. '.', 'k', '2', 'u', 'Z', '9', 'K', 'o',
  73. '3', 'x', ' ', '\'', '=', '&', 'N', '*',
  74. '1', 'z', '(', '`', 'R', 'P', ':', 'l',
  75. '4', '0', 'e', '$', '_', '}', 'j', 't',
  76. '?', 'S', 'q', '>', ';', 'T', 'y', 'i',
  77. '\\', 'A', 'D', 'V', '5', '|', '<', '/',
  78. 'E', 'g', 'm', ',', '[', 'H', '%', 'a',
  79. 's', 'n', 'I', 'X', '~', '"', 'G'
  80. };
  81. const int len = sizeof(substitutions) / sizeof(int);
  82. const int kMSGSIZE = 200000;
  83. static ofstream outfile;
  84. void OpenOutfile(void);
  85. int GetAndCheckLine(char line[], int linelen);
  86. void GetMessage(char txt[]);
  87. void Encrypt(char m[]);
  88. void SaveToFile(const char m[]);
  89. void CloseFile(void);
  90. void OpenOutfile(void)
  91. {
  92. const int kNAMESIZE = 100;
  93. char filename[kNAMESIZE];
  94. cout <<"\t\t\t:] CODED BY XXLB::OREN [:"<<endl;
  95. cout << "\n\nEnter name of message file : ";
  96. cin.getline(filename, kNAMESIZE-1, '\n');
  97. outfile.open(filename, ios::out);
  98. if(!outfile.good()) {
  99. cout << "Can't open that file. Quitting." << endl;
  100. exit(1);
  101. }
  102. }
  103. int GetAndCheckLine(char line[], int linelen)
  104. {
  105. cin.getline(line, linelen - 1, '\n');
  106. // Check for a line with no characters
  107. int actuallength = strlen(line);
  108. if(actuallength == 0)
  109. return 0;
  110. // Check contents
  111. // all characters should be printable ASCII
  112. for(int i = 0; i< actuallength; i++)
  113. if(!isprint(line[i])) {
  114. cout << "Illegal characters,"
  115. " line discarded"; return -1; }
  116. return actuallength;
  117. }
  118. void GetMessage(char txt[])
  119. {
  120. int count = 0;
  121. txt[count] = '\0';
  122. const int ll = 120;
  123. char Line[ll];
  124. int n = GetAndCheckLine(Line,ll);
  125. while(n != 0) {
  126. if(n > 0) {
  127. count += n;
  128. if(count > kMSGSIZE-2) {
  129. cout << "\n\nSorry that message is"
  130. "too long. Quitting." << endl;
  131. exit(1);
  132. }
  133. strcat(txt, Line);
  134. // add the space to the end of the message
  135. txt[count] =' ';
  136. // update message length
  137. count++;
  138. // and replace the null just overwritten
  139. txt[count] = '\0';
  140. }
  141. n = GetAndCheckLine(Line,ll);
  142. }
  143. }
  144. void Encrypt(char m[])
  145. {
  146. int k = kSTART;
  147. for(int i=0; m[i] != '\0'; i++) {
  148. char ch = m[i];
  149. m[i] = substitutions[(ch-32 +k ) % len];
  150. k += kINCREMENT;
  151. }
  152. }
  153. void SaveToFile(const char m[])
  154. {
  155. outfile << m;
  156. }
  157. void CloseFile(void)
  158. {
  159. outfile.close();
  160. }
  161. int main()
  162. {
  163. char msg[kMSGSIZE];
  164. OpenOutfile();
  165. cout <<"\n\n--------------------------------------------------------------------------------"<<endl;
  166. cout << "Enter text of message:" << endl;
  167. GetMessage(msg);
  168. cout << "Enter seed for encrypting routine generator" << endl;
  169. int s;
  170. cin >> s;
  171. srand(s);
  172. char table[NSYMS];
  173. for(int i = 0; i < NSYMS; i++)
  174. table[i] = i + ' ';
  175. // shuffle
  176. for(int j = 0; j < NSHUFFLES; j++ ) {
  177. int ndx1 = rand() % NSYMS;
  178. int ndx2 = rand() % NSYMS;
  179. char temp = table[ndx1];
  180. table[ndx1] = table[ndx2];
  181. table[ndx2] = temp;
  182. }
  183. // Output
  184. int j = 0;
  185. for(int i = 0; i < NSYMS; i++) {
  186. cout << "'" << table[i] << "', ";
  187. j++;
  188. if(j == 8) { cout << endl; j = 0; }
  189. }
  190. cout <<"\n--------------------------------------------------------------------------------"<<endl;
  191. cout << "\n\nEncrypting and saving." << endl;
  192. Encrypt(msg);
  193. SaveToFile(msg);
  194. CloseFile();
  195. cout <<"\n:] ========== >>>>> DONE <<<<< =========="<<endl;
  196. system("pause");
  197. return 0;
  198. }
  199.  
  200. //+ compile and run ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement