openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::RC4 Class Reference

#include <rc4.hpp>

Public Member Functions

void byte_swap (uint8_t *a, uint8_t *b)
 
void trad_swap (unsigned char *a, unsigned char *b)
 
void XOR_swap (unsigned char *a, unsigned char *b)
 
void KSA (char *key, unsigned char *S, int swap_type)
 
void PRGA (unsigned char *S, char *plaintext, unsigned char *ciphertext, int swap_type)
 
std::string store_hash (char *plaintext, unsigned char *hashtext, int swap_type)
 
unsigned char * compute (char *key, char *plaintext, unsigned char *hashtext, int swap_type)
 

Detailed Description

Examples
rc4.cpp.

Definition at line 71 of file rc4.hpp.

Member Function Documentation

◆ byte_swap()

void gpmp::RC4::byte_swap ( uint8_t *  a,
uint8_t *  b 
)

Definition at line 49 of file rc4.cpp.

49  {
50  uint8_t swapped = *a;
51  swapped = *a;
52  *a = *b;
53  *b = swapped;
54 }

◆ compute()

unsigned char * gpmp::RC4::compute ( char *  key,
char *  plaintext,
unsigned char *  hashtext,
int  swap_type 
)
Examples
rc4.cpp.

Definition at line 142 of file rc4.cpp.

145  {
146  if (ciphertext == NULL) {
147  throw std::runtime_error("[-] Error Allocating Memory");
148  }
149 
150  // check for swap types 1-3
151  if (0 <= swap_type && swap_type <= 2) {
152  unsigned char S[BYTE_LIMIT];
153  KSA(key, S, swap_type);
154  PRGA(S, plaintext, ciphertext, swap_type);
155  }
156 
157  else if (swap_type > 2) {
158  throw std::runtime_error("[-] Invalid swap_type");
159  }
160 
161  return ciphertext;
162 }
void PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext, int swap_type)
Definition: rc4.cpp:90
void KSA(char *key, unsigned char *S, int swap_type)
Definition: rc4.cpp:68
#define BYTE_LIMIT
Definition: rc4.hpp:63

References BYTE_LIMIT.

Referenced by main().

◆ KSA()

void gpmp::RC4::KSA ( char *  key,
unsigned char *  S,
int  swap_type 
)

Definition at line 68 of file rc4.cpp.

68  {
69  uint32_t len = strlen(key);
70  int j = 0;
71 
72  for (int i = 0; i < BYTE_LIMIT; i++) {
73  S[i] = i;
74  }
75 
76  for (int i = 0; i < BYTE_LIMIT; i++) {
77  j = (j + S[i] + key[i % len]) & BITS;
78 
79  // choose swap algorithm based off swap_type
80  if (swap_type == 0) {
81  XOR_swap(&S[i], &S[j]);
82  } else if (swap_type == 1) {
83  trad_swap(&S[i], &S[j]);
84  } else if (swap_type == 2) {
85  byte_swap(&S[i], &S[j]);
86  }
87  }
88 }
void XOR_swap(unsigned char *a, unsigned char *b)
Definition: rc4.cpp:62
void byte_swap(uint8_t *a, uint8_t *b)
Definition: rc4.cpp:49
void trad_swap(unsigned char *a, unsigned char *b)
Definition: rc4.cpp:56
#define BITS
Definition: rc4.hpp:62

References BITS, and BYTE_LIMIT.

◆ PRGA()

void gpmp::RC4::PRGA ( unsigned char *  S,
char *  plaintext,
unsigned char *  ciphertext,
int  swap_type 
)

Definition at line 90 of file rc4.cpp.

93  {
94  int i = 0;
95  int j = 0;
96 
97  for (size_t n = 0, len = strlen(plaintext); n < len; n++) {
98  i = (i + 1) & BITS;
99  j = (j + S[i]) & BITS;
100 
101  // choose swap algorithm based off swap_type
102  if (swap_type == 0) {
103  XOR_swap(&S[i], &S[j]);
104  } else if (swap_type == 1) {
105  trad_swap(&S[i], &S[j]);
106  } else if (swap_type == 2) {
107  byte_swap(&S[i], &S[j]);
108  }
109 
110  uint32_t rnd = S[(S[i] + S[j]) & BITS];
111 
112  ciphertext[n] = rnd ^ plaintext[n];
113  }
114 }

References BITS.

◆ store_hash()

std::string gpmp::RC4::store_hash ( char *  plaintext,
unsigned char *  hashtext,
int  swap_type 
)
Examples
rc4.cpp.

Definition at line 117 of file rc4.cpp.

117  {
118  // length of our plaintext
119  size_t len = strlen((char *)plaintext);
120  // for snprintf declare a buffer
121  char buffer[len + 1];
122  size_t size = sizeof(buffer);
123 
124  // initialize empty string
125  std::string stored_text = "";
126 
127  // traverse the hashtext appending each block to a string
128  for (size_t index = 0; index < len; index++) {
129  if (swap_type == 0) {
130  snprintf(buffer, size, "|x%02hhx|", hashtext[index]);
131  }
132  // to format our hash to a string for future manipulation
133  else {
134  snprintf(buffer, size, "%02hhX", hashtext[index]);
135  }
136  // append the declared string
137  stored_text += std::string(buffer);
138  }
139  return stored_text;
140 }

Referenced by main().

◆ trad_swap()

void gpmp::RC4::trad_swap ( unsigned char *  a,
unsigned char *  b 
)

Definition at line 56 of file rc4.cpp.

56  {
57  int swapped = *a;
58  *a = *b;
59  *b = swapped;
60 }

◆ XOR_swap()

void gpmp::RC4::XOR_swap ( unsigned char *  a,
unsigned char *  b 
)

Definition at line 62 of file rc4.cpp.

62  {
63  *a ^= *b;
64  *b ^= *a;
65  *a ^= *b;
66 }

The documentation for this class was generated from the following files: