Ⅰ 如何在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 之間並沒有虛擬鏈路,它們只是發送、接收數據報的對象。