2024-02-15 12:00:37 +08:00

57 lines
1.5 KiB
C++

#include "StdAfx.h"
#include <stdio.h>
void swapints(int *array, int ndx1, int ndx2)
{
int temp = array[ndx1];
array[ndx1] = array[ndx2];
array[ndx2] = temp;
}
void EnDeCrypt(const char *lpbufin, int ibufinLen, char *lpbufout, int ibufoutLen, const char *pszKey)
{
int a, b, i=0, j=0, k;
int ilen;
int sbox[256];
int key [256];
ilen = strlen(pszKey);
for (a=0; a < 256; a++){
key[a] = pszKey[a % ilen];
sbox[a] = a;
}
for (a=0, b=0; a < 256; a++){
b = (b + sbox[a] + key[a]) % 256;
swapints(sbox, a, b);
}
for (a=0; a < ibufinLen; a++){
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
swapints(sbox, i, j);
k = sbox[(sbox[i] + sbox[j]) % 256];
lpbufout[a] = lpbufin[a] ^ k;
}
return;
}
#define RC4PUBLICKEY "xtryiroeb"
int RC4EnCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen )
{
if ( lpbufin && bufinLen > 0 && lpbufout && bufoutLen > 0 ){
EnDeCrypt( lpbufin, bufinLen, lpbufout, bufoutLen, RC4PUBLICKEY );
return 0;
}
return 1;
}
int RC4DeCrypt( const char *lpbufin, int bufinLen, char *lpbufout, int bufoutLen )
{
if ( lpbufin && bufinLen > 0 && lpbufout && bufoutLen > 0 ){
EnDeCrypt( lpbufin, bufinLen, lpbufout, bufoutLen, RC4PUBLICKEY );
return 0;
}
return 1;
}