Ⅰ 如何在voip中使用rc4对udp收发函数进行加密
qutecom 一个开源的voip客户端
asterisk 开源的ippbx
rc4加密算法简单,快速,据说是比DES算法快10倍。sip 信令本身就属于明文方式传输的,之所以要加密,是为了防止运营商的干扰,使用一个弱的加密算法,是要能防止串改就满足要求了。
rc4 算法可以google原来,用密钥来生成一个256长度的box, 然后box与明文异或操作得到密文,密文再次异或就恢复明文。
下面实现了 qutecom 到asterisk 信令的当向加密,反向的目前还没弄完,等完工了在来补充。
rc4.h
/*
*RC4 functions for HTMLDOC.
*
* Original code by Rob Earhart
* Copyright 1999 by Carnegie Mellon University, All Rights Reserved
*
* Permission to use, , modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above right notice appear in all copies and that
* both that right notice and this permission notice appear in
* supporting documentation, and that the name of Carnegie Mellon
* University not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _RC4_H_
# define _RC4_H_
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
/*
* RC4 context...
*/
typedef struct
{
unsigned char sbox[256]; /* S boxes for encryption */
int i, j; /* Current indices into S boxes */
} rc4_context_t;
/*
* Prototypes...
*/
extern void rc4_init(rc4_context_t *context, const unsigned char *key,
unsigned keylen);
extern void rc4_encrypt(rc4_context_t *context, const unsigned char *input,
unsigned char *output, unsigned len);
# ifdef __cplusplus
}
# endif /* __cplusplus */
#endif /* !_RC4_H_ */
rc4.c
/*
* RC4 functions for HTMLDOC.
*
* Original code by Tim Martin
* Copyright 1999 by Carnegie Mellon University, All Rights Reserved
*
* Permission to use, , modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above right notice appear in all copies and that
* both that right notice and this permission notice appear in
* supporting documentation, and that the name of Carnegie Mellon
* University not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Contents:
*
* rc4_init() - Initialize an RC4 context with the specified key.
* rc4_encrypt() - Encrypt the given buffer.
*/
#include "rc4.h"
/*
* 'rc4_init()' - Initialize an RC4 context with the specified key.
*/
void
rc4_init(rc4_context_t *text, /* IO - Context */
const unsigned char *key, /* I - Key */
unsigned keylen) /* I - Length of key */
{
int i, j; /* Looping vars */
unsigned char tmp; /* Temporary variable */
/*
* Fill in linearly s0=0, s1=1, ...
*/
for (i = 0; i < 256; i ++)
text->sbox[i] = i;
for (i = 0, j = 0; i < 256; i ++)
{
/*
* j = (j + Si + Ki) mod 256
*/
j = (j + text->sbox[i] + key[i % keylen]) & 255;
/*
* Swap Si and Sj...
*/
tmp = text->sbox[i];
text->sbox[i] = text->sbox[j];
text->sbox[j] = tmp;
}
/*
* Initialized counters to 0 and return...
*/
text->i = 0;
text->j = 0;
}
/*
* 'rc4_encrypt()' - Encrypt the given buffer.
*/
void
rc4_encrypt(rc4_context_t *text, /* I - Context */
const unsigned char *input, /* I - Input buffer */
unsigned char *output, /* O - Output buffer */
unsigned len) /* I - Size of buffers */
{
unsigned char tmp; /* Swap variable */
int i, j; /* Looping vars */
int t; /* Current S box */
/*
* Loop through the entire buffer...
*/
i = text->i;
j = text->j;
while (len > 0)
{
/*
* Get the next S box indices...
*/
i = (i + 1) & 255;
j = (j + text->sbox[i]) & 255;
/*
* Swap Si and Sj...
*/
tmp = text->sbox[i];
text->sbox[i] = text->sbox[j];
text->sbox[j] = tmp;
/*
* Get the S box index for this byte...
*/
t = (text->sbox[i] + text->sbox[j]) & 255;
/*
* Encrypt using the S box...
*/
*output++ = *input++ ^ text->sbox[t];
len --;
}
/*
* Copy current S box indices back to context...
*/
text->i = i;
text->j = j;
}
修改exosip项目中的 jcallback.c 在函数cb_udp_snd_message 中修改,加入rc4加密部分
....
if( 1 )
{
rc4_context_t context;
char * key = "*****";
unsigned char * out = NULL;
int i=0;
out = osip_malloc (length);
if (out == NULL)
return -1;
rc4_init(&context,key,16);
rc4_encrypt(&context,message,out,length);
rc4_message = osip_malloc(length+4);
if(rc4_message != NULL)
{
rc4_message[0] = 'R';
rc4_message[1] = 'C';
rc4_message[2] = '4';
rc4_message[3] = ':';
for(i=0;i<length;i++)
{
rc4_message[i+4] = out[i];
}
}
osip_free(out);
}
// Really send the packet over network
if(rc4_message == NULL)
{
i = owsip_send (account, (const void*) message, length, 0, address, OWSL_ADDRESS_SIZE);
}
else
{
i = owsip_send (account, (const void*) rc4_message, length+4, 0, address, OWSL_ADDRESS_SIZE);
osip_free(rc4_message);
}
....
在asterisk 中的chan_sip.c 修改函数 sipsock_read, 添加 接受信令rc4解密代码
.....
if(res>4 && req.data[0]=='R' && req.data[1]=='C' && req.data[2]=='4' && req.data[3]==':')
{
rc4_context_t context;
char * key = "********";
unsigned char * out = NULL;
int i=0;
out = malloc(res-4);
rc4_init(&context,key,16);
rc4_encrypt(&context,req.data+4,out,res-4);
for(i=0;i<res-4;i++)
{
req.data[i] = out[i];
}
free(out);
req.data[res-4] = '/0';
res = res-4;
req.len = res;
}
.....
Ⅱ 用迅雷下载时,传输层采用什么协议(TCP/UDP)
迅雷可以走tcp,也可以走迅雷加密的udp。tcp的话很好识别,也很容易被封杀,而加密的udp就不好识别了。tcp/udp 都可以用来做为迅雷下载的传输协议。
Ⅲ UDP是什么,UDP协议及优缺点
UDP,全称 User Datagram Protocol,中文名称为用户数据报协议,主要用来支持那些需要在计算机之间传输数据的网络连接。
UDP 协议从问世至今已经被使用了很多年,虽然目前 UDP 协议的应用不如 TCP 协议广泛,但 UDP 依然是一种非常实用和可行的网络传输层协议。尤其是在一些实时性很强的应用场景中,比如网络游戏、视频会议等,UDP 协议的快速能力更具有独特的魅力。
UDP 是一种面向非连接的协议,面向非连接指的是在正式通信前不必与对方先建立连接,不管对方状态就直接发送数据。至于对方是否可以接收到这些数据,UDP 协议无法控制,所以说 UDP 是一种不可靠的协议。
UDP 协议适用于一次只传送少量数据、对可靠性要求不高的应用环境。
与前面介绍的 TCP 协议一样,UDP 协议直接位于 IP 协议之上。实际上,IP 协议属于 OSI 参考模型的网络层协议,而 UDP 协议和 TCP 协议都属于传输层协议。
因为 UDP 是面向非连接的协议,没有建立连接的过程,因此它的通信效率很高,但也正因为如此,它的可靠性不如 TCP 协议。
UDP 协议的主要作用是完成网络数据流和数据报之间的转换在信息的发送端,UDP 协议将网络数据流封装成数据报,然后将数据报发送出去;在信息的接收端,UDP 协议将数据报转换成实际数据内容。
可以认为 UDP 协议的 socket 类似于码头,数据报则类似于集装箱。码头的作用就是负责友送、接收集装箱,而 socket 的作用则是发送、接收数据报。因此,对于基于 UDP 协议的通信双方而言,没有所谓的客户端和服务器端的概念。
UDP 协议和 TCP 协议简单对比如下:
TCP 协议:可靠,传输大小无限制,但是需要连接建立时间,差错控制开销大。
UDP 协议:不可靠,差错控制开销较小,传输大小限制在 64 KB以下,不需要建立连接。
?相比较 TCP,UDP 是一种不可靠的网络协议,它在通信实例的两端各建立一个 socket,但这两个 socket 之间并没有虚拟链路,它们只是发送、接收数据报的对象。