⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc4.c

📁 基于实时嵌入式系统的voip系统(real time embeded system)。主要难点在实时处理语音信号。语音信号基于其自身特点
💻 C
字号:
/**************************************************************************
*  rc4.c - RC4 Encryption and Key Generation.
*
*  Author - Ed Hursey
*  Date - December 2005
*
*  Description - This file includes functions for creating an RC4
*      key and encrypting and decrypting with RC4.
*
*  Note - This file is based on a few examples of the RC4
*         Encryption algorithm that I found on the web
*         most of it came from:
*  http://freebsd.active-venture.com/FreeBSD-srctree/newsrc/crypto/rc4/rc4.c.html
**************************************************************************/
#include "rc4.h"
static void swap_byte(unsigned char *a, unsigned char *b);


/**************************************************************************
*   Function - swap_byte
*   Parameters - a,b - The two bytes to be swapped
*   Returns - void
*   Purpose - waps two bytes, used by key generation
*             function below.
**************************************************************************/
static void swap_byte(unsigned char *a, unsigned char *b)
{
   unsigned char swapByte;

   swapByte = *a;
	*a = *b;
	*b = swapByte;
}

/**************************************************************************
*   Function - rc4_crypt
*   Parameters - key - the rc4_key generated by the
*                       rc4_init function.
*                inbuf - the buffer to be encrypted/decrypted
*                outbuf - the buffer that the stored
*                         encrypted/decrypted value is stored in
*                buflen - the length of in and out buffers
*   Returns - void
*   Purpose - Encrypts/Decrypts the inbuf and puts
*      in outbuf.  inbuf and outbuf can be called
*      with the same buffer if you want to over
*      write the inbuf with encrypted/decrypted
*      value.  It should be noted that the rc4
*      key gets modified by this function so
*      you either need to regenerate it every
*      time or keep a copy and pass in a copy
*      of the copy every time.
**************************************************************************/
void rc4_crypt(struct rc4_key *const key,  const unsigned char *inbuf, unsigned char *outbuf, int buflen)
{
	int i;
	unsigned char j;

	for (i = 0; i < buflen; i++) {

		/* Update modification indicies */
		key->x++;
		key->y += key->state[key->x];

		/* Modify permutation */
		swap_byte(&key->state[key->x],&key->state[key->y]);

		/* Encrypt/decrypt next byte */
		j = key->state[key->x] + key->state[key->y];
		outbuf[i] = inbuf[i] ^ key->state[j];
	}
}

/**************************************************************************
*   Function - rc4_init
*   Parameters - key - the rc4_key to be generated, i.e. this
*                      is updated with the created value.
*                key_data - the data used to create the key,
*                           it can be of any length
*                key_data_len - the length of the key data
*   Returns - void
*   Purpose - Generates an RC4 key from the key_data to be
*             used by the rc4_crypt function.
**************************************************************************/
void rc4_init(struct rc4_key *const key,  const unsigned char *key_data, int key_data_len)
{
	unsigned char j;
	int i;

	/* Initialize state with identity permutation */
	for (i = 0; i < 256; i++)
		key->state[i] = (unsigned char)i;
	key->x = 0;
	key->y = 0;

	/* Randomize the permutation using key data */
	for (j = i = 0; i < 256; i++)
	{
		j += key->state[i] + key_data[i % key_data_len];
		swap_byte(&key->state[i], &key->state[j]);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -