python模塊app登陸認證(M2Crypto數字證書加密)

jopen 8年前發布 | 13K 次閱讀 Python 數字證書 Python開發

需求:

1、通過數字證書,非對稱加密方式傳送對稱秘鑰給服務端

2、用戶名、密碼使用對稱秘鑰加密,發送服務端驗證

3、傳送數據使用字節流方式

實現思路:

1、了解python的struct模塊,用于字節流組件保溫

2、安裝 M2Crypto 模塊,此模塊依賴第三方軟件swig、openssl

M2Crypto 模塊安裝步驟(centos6.5環境)

1、安裝python2.7.10
yum -y install openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel python-devel zlib libcurl-devel
tar zxvf Python-2.7.10.tgz
mkdir -p /usr/local/python/2.7.10/lib
./configure --enable-shared --prefix=/usr/local/python/2.7.10 LDFLAGS="-Wl,-rpath /usr/local/python/2.7.10/lib"
make
make install
mv /usr/bin/python /usr/bin/python2.6.6
ln -fs /usr/local/python/2.7.10/bin/python2.7 /usr/bin/python
2、配置yum調用python版本
vi /usr/bin/yum
修改成!/usr/bin/python2.6.6
安裝 setuptools、pip
3、配置環境變量
export PATH=$PATH:/usr/local/python/2.7.10/bin:/usr/local/pcre/bin:/usr/local/swig3.0.7/bin
export LD_LIBRARY_PATH=/lib:/usr/local/pcre/lib/
4、安裝PCRE
下載地址:http://www.pcre.org/
./configure --prefix=/usr/local/pcre
make
make install
ln -s /usr/local/pcre/lib/libpcre.so.1 /lib
5、安裝swig
下載地址:http://www.swig.org/download.html
./configure --prefix=/usr/local/swig3.0.7
make
make install
6、安裝M2Crypto
下載地址:https://pypi.python.org/pypi/M2Crypto/0.22.5
cp /usr/include/openssl/opensslconf-x86_64.h ./
python setup.py install

struc模塊說明

struct模塊中最重要的三個函數是pack(), unpack(), calcsize()
pack(fmt, v1, v2, ...)   按照給定的格式(fmt),把數據封裝成字符串(實際上是類似于c結構體的字節流)
unpack(fmt, string)      按照給定的格式(fmt)解析字節流string,返回解析出來的tuple
calcsize(fmt)            計算給定的格式(fmt)占用多少字節的內存

struct中支持的格式如下表:

</tr> </tbody> </table>

注1.q和Q只在機器支持64位操作時有意思

注2.每個格式前可以有一個數字,表示個數

注3.s格式表示一定長度的字符串,4s表示長度為4的字符串,但是p表示的是pascal字符串

注4.P用來轉換一個指針,其長度和機器字長相關

注5.最后一個可以用來表示指針類型的,占4個字節

為了同 c 中的結構體交換數據,還要考慮有的 c 或 c++ 編譯器使用了字節對齊,通常是以 4 個字節為單位的 32 位系統,故而 struct 根據本地機器字節順序轉換 . 可以用格式中的第一個字符來改變對齊方式 . 定義如下:

Format

</td>

C Type

</td>

Python

</td>

字節數

</td> </tr> </thead>

x

</td>

pad byte

</td>

no value

</td>

1

</td> </tr>

c

</td>

char

</td>

string of length 1

</td>

1

</td> </tr>

b

</td>

signed char

</td>

integer

</td>

1

</td> </tr>

B

</td>

unsigned char

</td>

integer

</td>

1

</td> </tr>

?

</td>

_Bool

</td>

bool

</td>

1

</td> </tr>

h

</td>

short

</td>

integer

</td>

2

</td> </tr>

H

</td>

unsigned short

</td>

integer

</td>

2

</td> </tr>

i

</td>

int

</td>

integer

</td>

4

</td> </tr>

I

</td>

unsigned int

</td>

integer or long

</td>

4

</td> </tr>

l

</td>

long

</td>

integer

</td>

4

</td> </tr>

L

</td>

unsigned long

</td>

long

</td>

4

</td> </tr>

q

</td>

long long

</td>

long

</td>

8

</td> </tr>

Q

</td>

unsigned long long

</td>

long

</td>

8

</td> </tr>

f

</td>

float

</td>

float

</td>

4

</td> </tr>

d

</td>

double

</td>

float

</td>

8

</td> </tr>

s

</td>

char[]

</td>

string

</td>

1

</td> </tr>

p

</td>

char[]

</td>

string

</td>

1

</td> </tr>

P

</td>

void *

</td>

long

</td>

Character

</td>

Byte order

</td>

Size and alignment

</td> </tr> </thead>

@

native

</td>

native            湊夠 4 個字節

</span></span></td> </tr>

=

</td>

native

</td>

standard        按原字節數

</span></td> </tr>

<  

</span></span></td>

little-endian

</td>

standard        按原字節數

</span></td> </tr>

>  

</span></span></td>

big-endian

</td>

standard       按原字節數

</span></td> </tr>

!

</td>

network (= big-endian)

</td>

standard       按原字節數

</span></td> </tr> </tbody> </table>

使用方法是放在 fmt 的第一個位置,就像 '@5s6sif'

數字證書加密代碼

  1 from M2Crypto import X509, EVP, RSA, ASN1, BIO
 2 
 3 def random_key():
 4     # 隨機秘鑰(8位)
 5     checkcode = ''
 6     for i in range(8):
 7         current = random.randrange(0, 8)
 8         if current != i:
 9             temp = chr(random.randint(65, 90))
10         else:
11             temp = random.randint(0, 9)
12         checkcode += str(temp)
13     return checkcode
14 
15 key = random_key()
16 encrypted_key = RSA.load_pub_key("rsa_public.key")
17 encrypted = encrypted_key.public_encrypt(key, RSA.pkcs1_padding)

View Code</pre>

M2Crypto 使用 http://www.heikkitoivonen.net/m2crypto/api/

字節流組包代碼

  1 def header(body,datasize,isprocess,processsize,msgId):
 2     #結構體包頭
 3     #ZipHeader
 4     datasize_len = datasize
 5     isprocess_num = isprocess
 6     processsize_len = processsize
 7     datasize = struct.pack('i', datasize_len)
 8     isprocess = struct.pack('h', isprocess_num)
 9     processsize = struct.pack('i', processsize_len)
10     ZipHeader = datasize+isprocess+processsize+body
11 
12     #RequestHeader
13     pkgSize_len = len(ZipHeader)
14     msgId = msgId
15     ownerId = 0
16     magicId = 0
17     RequestHeader = struct.pack('3Ih%ds' % pkgSize_len, pkgSize_len, msgId, ownerId, magicId, ZipHeader)
18     return RequestHeader

View Code</pre>

字節流參考博客:http://blog.csdn.net/jrckkyy/article/details/38816565

</div>

來自: http://www.cnblogs.com/shhnwangjian/p/5110459.html

</span></span></span></span></span></span></span></span></span></span>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!
  • sesese色