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 mysql-devel libpcap-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=/usr/local/swig3.0.7/bin:$PATH:/usr/local/python/2.7.10/bin:/usr/local/pcre/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
或者yum安装
yum install -y pcre pcre-devel
5、安装swig
下载地址:http://www.swig.org/download.html
./configure --prefix=/usr/local/swig3.0.7
make
make install
查看版本信息swig -version,如果版本信息与安装的不匹配,which swig,看下当前swig程序的路径,如果不是自己安装的路径,修改环境变量。
6、安装M2Crypto
下载地址:https://pypi.python.org/pypi/M2Crypto/0.22.5
cp /usr/include/openssl/opensslconf-x86_64.h ./
python setup.py install
M2Crypto模块安装步骤(Ubuntu 16.04.1环境)
1、基础安装
apt-get install g++ build-essential make
2、安装python2.7
apt-get install python2.7
3、安装pcre
apt-get install libpcre3 libpcre3-dev
4、安装swig
下载地址 http://www.swig.org/download.html
./configure --prefix=/usr/local/swig3.0.7
make
make install
配置环境变量
vi /root/.bashrc
export PATH=/usr/local/swig3.0.7/bin:$PATH
5、安装M2Crypto
apt-get install python-m2crypto
struc模块说明
struct模块中最重要的三个函数是pack(), unpack(), calcsize()
pack(fmt, v1, v2, ...) 按照给定的格式(fmt),把数据封装成字符串(实际上是类似于c结构体的字节流)
unpack(fmt, string) 按照给定的格式(fmt)解析字节流string,返回解析出来的tuple
calcsize(fmt) 计算给定的格式(fmt)占用多少字节的内存
struct中支持的格式如下表:
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
Format |
C Type |
Python |
字节数 |
x |
pad byte |
no value |
1 |
c |
char |
string of length 1 |
1 |
b |
signed char |
integer |
1 |
B |
unsigned char |
integer |
1 |
? |
_Bool |
bool |
1 |
h |
short |
integer |
2 |
H |
unsigned short |
integer |
2 |
i |
int |
integer |
4 |
I |
unsigned int |
integer or long |
4 |
l |
long |
integer |
4 |
L |
unsigned long |
long |
4 |
q |
long long |
long |
8 |
Q |
unsigned long long |
long |
8 |
f |
float |
float |
4 |
d |
double |
float |
8 |
s |
char[] |
string |
1 |
p |
char[] |
string |
1 |
P |
void * |
long |
注1.q和Q只在机器支持64位操作时有意思
注2.每个格式前可以有一个数字,表示个数
注3.s格式表示一定长度的字符串,4s表示长度为4的字符串,但是p表示的是pascal字符串
注4.P用来转换一个指针,其长度和机器字长相关
注5.最后一个可以用来表示指针类型的,占4个字节
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
为了同c中的结构体交换数据,还要考虑有的c或c++编译器使用了字节对齐,通常是以4个字节为单位的32位系统,故而struct根据本地机器字节顺序转换.可以用格式中的第一个字符来改变对齐方式.定义如下:
Character |
Byte order |
Size and alignment |
@ |
native |
native |
= |
native |
standard |
< |
little-endian |
standard |
> |
big-endian |
standard |
! |
network (= big-endian) |
standard |
使用方法是放在fmt的第一个位置,就像'@5s6sif'
数字证书加密代码
from M2Crypto import X509, EVP, RSA, ASN1, BIO def random_key():
# 随机秘钥(8位)
checkcode = ''
for i in range(8):
current = random.randrange(0, 8)
if current != i:
temp = chr(random.randint(65, 90))
else:
temp = random.randint(0, 9)
checkcode += str(temp)
return checkcode key = random_key()
encrypted_key = RSA.load_pub_key("rsa_public.key")
encrypted = encrypted_key.public_encrypt(key, RSA.pkcs1_padding)
M2Crypto使用 http://www.heikkitoivonen.net/m2crypto/api/
字节流组包代码
def header(body,datasize,isprocess,processsize,msgId):
#结构体包头
#ZipHeader
datasize_len = datasize
isprocess_num = isprocess
processsize_len = processsize
datasize = struct.pack('i', datasize_len)
isprocess = struct.pack('h', isprocess_num)
processsize = struct.pack('i', processsize_len)
ZipHeader = datasize+isprocess+processsize+body #RequestHeader
pkgSize_len = len(ZipHeader)
msgId = msgId
ownerId = 0
magicId = 0
RequestHeader = struct.pack('3Ih%ds' % pkgSize_len, pkgSize_len, msgId, ownerId, magicId, ZipHeader)
return RequestHeader
字节流参考博客:http://blog.csdn.net/jrckkyy/article/details/38816565
python模块app登陆认证(M2Crypto数字证书加密)的更多相关文章
- Java使用数字证书加密通信(加解密/加签验签)
本文中使用的Base64Utils.java可参考:http://www.cnblogs.com/shindo/p/6346618.html 证书制作方法可参考:http://www.cnblogs. ...
- Python学习之登陆认证
需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 (提示,通过列表存多个账户信息) 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提 ...
- 使用X.509数字证书加密解密实务(三)-- 使用RSA证书结合对称加密技术加密长数据
一. 使用证书结合对称加密算法加.解密长数据 上一章节讨论了如何使用RSA证书加密数据,文中提到:“Dotnet的RSA实现有个特点,它必须要在明文中添加一些随机数,所以明文不能把128字节占满,实 ...
- 使用X.509数字证书加密解密实务(二)-- 使用RSA证书加密敏感数据
一. 使用RSA证书加.解密敏感数据 X.509证书标准支持三种不对称加密算法:RSA, DSA, Diffie-Hellman algorithms.最常用的是RSA算法.所以本文就以前面章节使用 ...
- 使用X.509数字证书加密解密实务(一)-- 证书的获得和管理
http://www.cnblogs.com/chnking/archive/2007/08/18/860983.html
- 基于SSL协议的双向认证 - 数字证书 [2]
1.1 数字证书 1.1.1 概念理解 一种文件的名称,例如一个机构或人的签名,能够证明这个机构或人的真实性.简而言之数字证书是一种网络上证明持有者身份的文件,同时还包括有公钥.证书是由国际 ...
- RSA原理、ssl认证、Tomcat中配置数字证书以及网络传输数据中的密码学知识
情形一:接口的加.解密与加.验签 rsa不是只有加密解密,除此外还有加签和验签.之前一直误以为加密就是加签,解密就是验签.这是错误的! 正确的理解是: 数据传输的机密性:公钥加密私钥解密是密送,保 ...
- 数字证书、SSL、HTTPS及在Nginx中的配置
一.什么是 RSA.SSL.HTTPS RSA:它是非对称加密算法的一种,而且是最常用的一种.它的理论基础是:计算两个大质数的乘积非常简单,而对该乘积进行因子分解就非常困难.而且 这两个质数越大,对其 ...
- [转载]JavaEE学习篇之——网络传输数据中的密码学知识以及Tomcat中配置数字证书EE
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/21716557 今天是学习JavaWeb的第二天,我们来了解什么呢?就了解一 ...
随机推荐
- flash
1. 1.这种方式已经比较旧了, 2. html.push('<div class="flash-ad" style = "position:relative&qu ...
- 清除webBrowser 缓存和Cookie的解决方案
通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除 解决方案1: publ ...
- vim 插件管理
1 进入自己的vim mkdir ./bundle/vundle 2 在vimrc同级中执行 git clone https://github.com/gmarik/vundle.git ./bund ...
- jquery插件之jquery-validation
equalTo方法: equalTo: function( value, element, param ) { // Bind to the blur event of the target in o ...
- JS 获取上一层目录
派生到我的代码片 <script type="text/javascript"> //返回当前工作目录 function GetCurrDir(){ var pathN ...
- SpringMVC对日期类型的转换
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- 顺序队列C/C++实现
#include <iostream> using namespace std; const int MAXSIZE = 1000; typedef int ELEMTYPE; const ...
- ssh全屏退出的办法
在使用ssh的时候遇到,不知道碰到哪里了,突然xshell就全屏了 解决方法 按下键盘 Alt+Enter就好了
- 自定义jstl标签库
开发环境:Spring+SpringMVC +Maven +Mybatis JSTL 标签库的配置: 导入对应的 jstl.jar 和 standard.jar ,我使用的配置如下: <prop ...
- 100722B
在stack里套set,然后每次根据他的操作,在set里操作,把括号hash,插入,输出set的size-1 #include<iostream> #include<set> ...