/* Perform the encrypt/decrypt operation (can use it for either since this is a stream cipher). NOTE: *msg and *out must be the same pointer (performance tweak)  */
void ICACHE_FLASH_ATTR RC4_crypt(RC4_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
/* Perform the encrypt/decrypt operation (can use it for either since this is a stream cipher). NOTE: *msg and *out must be the same pointer (performance tweak)  */
void ICACHE_FLASH_ATTR RC4_crypt(RC4_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
{ 
    int i;
    uint8_t *m, x, y, a, b;
    x = ctx->x;
    y = ctx->y;
    m = ctx->m;
    for (i = 0; i < length; i++)
    {
        a = m[++x];
        y += a;
        m[x] = b = m[y];
        m[y] = a;
        out[i] ^= m[(uint8_t)(a + b)];
    }
    ctx->x = x;
    ctx->y = y;
}