Skip to content

Instantly share code, notes, and snippets.

@Jinz0
Created December 28, 2011 18:01
Show Gist options
  • Select an option

  • Save Jinz0/1528906 to your computer and use it in GitHub Desktop.

Select an option

Save Jinz0/1528906 to your computer and use it in GitHub Desktop.
Simple implementation of RC4 enciphering and VL64 + B64 enciphering
/*
Rolecall:
Jeax ~ Original implementation (I think)
Jordan ~ Java edition (Most of this is based off it, well all of it really)
Adil ~ Porting to C++ (Didn't even know I could do this lol)
*/
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include "RC4.h"
using namespace std;
CRC4 rc4;
static string Encode_VL64(int i){
string s = "";
char res[6]; //assign the var res to a a char array - len6
int p = 0;
int sP = 0;
int bytes = 1;
int negativeMask = i >= 0 ? 0 : 4; //? : = if else clause
i = abs(i);
res[p++] = (char)(64 +(i & 3));
for (i >>= 2; i != 0; i >>= 6){
bytes++;
res[p++] = (char)(64 + (i & 0x3f));
}
int null = 0;
res[sP] = (char)(res[sP] | bytes << 3 | negativeMask);
string tmp = string(res);
//tmp.erase(2,22); //clever little method to cut the string.
//If this was not done, the output would have extremely weird chars.
return tmp;
}
static string Encode_B64(int i){
string s = "";
for (int x = 1; x <= 2; x++){
s += (char)((char)(64 + (i >> 6 * (2 - x) & 0x3f)));
}
return s;
}
int main()
{
int choice = 0;
int value;
int b64val;
while(choice <= 3){
cout <<"Packetscout V1.0 - STABLE\n";
cout <<"#########################\n";
cout <<"### 1: Encode to B64 ###\n";
cout <<"### 2: Encode to VL64 ###\n";
cout <<"### 3: Decode packet ###\n";
cout <<"#########################\n";
cin >> choice;
switch(choice){
case 1:{
cout << "Enter your value\n";
cin >> b64val;
cout << Encode_B64(b64val) << "\n";
cout << "\n\n\n\n\n\n\n\n";
break;
}
case 2:{
cout << "Enter your value\n";
cin >> value;
cout << Encode_VL64(value);
cout << "\n\n\n\n\n\n\n\n";
break;
}
case 3:
{char key[64];
cout << "Enter a key\n";
cin >> key;
rc4.Encrypt(key, "NV6VVFPoC7FLDlzDUri3qcOAg9cRoFOmsYR9ffDGy5P8HfF6eekX40SFSVfJ1mDb3lcpYRqdg28sp61eHkPukKbqTu1JsVEKiRavi04YtSzUsLXaYSa5BEGwg5G2OF");
cout << "Encrypted key : " << key << endl;
cout << "Decrypted key : " << rc4.Decrypt(key, "NV6VVFPoC7FLDlzDUri3qcOAg9cRoFOmsYR9ffDGy5P8HfF6eekX40SFSVfJ1mDb3lcpYRqdg28sp61eHkPukKbqTu1JsVEKiRavi04YtSzUsLXaYSa5BEGwg5G2OF");
main();
}
default: exit(1);
}
}}
#define SWAP(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
class CRC4
{
public:
CRC4 ()
{
memset(sbox,0,256);
memset(key,0,256);
}
virtual ~CRC4 ()
{
memset(sbox,0,256); /* remove Key traces in memory */
memset(key,0,256);
}
char *Encrypt(char *pszText,const char *pszKey)
{
i=0, j=0,n = 0;
ilen = (int)strlen(pszKey);
for (m = 0; m < 256; m++) /* Initialize the key sequence */
{
*(key + m)= *(pszKey + (m % ilen));
*(sbox + m) = m;
}
for (m=0; m < 256; m++)
{
n = (n + *(sbox+m) + *(key + m)) &0xff;
SWAP(*(sbox + m),*(sbox + n));
}
ilen = (int)strlen(pszText);
for (m = 0; m < ilen; m++)
{
i = (i + 1) &0xff;
j = (j + *(sbox + i)) &0xff;
SWAP(*(sbox+i),*(sbox + j)); /* randomly Initialize
the key sequence */
k = *(sbox + ((*(sbox + i) + *(sbox + j)) &0xff ));
if(k == *(pszText + m)) /* avoid '\0' among the
encoded text; */
k = 0;
*(pszText + m) ^= k;
}
return pszText;
}
char *Decrypt(char *pszText,const char *pszKey)
{
return Encrypt(pszText,pszKey) ; /* using the same function
as encoding */
}
private:
unsigned char sbox[256]; /* Encryption array */
unsigned char key[256],k; /* Numeric key values */
int m, n, i, j, ilen; /* Ambiguously named counters */
};
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment