非对称加密:加密解密分别对应公钥和私钥,或加密解密分别对应私钥和公钥
Android各种加解密
http://www.cnblogs.com/whoislcj/p/5473030.html
RSA(非对称加密):
ECC(非对称加密):
XXTEA(对称加密):MD5(摘要):
SHA1(摘要):
数据传输过程:先用非对称加密进行密钥交换,再用对称加密进行数据传输
密钥协商过程:
message CSTalk}
C: Client
S: Server
C一开始保存在本地的数据:RSA公钥、加密密钥Key1
1、C发送:RSApub(XXTEA密钥Key1),即:服务端公钥加密的动态密钥; S返回:sessionidB和RSA公钥pubB (sessionidB在服务端会对应一个RSA私钥priB,一个RSA公钥pubB)
2、C发送:用RSA公钥pubB进行XXTEA加密的数据和sessionidB; S收到:取得对应sessionidB对应的RSA私钥priB,对数据进行解密
以下2种,本质是一样的:
伤不起的算法-ECC-ECDH
http://blog.sina.com.cn/s/blog_55a9111c01011zxz.html
过程:
椭圆曲线:公开(E, N, G)
Client:
第一步:a(私1) = random
第一步:A(公1)= a(私1) * G
第二步:Q2 = a(私1) * B(公2)
Server:
第一步:b(私2) = random
第一步:B(公2) = b(私2) * G
第二步:Q1 = b(私2) * A(公1)
第一步: Client->Server 传输 A(公1)
第二步: Server->Client 传输 B(公2)
用于传输数据的密钥:Q1=Q2
DH密钥交换算法
http://blog.csdn.net/fw0124/article/details/8462373
非对称加密算法-DH算法
http://blog.csdn.net/kongqz/article/details/6302913
过程:
Client:
第一步:已有Pu1(公1)
第二步:产生一对Pr2(私2)和Pu2(公2),Client->Server 传输 Pu2(公2)
第三步:Q1 = Pu1 + Pr2
Server:
第一步:已有Pr1(私1)
第二步:接收 Pu2(公2)
第三步:Q2 = Pr1 + Pu2
用于传输数据的密钥:Q1=Q2
参考:
ECC加密算法入门介绍
http://www.pediy.com/kssd/pediy06/pediy6014.htm
椭圆曲线密码学简介
http://www.8btc.com/introduction
RSA与ECC比较
http://blog.163.com/aining_li@126/blog/static/67536753201162245932189/
Java加密技术(七)——非对称加密算法最高级ECC
http://snowolf.iteye.com/blog/383412
代码示例:
参考:ECC加密算法实现代码
ecies.py
import hashlib
from openssl_wrapper import OpenSSL
from struct import pack, unpack
#1. Generate a ephemeral EC key pair
# we use shortand patent free EC curve in the QR scenerio, which is 'secp160r1'
PUBKEY='02c500146ec52c4fdae22c1c18d81b3679cf70397fdaab8f0014bac0efaff80ce2ec719ba1bcaa53530bf03d6d95'
PRIVKEY='02c500147bd49a3ea78ba2d5a06920f4326e693002419685'
#PUBKEY=PUBKEY.decode('hex')
#PRIVKEY=PRIVKEY.decode('hex')
def gen_ec_keypair(curve='secp112r1'):
#this function generates EC key pair
try:
curve=OpenSSL.curves[curve]
key = OpenSSL.EC_KEY_new_by_curve_name(curve)
OpenSSL.EC_KEY_generate_key(key)
_pubkey_x = OpenSSL.BN_new()
_pubkey_y = OpenSSL.BN_new()
_privkey = OpenSSL.EC_KEY_get0_private_key(key)
_pubkey = OpenSSL.EC_KEY_get0_public_key(key)
_group = OpenSSL.EC_KEY_get0_group(key)
OpenSSL.EC_POINT_get_affine_coordinates_GFp(_group, _pubkey, _pubkey_x, _pubkey_y, 0)
privkey = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(_privkey))
pubkeyx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(_pubkey_x))
pubkeyy = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(_pubkey_y))
OpenSSL.BN_bn2bin(_privkey, privkey)
privkey = privkey.raw
OpenSSL.BN_bn2bin(_pubkey_x, pubkeyx)
pubkeyx = pubkeyx.raw
OpenSSL.BN_bn2bin(_pubkey_y, pubkeyy)
pubkeyy = pubkeyy.raw
#self.raw_check_key(privkey, pubkeyx, pubkeyy)
full_privkey=pack('!H', curve) + pack('!H', len(privkey)) + privkey
full_pubkey=pack('!H', curve) + pack('!H', len(pubkeyx)) + pubkeyx + pack('!H', len(pubkeyy)) + pubkeyy
return full_privkey, full_pubkey
finally:
#release c pointers
OpenSSL.EC_KEY_free(key)
OpenSSL.BN_free(_pubkey_x)
OpenSSL.BN_free(_pubkey_y)
def ecdh_key(a_privkey, b_pubkey):
#keys should be in binary format
a_curve=int(a_privkey[0:2].encode('hex'), 16)
b_curve=int(b_pubkey[0:2].encode('hex'), 16)
if a_curve != b_curve:
raise Exception("ECDH Error: Both key must have the save curve type.")
sx=int(b_pubkey[2:4].encode('hex'), 16)
sy=int(b_pubkey[4+sx:sx+6].encode('hex'), 16)
pub_x, pub_y = b_pubkey[4:4+sx], b_pubkey[6+sx:6+sx+sy]
b_key=OpenSSL.EC_KEY_new_by_curve_name(b_curve)
_pub_x=OpenSSL.BN_bin2bn(pub_x, sx, 0)
_pub_y=OpenSSL.BN_bin2bn(pub_y, sy, 0)
_group=OpenSSL.EC_KEY_get0_group(b_key)
_pubkey=OpenSSL.EC_POINT_new(_group)
OpenSSL.EC_POINT_set_affine_coordinates_GFp(_group, _pubkey, _pub_x, _pub_y, 0)
OpenSSL.EC_KEY_set_public_key(b_key, _pubkey)
#OpenSSL.EC_KEY_check_key(b_key)
s=int(a_privkey[2:4].encode('hex'), 16)
priv=a_privkey[4:4+s]
a_key=OpenSSL.EC_KEY_new_by_curve_name(a_curve)
_privkey=OpenSSL.BN_bin2bn(priv, len(priv), 0)
OpenSSL.EC_KEY_set_private_key(a_key, _privkey)
#ECDH
OpenSSL.ECDH_set_method(a_key, OpenSSL.ECDH_OpenSSL())
ecdh_buf = OpenSSL.malloc(0, s) #computed buffer size should the same as key length
ecdh_keylen=OpenSSL.ECDH_compute_key(ecdh_buf, s, _pubkey, a_key, 0)
return ecdh_buf.raw
from Crypto.Cipher import AES
def encrypt(a_privkey, a_pubkey, b_pubkey, content):
ecdh1=ecdh_key(a_privkey,b_pubkey)
shared_key=hashlib.md5(ecdh1).digest() #we need 128 bit key in our QR scenerio
obj = AES.new(shared_key, AES.MODE_ECB)
ciphertext = obj.encrypt(content)
qr=(a_pubkey+ciphertext).encode('base64')
return qr
def decrypt(b_privkey, qr):
qr=qr.decode('base64')
sx=int(qr[2:4].encode('hex'), 16)
sy=int(qr[4+sx:sx+6].encode('hex'), 16)
a_pubkey=qr[:6+sx+sy]
ciphertext=qr[6+sx+sy:]
ecdh2=ecdh_key(b_privkey,a_pubkey)
shared_key=hashlib.md5(ecdh2).digest()
obj = AES.new(shared_key, AES.MODE_ECB)
content = obj.decrypt(ciphertext)
return content
a= gen_ec_keypair('secp112r2') #a key is ephemeral, generated everytime
b=gen_ec_keypair('secp112r2') #in real code, this key pair should come from server, and a client has its public key only
print 'Generated EC pairs for ECDH key exchange:'
print 'Alice -> Private:'+ a[0].encode('hex'), 'Public:' + a[1].encode('hex')
print 'Bob -> Private:' + b[0].encode('hex'), 'Public:' + b[1].encode('hex')
e1= ecdh_key(a[0],b[1])
e2=ecdh_key(b[0],a[1]) #e2 is for server code only
print e1.encode('hex')
print e2.encode('hex')
bm=buffer('Hello there DAI!')
qr= encrypt(a[0],a[1],b[1],bm)
print 'QR code size is', len(qr), ':', qr
#import qrcode
#qrcode.make(qr).show()
#server does the following:
content=decrypt(b[0], qr)
print 'After decryption, content is ', content
openssl_wrapper.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 Yann GUIBET
# See LICENSE for details.
import sys
import ctypes
import ctypes.util
OpenSSL = None
class CipherName:
def __init__(self, name, pointer, blocksize):
self._name = name
self._pointer = pointer
self._blocksize = blocksize
def __str__(self):
return "Cipher : " + self._name + " | Blocksize : " + str(self._blocksize) + " | Fonction pointer : " + str(self._pointer)
def get_pointer(self):
return self._pointer()
def get_name(self):
return self._name
def get_blocksize(self):
return self._blocksize
class _OpenSSL:
"""
Wrapper for OpenSSL using ctypes
"""
def __init__(self, library):
"""
Build the wrapper
"""
self._lib = ctypes.CDLL(library)
self.pointer = ctypes.pointer
self.c_int = ctypes.c_int
self.byref = ctypes.byref
self.create_string_buffer = ctypes.create_string_buffer
self.BN_new = self._lib.BN_new
self.BN_new.restype = ctypes.c_void_p
self.BN_new.argtypes = []
self.BN_free = self._lib.BN_free
self.BN_free.restype = None
self.BN_free.argtypes = [ctypes.c_void_p]
self.BN_num_bits = self._lib.BN_num_bits
self.BN_num_bits.restype = ctypes.c_int
self.BN_num_bits.argtypes = [ctypes.c_void_p]
self.BN_bn2bin = self._lib.BN_bn2bin
self.BN_bn2bin.restype = ctypes.c_int
self.BN_bn2bin.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
self.BN_bin2bn = self._lib.BN_bin2bn
self.BN_bin2bn.restype = ctypes.c_void_p
self.BN_bin2bn.argtypes = [ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p]
self.EC_KEY_free = self._lib.EC_KEY_free
self.EC_KEY_free.restype = None
self.EC_KEY_free.argtypes = [ctypes.c_void_p]
self.EC_KEY_new_by_curve_name = self._lib.EC_KEY_new_by_curve_name
self.EC_KEY_new_by_curve_name.restype = ctypes.c_void_p
self.EC_KEY_new_by_curve_name.argtypes = [ctypes.c_int]
self.EC_KEY_generate_key = self._lib.EC_KEY_generate_key
self.EC_KEY_generate_key.restype = ctypes.c_int
self.EC_KEY_generate_key.argtypes = [ctypes.c_void_p]
self.EC_KEY_check_key = self._lib.EC_KEY_check_key
self.EC_KEY_check_key.restype = ctypes.c_int
self.EC_KEY_check_key.argtypes = [ctypes.c_void_p]
self.EC_KEY_get0_private_key = self._lib.EC_KEY_get0_private_key
self.EC_KEY_get0_private_key.restype = ctypes.c_void_p
self.EC_KEY_get0_private_key.argtypes = [ctypes.c_void_p]
self.EC_KEY_get0_public_key = self._lib.EC_KEY_get0_public_key
self.EC_KEY_get0_public_key.restype = ctypes.c_void_p
self.EC_KEY_get0_public_key.argtypes = [ctypes.c_void_p]
self.EC_KEY_get0_group = self._lib.EC_KEY_get0_group
self.EC_KEY_get0_group.restype = ctypes.c_void_p
self.EC_KEY_get0_group.argtypes = [ctypes.c_void_p]
self.EC_POINT_get_affine_coordinates_GFp = self._lib.EC_POINT_get_affine_coordinates_GFp
self.EC_POINT_get_affine_coordinates_GFp.restype = ctypes.c_int
self.EC_POINT_get_affine_coordinates_GFp.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
self.EC_KEY_set_private_key = self._lib.EC_KEY_set_private_key
self.EC_KEY_set_private_key.restype = ctypes.c_int
self.EC_KEY_set_private_key.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
self.EC_KEY_set_public_key = self._lib.EC_KEY_set_public_key
self.EC_KEY_set_public_key.restype = ctypes.c_int
self.EC_KEY_set_public_key.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
self.EC_KEY_set_group = self._lib.EC_KEY_set_group
self.EC_KEY_set_group.restype = ctypes.c_int
self.EC_KEY_set_group.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
self.EC_POINT_set_affine_coordinates_GFp = self._lib.EC_POINT_set_affine_coordinates_GFp
self.EC_POINT_set_affine_coordinates_GFp.restype = ctypes.c_int
self.EC_POINT_set_affine_coordinates_GFp.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
self.EC_POINT_new = self._lib.EC_POINT_new
self.EC_POINT_new.restype = ctypes.c_void_p
self.EC_POINT_new.argtypes = [ctypes.c_void_p]
self.EC_POINT_free = self._lib.EC_POINT_free
self.EC_POINT_free.restype = None
self.EC_POINT_free.argtypes = [ctypes.c_void_p]
self.EC_KEY_set_private_key = self._lib.EC_KEY_set_private_key
self.EC_KEY_set_private_key.restype = ctypes.c_int
self.EC_KEY_set_private_key.argtypes = [ctypes.c_void_p,
ctypes.c_void_p]
self.ECDH_OpenSSL = self._lib.ECDH_OpenSSL
self._lib.ECDH_OpenSSL.restype = ctypes.c_void_p
self._lib.ECDH_OpenSSL.argtypes = []
self.ECDH_set_method = self._lib.ECDH_set_method
self._lib.ECDH_set_method.restype = ctypes.c_int
self._lib.ECDH_set_method.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
self.ECDH_compute_key = self._lib.ECDH_compute_key
self.ECDH_compute_key.restype = ctypes.c_int
self.ECDH_compute_key.argtypes = [ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p]
self.EVP_CipherInit_ex = self._lib.EVP_CipherInit_ex
self.EVP_CipherInit_ex.restype = ctypes.c_int
self.EVP_CipherInit_ex.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p]
self.EVP_CIPHER_CTX_new = self._lib.EVP_CIPHER_CTX_new
self.EVP_CIPHER_CTX_new.restype = ctypes.c_void_p
self.EVP_CIPHER_CTX_new.argtypes = []
# Cipher
self.EVP_aes_128_cfb128 = self._lib.EVP_aes_128_cfb128
self.EVP_aes_128_cfb128.restype = ctypes.c_void_p
self.EVP_aes_128_cfb128.argtypes = []
self.EVP_aes_256_cfb128 = self._lib.EVP_aes_256_cfb128
self.EVP_aes_256_cfb128.restype = ctypes.c_void_p
self.EVP_aes_256_cfb128.argtypes = []
self.EVP_aes_128_cbc = self._lib.EVP_aes_128_cbc
self.EVP_aes_128_cbc.restype = ctypes.c_void_p
self.EVP_aes_128_cbc.argtypes = []
self.EVP_aes_256_cbc = self._lib.EVP_aes_256_cbc
self.EVP_aes_256_cbc.restype = ctypes.c_void_p
self.EVP_aes_256_cbc.argtypes = []
# self.EVP_aes_128_ctr = self._lib.EVP_aes_128_ctr
# self.EVP_aes_128_ctr.restype = ctypes.c_void_p
# self.EVP_aes_128_ctr.argtypes = []
# self.EVP_aes_256_ctr = self._lib.EVP_aes_256_ctr
# self.EVP_aes_256_ctr.restype = ctypes.c_void_p
# self.EVP_aes_256_ctr.argtypes = []
self.EVP_aes_128_ofb = self._lib.EVP_aes_128_ofb
self.EVP_aes_128_ofb.restype = ctypes.c_void_p
self.EVP_aes_128_ofb.argtypes = []
self.EVP_aes_256_ofb = self._lib.EVP_aes_256_ofb
self.EVP_aes_256_ofb.restype = ctypes.c_void_p
self.EVP_aes_256_ofb.argtypes = []
self.EVP_bf_cbc = self._lib.EVP_bf_cbc
self.EVP_bf_cbc.restype = ctypes.c_void_p
self.EVP_bf_cbc.argtypes = []
self.EVP_bf_cfb64 = self._lib.EVP_bf_cfb64
self.EVP_bf_cfb64.restype = ctypes.c_void_p
self.EVP_bf_cfb64.argtypes = []
self.EVP_rc4 = self._lib.EVP_rc4
self.EVP_rc4.restype = ctypes.c_void_p
self.EVP_rc4.argtypes = []
self.EVP_CIPHER_CTX_cleanup = self._lib.EVP_CIPHER_CTX_cleanup
self.EVP_CIPHER_CTX_cleanup.restype = ctypes.c_int
self.EVP_CIPHER_CTX_cleanup.argtypes = [ctypes.c_void_p]
self.EVP_CIPHER_CTX_free = self._lib.EVP_CIPHER_CTX_free
self.EVP_CIPHER_CTX_free.restype = None
self.EVP_CIPHER_CTX_free.argtypes = [ctypes.c_void_p]
self.EVP_CipherUpdate = self._lib.EVP_CipherUpdate
self.EVP_CipherUpdate.restype = ctypes.c_int
self.EVP_CipherUpdate.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int]
self.EVP_CipherFinal_ex = self._lib.EVP_CipherFinal_ex
self.EVP_CipherFinal_ex.restype = ctypes.c_int
self.EVP_CipherFinal_ex.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p]
self.EVP_DigestInit = self._lib.EVP_DigestInit
self.EVP_DigestInit.restype = ctypes.c_int
self._lib.EVP_DigestInit.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
self.EVP_DigestUpdate = self._lib.EVP_DigestUpdate
self.EVP_DigestUpdate.restype = ctypes.c_int
self.EVP_DigestUpdate.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_int]
self.EVP_DigestFinal = self._lib.EVP_DigestFinal
self.EVP_DigestFinal.restype = ctypes.c_int
self.EVP_DigestFinal.argtypes = [ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p]
self.EVP_ecdsa = self._lib.EVP_ecdsa
self._lib.EVP_ecdsa.restype = ctypes.c_void_p
self._lib.EVP_ecdsa.argtypes = []
self.ECDSA_sign = self._lib.ECDSA_sign
self.ECDSA_sign.restype = ctypes.c_int
self.ECDSA_sign.argtypes = [ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
self.ECDSA_verify = self._lib.ECDSA_verify
self.ECDSA_verify.restype = ctypes.c_int
self.ECDSA_verify.argtypes = [ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
self.EVP_MD_CTX_create = self._lib.EVP_MD_CTX_create
self.EVP_MD_CTX_create.restype = ctypes.c_void_p
self.EVP_MD_CTX_create.argtypes = []
self.EVP_MD_CTX_init = self._lib.EVP_MD_CTX_init
self.EVP_MD_CTX_init.restype = None
self.EVP_MD_CTX_init.argtypes = [ctypes.c_void_p]
self.EVP_MD_CTX_destroy = self._lib.EVP_MD_CTX_destroy
self.EVP_MD_CTX_destroy.restype = None
self.EVP_MD_CTX_destroy.argtypes = [ctypes.c_void_p]
self.RAND_bytes = self._lib.RAND_bytes
self.RAND_bytes.restype = None
self.RAND_bytes.argtypes = [ctypes.c_void_p, ctypes.c_int]
self.EVP_sha256 = self._lib.EVP_sha256
self.EVP_sha256.restype = ctypes.c_void_p
self.EVP_sha256.argtypes = []
self.EVP_sha512 = self._lib.EVP_sha512
self.EVP_sha512.restype = ctypes.c_void_p
self.EVP_sha512.argtypes = []
self.HMAC = self._lib.HMAC
self.HMAC.restype = ctypes.c_void_p
self.HMAC.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int,
ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p]
# self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC
# self.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int
# self.PKCS5_PBKDF2_HMAC.argtypes = [ctypes.c_void_p, ctypes.c_int,
# ctypes.c_void_p, ctypes.c_int,
# ctypes.c_int, ctypes.c_void_p,
# ctypes.c_int, ctypes.c_void_p]
self._set_ciphers()
self._set_curves()
def _set_ciphers(self):
self.cipher_algo = {
'aes-128-cbc': CipherName('aes-128-cbc', self.EVP_aes_128_cbc, 16),
'aes-256-cbc': CipherName('aes-256-cbc', self.EVP_aes_256_cbc, 16),
'aes-128-cfb': CipherName('aes-128-cfb', self.EVP_aes_128_cfb128, 16),
'aes-256-cfb': CipherName('aes-256-cfb', self.EVP_aes_256_cfb128, 16),
'aes-128-ofb': CipherName('aes-128-ofb', self._lib.EVP_aes_128_ofb, 16),
'aes-256-ofb': CipherName('aes-256-ofb', self._lib.EVP_aes_256_ofb, 16),
#'aes-128-ctr': CipherName('aes-128-ctr', self._lib.EVP_aes_128_ctr, 16),
#'aes-256-ctr': CipherName('aes-256-ctr', self._lib.EVP_aes_256_ctr, 16),
'bf-cfb': CipherName('bf-cfb', self.EVP_bf_cfb64, 8),
'bf-cbc': CipherName('bf-cbc', self.EVP_bf_cbc, 8),
'rc4': CipherName('rc4', self.EVP_rc4, 128), # 128 is the initialisation size not block size
}
def _set_curves(self):
self.curves = {
'secp112r1': 704,
'secp112r2': 705,
'secp128r1': 706,
'secp128r2': 707,
'secp160k1': 708,
'secp160r1': 709,
'secp160r2': 710,
'secp192k1': 711,
'secp224k1': 712,
'secp224r1': 713,
'secp256k1': 714,
'secp384r1': 715,
'secp521r1': 716,
'sect113r1': 717,
'sect113r2': 718,
'sect131r1': 719,
'sect131r2': 720,
'sect163k1': 721,
'sect163r1': 722,
'sect163r2': 723,
'sect193r1': 724,
'sect193r2': 725,
'sect233k1': 726,
'sect233r1': 727,
'sect239k1': 728,
'sect283k1': 729,
'sect283r1': 730,
'sect409k1': 731,
'sect409r1': 732,
'sect571k1': 733,
'sect571r1': 734,
'prime256v1': 415,
}
def BN_num_bytes(self, x):
"""
returns the length of a BN (OpenSSl API)
"""
return int((self.BN_num_bits(x) + 7) / 8)
def get_cipher(self, name):
"""
returns the OpenSSL cipher instance
"""
if name not in self.cipher_algo:
raise Exception("Unknown cipher")
return self.cipher_algo[name]
def get_curve(self, name):
"""
returns the id of a elliptic curve
"""
if name not in self.curves:
raise Exception("Unknown curve")
return self.curves[name]
def get_curve_by_id(self, id):
"""
returns the name of a elliptic curve with his id
"""
res = None
for i in self.curves:
if self.curves[i] == id:
res = i
break
if res is None:
raise Exception("Unknown curve")
return res
def rand(self, size):
"""
OpenSSL random function
"""
buffer = self.malloc(0, size)
self.RAND_bytes(buffer, size)
return buffer.raw
def malloc(self, data, size):
"""
returns a create_string_buffer (ctypes)
"""
buffer = None
if data != 0:
if sys.version_info.major == 3 and isinstance(data, type('')):
data = data.encode()
buffer = self.create_string_buffer(data, size)
else:
buffer = self.create_string_buffer(size)
return buffer
libname = ctypes.util.find_library('crypto')
if libname is None:
# For Windows ...
libname = ctypes.util.find_library('libeay32.dll')
if libname is None:
raise Exception("Couldn't load OpenSSL lib ...")
OpenSSL = _OpenSSL(libname)
问题:找不到libeay32.dll
libeay32.dll 脚本
http://download.csdn.net/detail/huxijiuhao/7825659
libeay32.dll控件常规安装方法(仅供参考):
一、如果在运行某软件或编译程序时提示缺少、找不到libeay32.dll等类似提示,您可将从脚本之家下载来的libeay32.dll拷贝到指定目录即可(一般是system系统目录或放到软件同级目录里面),或者重新添加文件引用。
二、您从我们网站下载下来文件之后,先将其解压(一般都是rar压缩包), 然后根据您系统的情况选择X86/X64,X86为32位电脑,X64为64位电脑。默认都是支持32位系统的, 如果您不知道是X86还是X64,您可以看这篇文章。
三、根据软件情况选择文件版本。此步骤比较复杂,如果是Windows的dll文件,
版本号以5.0开头的或含有 nt 一般是windows2000的文件。
版本号以5.1开头的或含有 xp、xpsp1、xpsp2、xpsp3 信息的一般是windowsXP的文件。
版本号以6.0开头的或含有 longhorn、vista 信息的一般是windowsVista的文件。
版本号以6.1开头的或含有 win7 信息的一般是windows7的文件。 如果不是windows的dll文件,则需要灵活查看版本号、描述、网友提供的信息、以及相关dll的版本号去判断。
四、直接拷贝该文件到系统目录里:
1、Windows 95/98/Me系统,将libeay32.dll复制到C:\Windows\System目录下。
2、Windows NT/2000系统,将libeay32.dll复制到C:\WINNT\System32目录下。
3、Windows XP/WIN7/Vista系统,将libeay32.dll复制到C:\Windows\System32目录下。
4、如果您的系统是64位的请将文件复制到C:\Windows\SysWOW64目录
五、打开"开始-运行-输入regsvr32 libeay32.dll",回车即可解决。希望脚本之家为您提供的libeay32.dll对您有所帮助。
问题:ImportError: No module named Crypto.Cipher
安装pycrypto要用easy_install
pip uninstall pycrypto
easy_install pycrypto
参考:http://stackoverflow.com/questions/19623267/importerror-no-module-named-crypto-cipher