Path: blob/master/data/headers/windows/rc4.h
19591 views
//1// License:2// https://github.com/rapid7/metasploit-framework/blob/master/LICENSE3//45// This code was originally obtained and modified from the following source6// by Bobin Verton:7// https://gist.github.com/rverton/a44fc8ca67ab9ec3208989#define N 256 // 2^81011void swap(unsigned char *a, unsigned char *b) {12int tmp = *a;13*a = *b;14*b = tmp;15}1617int KSA(char *key, unsigned char *S) {18int len = strlen(key);19int j = 0;2021for (int i = 0; i < N; i++) {22S[i] = i;23}2425for (int i = 0; i < N; i++) {26j = (j + S[i] + key[i % len]) % N;27swap(&S[i], &S[j]);28}2930return 0;31}3233int PRGA(unsigned char *S, char *plaintext, unsigned char *ciphertext, int plainTextSize) {34int i = 0;35int j = 0;3637for (size_t n = 0, len = plainTextSize; n < len; n++) {38i = (i + 1) % N;39j = (j + S[i]) % N;40swap(&S[i], &S[j]);41int rnd = S[(S[i] + S[j]) % N];42ciphertext[n] = rnd ^ plaintext[n];43}4445return 0;46}4748int RC4(char *key, char *plaintext, unsigned char *ciphertext, int plainTextSize) {49unsigned char S[N];50KSA(key, S);51PRGA(S, plaintext, ciphertext, plainTextSize);52return 0;53}5455