Cipher



import time


def trunk_test(name, cipher, trunk):
    print name
    trunk = trunk[len(trunk)%cipher.block_size:]
    print "trunk size ", len(trunk)
    
    key_size = 16 if cipher.key_size < 1 else cipher.key_size
    key = "0" * key_size
    
    cipher_obj = cipher.new(key)
    t0 = time.time()
    encrypted = cipher_obj.encrypt(trunk)
    t1 = time.time()
    cost = t1 - t0
    print "encrypt cost", cost, "avg", len(trunk) / cost / (1024*1024), "MB", "per sec"
    
    cipher_obj = cipher.new(key)
    t0 = time.time()
    encrypted = cipher_obj.decrypt(trunk)
    t1 = time.time()
    cost = t1 - t0
    print "decrypt cost", cost, "avg", len(trunk) / cost / (1024*1024), "MB", "per sec"
    
def block_test(name, cipher, loop):
    print name
    block_size = 16 if cipher.block_size < 4 else cipher.block_size
    block = "0" * block_size
    print "block size", block_size
    
    key_size = 16 if cipher.key_size < 1 else cipher.key_size
    key = "0" * key_size
    
    cipher_obj = cipher.new(key)
    t0 = time.time()
    for i in xrange(0, loop):
        encrypted = cipher_obj.encrypt(block)
    t1 = time.time()
    cost = t1 - t0
    print "encrypt cost", cost, "avg", int(loop / cost), "per sec"
    
    cipher_obj = cipher.new(key)
    t0 = time.time()
    for i in xrange(0, loop):
        encrypted = cipher_obj.decrypt(block)
    t1 = time.time()
    cost = t1 - t0
    print "decrypt cost", cost, "avg", int(loop / cost), "per sec"
    
from Crypto.Cipher import DES
from Crypto.Cipher import DES3
from Crypto.Cipher import Blowfish
from Crypto.Cipher import ARC4
from Crypto.Cipher import AES


ciphers = (("DES", DES), ("DES3", DES3), ("Blowfish", Blowfish), ("ARC4", ARC4), ("AES", AES))


trunk = "0" * (1024*1024*10)
for cipher_name, cipher in ciphers:
    trunk_test(cipher_name, cipher, trunk)


print
loop = 10000
for cipher_name, cipher in ciphers:
    block_test(cipher_name, cipher, loop)


DES

trunk size  10485760
encrypt cost 0.401318073273 avg 24.9178909847 MB per sec
decrypt cost 0.403321027756 avg 24.7941448916 MB per sec
DES3
trunk size  10485760
encrypt cost 1.15098285675 avg 8.68822671107 MB per sec
decrypt cost 1.15614390373 avg 8.64944231226 MB per sec
Blowfish
trunk size  10485760
encrypt cost 0.234624862671 avg 42.6212289958 MB per sec
decrypt cost 0.23650097847 avg 42.2831231596 MB per sec
ARC4
trunk size  10485760
encrypt cost 0.194773197174 avg 51.3417664498 MB per sec
decrypt cost 0.196748971939 avg 50.8261867975 MB per sec
AES
trunk size  10485760
encrypt cost 0.211192846298 avg 47.3500886762 MB per sec
decrypt cost 0.208957195282 avg 47.8566913501 MB per sec


DES
block size 8
encrypt cost 0.00846099853516 avg 1181893 per sec
decrypt cost 0.00875186920166 avg 1142613 per sec
DES3
block size 8
encrypt cost 0.0141088962555 avg 708772 per sec
decrypt cost 0.0141921043396 avg 704617 per sec
Blowfish
block size 8
encrypt cost 0.00711393356323 avg 1405692 per sec
decrypt cost 0.00703597068787 avg 1421268 per sec
ARC4
block size 16
encrypt cost 0.00886487960815 avg 1128046 per sec
decrypt cost 0.00869393348694 avg 1150227 per sec
AES
block size 16
encrypt cost 0.00845193862915 avg 1183160 per sec

decrypt cost 0.00836610794067 avg 1195298 per sec


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值