openGPMP
Open Source Mathematics Package
Functions
rc4.cpp File Reference
#include <iostream>
#include <openGPMP/nt/rc4.hpp>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( void  )
Examples
rc4.cpp.

Definition at line 14 of file rc4.cpp.

14  {
15  /*
16  * The RC4 algorithm in this case used 5 methods all dependent off
17  * eachother with only 2 being required by the user and 1
18  * optional.
19  * 1. compute(): requires
20  */
21  std::cout << "<--------------------------------------------------"
22  "---->\n\n";
23  std::cout << "RIVEST CIPHER (RC4) EXAMPLE\n\n";
24 
25  /*<--------------------------------INIT--------------------------------->*/
26 
27  // declare RC4 class obj
28  gpmp::RC4 rc;
29 
30  /*
31  * declare some key values and strings to hash
32  * these first 3 plaintext strings are verified from the RC4 wiki
33  * and will be displayed when using a traditional swap method
34  */
35  // expected hash = BBF316E8D940AF0AD3
36  char *key0 = (char *)"Key";
37  char *text0 = (char *)"Plaintext";
38  // expected hash = 1021BF0420
39  char *key1 = (char *)"Wiki";
40  char *text1 = (char *)"pedia";
41  // expected hash = 45A01F645FC35B383552544B9BF5
42  char *key2 = (char *)"Secret";
43  char *text2 = (char *)"Attack at dawn";
44 
45  char *key3 = (char *)"1";
46  char *text3 = (char *)"username";
47 
48  /*
49  * char value for using one of the following
50  * XOR swap 0
51  * traditional swap 1
52  * byte swap 2
53  */
54  int x_swap = 0;
55  int t_swap = 1;
56  int b_swap = 2;
57 
58  /*<------------------------------------------------------------------>*/
59 
60  // declare a ciphertext pointer and allocate memory
61  unsigned char *hashtext_0 =
62  (unsigned char *)malloc(sizeof(int) * strlen(text0));
63 
64  std::cout << "HASHTEXT_0: " << hashtext_0 << std::endl;
65  printf("String before hash : %s\n", text0);
66 
67  /*
68  * the main arguments are <key> & <plaintext> + allocated memory
69  * of ciphertext as well as the swap type (x_swap true/false)
70  */
71  clock_t a;
72  a = clock();
73 
74  unsigned char *computed_text = rc.compute(key0, text0, hashtext_0, x_swap);
75  std::cout << "computed_text addr: " << &computed_text << std::endl;
76  /*
77  * function to store the computed hash
78  */
79  std::string display_text = rc.store_hash(text0, hashtext_0, x_swap);
80  std::cout << "COMPUTED HASH(XSWAP) = " << display_text << std::endl;
81 
82  // free whats no longer in use
83  free(hashtext_0);
84  a = clock() - a;
85  double a_time = ((double)a) / CLOCKS_PER_SEC;
86  printf("a_time = %f sec(s)\n\n", a_time);
87  /*<------------------------------------------------------------------>*/
88  printf("String before hash: %s\n", text1);
89  unsigned char *hashtext_1 =
90  (unsigned char *)malloc(sizeof(int) * strlen(text1));
91 
92  clock_t b;
93  b = clock();
94 
95  unsigned char *computed_text1 = rc.compute(key1, text1, hashtext_1, t_swap);
96  std::cout << "computed_text1 addr: " << &computed_text1 << std::endl;
97  std::string display_text1 = rc.store_hash(text1, hashtext_1, t_swap);
98  std::cout << "COMPUTED HASH (TSWAP) = " << display_text1 << std::endl;
99 
100  free(hashtext_1);
101  b = clock() - b;
102  double b_time = ((double)b) / CLOCKS_PER_SEC;
103  printf("b_time = %f sec(s)\n\n", b_time);
104  /*<------------------------------------------------------------------>*/
105  printf("String before hash: %s\n", text2);
106  unsigned char *hashtext_2 =
107  (unsigned char *)malloc(sizeof(int) * strlen(text2));
108 
109  clock_t c;
110  c = clock();
111  unsigned char *computed_text2 = rc.compute(key2, text2, hashtext_2, b_swap);
112  std::cout << "computed_text2 addr: " << &computed_text2 << std::endl;
113  std::string display_text2 = rc.store_hash(text2, hashtext_2, b_swap);
114  std::cout << "COMPUTED HASH (BSWAP) = " << display_text2 << std::endl;
115 
116  free(hashtext_2);
117  c = clock() - c;
118  double c_time = ((double)c) / CLOCKS_PER_SEC;
119  printf("c_time = %f sec(s)\n\n", c_time);
120 
121  /*<------------------------------------------------------------------>*/
122  printf("String before hash: %s\n", text3);
123  unsigned char *hashtext_3 =
124  (unsigned char *)malloc(sizeof(int) * strlen(text3));
125 
126  clock_t d;
127  d = clock();
128  unsigned char *computed_text3 = rc.compute(key3, text3, hashtext_3, x_swap);
129  std::cout << "computed_text2 addr: " << &computed_text3 << std::endl;
130  std::string display_text3 = rc.store_hash(text2, hashtext_3, x_swap);
131  std::cout << "COMPUTED HASH (XSWAP) = " << display_text3 << std::endl;
132 
133  free(hashtext_3);
134  d = clock() - d;
135  double d_time = ((double)d) / CLOCKS_PER_SEC;
136  printf("d_time = %f sec(s)\n\n", d_time);
137 
138  return EXIT_SUCCESS;
139 }
std::string store_hash(char *plaintext, unsigned char *hashtext, int swap_type)
Definition: rc4.cpp:117
unsigned char * compute(char *key, char *plaintext, unsigned char *hashtext, int swap_type)
Definition: rc4.cpp:142

References gpmp::RC4::compute(), and gpmp::RC4::store_hash().