實現PROXY穿越(2):Base64演算法

來源:互聯網
上載者:User

  最近忙,事比較多,活多了,還要降薪,唉。沒什麼時間看書,將以前的一些技術blog也移到這裡。NTLM在去年年底和今年年初研究過一陣子,寫了總結和例子程式。裡面涉及很多演算法,在網上查了很久。下面是以前的部落格:

 

  如果我們進行抓包,在步驟二、步驟三、步驟四中,傳遞相關資訊,在WWW-Authenticate中,使用了Base64演算法進行擾碼。

  按照RFC2045的定義,Base64被定義為:Base64內容傳送編碼被設計用來把任意序列的8位位元組描述為一種不易被人直接識別的形式。(The
Base64 Content-Transfer-Encoding is designed  to
represent arbitrary sequences of octets in a form that need not be
humanly readable.)

  BASE64的原理可以在RFC中查閱,或者青參考網路共產主義:http://www.5dmail.net/html/2004-1-30/200413084348.htm

。我們在下面給出相關的程式碼。

//
*********************************  Base64 演算法 begin
*****************************


// Translation Table as
described in RFC1113

static const char
cb64[]="ABCDEFGHIJKLMNOPQRSTUVWX

YZabcdefghijklmnopqrstuvwxyz0123456789+/";

static unsigned char db64[256] = {

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  62,  255, 
255,  255,  63, 
52,  53,  54, 
55,  56,  57, 
58,  59,  60, 
61, 

   
255,  255, 
255,  0,  255, 
255,  255,  0, 
1,  2,  3, 
4,  5,  6, 
7,  8,  9, 
10, 

   
11,  12,  13, 
14,  15,  16, 
17,  18,  19, 
20,  21,  22, 
23,  24,  25, 
255,  255, 

   
255,  255, 
255,  255,  26, 
27,  28,  29, 
30,  31,  32, 
33,  34,  35, 
36,  37, 

   
38,  39,  40, 
41,  42,  43, 
44,  45,  46, 
47,  48,  49, 
50,  51,  255, 
255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 
255,  255, 

   
255,  255, 
255,  255,  255};

//
encodeblock


// encode 3 8-bit binary bytes
as 4 '6-bit' characters


static void encodeblock( unsigned char in[3], unsigned char out[4],
int len )

{

    out[0] =
cb64[ in[0] >> 2 ];

    out[1] =
cb64[ ((in[0] & 0x03)
<< 4) | ((in[1] &
0xf0) >> 4) ];

    out[2] =
(unsigned char) (len > 1 ? cb64[ ((in[1]
& 0x0f) << 2) |
((in[2] & 0xc0) >> 6)
] : '=');

    out[3] =
(unsigned char) (len > 2 ? cb64[ in[2]
& 0x3f ] : '=');

}

void encode_base64(OUT char *dst,
IN const char *src, IN int sz )

{

    unsigned
char in[3], *out = (unsigned char *) dst;

    int i,
len;

    while (sz
> 0){

       
len = 0;

       
for (i = 0; i < 3; i++, sz--){

           
if (sz > 0){

               
len++;

               
in[i] = src[i];

           
} else {

               
in[i] = 0;

           
}

       
}

       
src += 3;

       
if (len){

           
encodeblock(in, out, len);

           
out += 4;

       
}

    }

    *out =
'/0';

}

void decode_base64(IN char *src,
IN int src_len, OUT char *dst,OUT int * dst_len)

{

    int j = 0, i
= 0;

    for ( i = 0;
i < src_len; i += 4) {

   
    dst[j++] =
db64[src[i]] << 2 | db64[src[i + 1]]
>> 4;

   
    dst[j++] =
db64[src[i + 1]] << 4 | db64[src[i +
2]] >> 2;

   
    dst[j++] =
db64[src[i + 2]] << 6 | db64[src[i +
3]];

    }

    dst[j] =
'/0';

    * dst_len =
j-1;

}
// end of part1: Base64 演算法
end

相關連結:我的網路通訊相關文章

NTLM的實現:

  • 實現PROXY穿越(16):NTLM的PROXY穿越

  • 實現PROXY穿越(15):NTLM Session Security

  • 實現PROXY穿越(14):NTLM type3 Message
  • 實現PROXY穿越(13):NTLM type2 Message
  • 實現PROXY穿越(12):NTLM type1 Message
  • 實現PROXY穿越(11):NTLMv2 session response
  • 實現PROXY穿越(10):NTLMv2 response
  • 實現PROXY穿越(9):NTLMv1 response
  • 實現PROXY穿越(8):NT-Hash的實現
  • 實現PROXY穿越(7):MD4和MD5
  • 實現PROXY穿越(6):LM-Hash的實現
  • 實現PROXY穿越(5):DES演算法之三
  • 實現PROXY穿越(4):DES演算法之二
  • 實現PROXY穿越(3):DES演算法之一
  • 實現PROXY穿越(2):Base64演算法
  • 實現PROXY穿越(1):流程和NTLM演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.