Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215608
  • 博文数量: 70
  • 博客积分: 2050
  • 博客等级: 大尉
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-15 21:42
文章分类

全部博文(70)

文章存档

2013年(1)

2011年(5)

2010年(3)

2009年(9)

2008年(17)

2007年(6)

2006年(29)

我的朋友

分类: Python/Ruby

2011-04-02 16:49:09

#!/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) |
给主人留下些什么吧!~~