#!/usr/bin/python
import socket, ctypes
library = ctypes.cdll.LoadLibrary('/lib/libcrypto.so.0.9.8')
context = ctypes.create_string_buffer(1024)
library.EVP_CIPHER_CTX_init(context)
blowfish = library.EVP_bf_cfb
blowfish.restype = ctypes.c_void_p
blowfish = ctypes.c_void_p(blowfish())
def encrypt(key, iv, message):
library.EVP_EncryptInit_ex(context, blowfish, None, key, iv)
inl = len(message)
outl = ctypes.c_int(inl)
out = ctypes.create_string_buffer(inl)
library.EVP_EncryptUpdate(context, out, ctypes.byref(outl), message, inl)
return out.raw
def decrypt(key, iv, message):
library.EVP_DecryptInit_ex(context, blowfish, None, key, iv)
inl = len(message)
outl = ctypes.c_int(inl)
out = ctypes.create_string_buffer(inl)
library.EVP_DecryptUpdate(context, out, ctypes.byref(outl), message, inl)
return out.raw
key, iv = '1234567890ABCDEF', '12345678'
a = encrypt(key, iv, 'hello')
b = decrypt(key, iv, a)
print b
阅读(817) | 评论(0) | 转发(0) |