python模塊app登陸認證(M2Crypto數字證書加密)
需求:
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中支持的格式如下表:
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> | </tr> </tbody> </table> |
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) |