Last active
February 1, 2018 13:36
-
-
Save yni3/d67366605c0f82ec9df96e65bbd427a7 to your computer and use it in GitHub Desktop.
ARCFOUR implemenation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ARCFOUR.h -- ARCFOUR implemenation | |
Copyright 2018 yni3 | |
License : Public Domain (useful for copy and paste for your project) | |
*/ | |
#pragma once | |
#ifndef __ARCFOUR | |
#define __ARCFOUR | |
class ARCFOUR | |
{ | |
static constexpr unsigned int N = 255; | |
//Conversion table | |
unsigned char m_S[N]; | |
int i = 0, j = 0; | |
inline void swap(unsigned char *a, unsigned char *b) const { | |
int tmp = *a; | |
*a = *b; | |
*b = tmp; | |
} | |
void KSA(const unsigned char *key, int key_len, unsigned char *S) const { | |
int j = 0; | |
for (int i = 0; i < N; i++) | |
S[i] = i; | |
for (int i = 0; i < N; i++) { | |
j = (j + S[i] + key[i % key_len]) % N; | |
swap(&S[i], &S[j]); | |
} | |
} | |
void PRGA(unsigned char *S, unsigned char *dst, const unsigned char *src, int srclen) { | |
for (size_t n = 0, len = srclen; n < len; n++) { | |
i = (i + 1) % N; | |
j = (j + S[i]) % N; | |
swap(&S[i], &S[j]); | |
int rnd = S[(S[i] + S[j]) % N]; | |
dst[n] = rnd ^ src[n]; | |
} | |
} | |
public: | |
ARCFOUR() | |
: m_S(), i(0), j(0) | |
{ | |
} | |
ARCFOUR(const unsigned char *key, int key_len) | |
{ | |
KSA(key, key_len, m_S); | |
} | |
virtual ~ARCFOUR() {}; | |
//call straightforward encrypt | |
//call reverse decrypt | |
void RC4(unsigned char *dst, const unsigned char *src, int srclen) { | |
PRGA(m_S, dst, src, srclen); | |
} | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment