00001 /* 00002 * RC4 encryption/decryption/pseudo-random number generator 00003 * Copyright (c) 2007 Reimar Doeffinger 00004 * 00005 * loosely based on LibTomCrypt by Tom St Denis 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 #include "common.h" 00024 #include "rc4.h" 00025 00026 void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) { 00027 int i, j; 00028 uint8_t x, y; 00029 uint8_t state[256]; 00030 for (i = 0; i < 256; i++) 00031 state[i] = i; 00032 y = 0; 00033 // j is i % keylen 00034 for (j = 0, i = 0; i < 256; i++, j++) { 00035 if (j == keylen) j = 0; 00036 y += state[i] + key[j]; 00037 FFSWAP(uint8_t, state[i], state[y]); 00038 } 00039 // state initialized, now do the real encryption 00040 x = 1; y = state[1]; 00041 while (datalen-- > 0) { 00042 uint8_t sum = state[x] + state[y]; 00043 FFSWAP(uint8_t, state[x], state[y]); 00044 *data++ ^= state[sum]; 00045 x++; 00046 y += state[x]; 00047 } 00048 }