📄 test.py
字号:
## test.py : Functions used for testing the modules## Part of the Python Cryptography Toolkit## Distribute and use freely; there are no restrictions on further# dissemination and usage except those imposed by the laws of your# country of residence. This software is provided "as is" without# warranty of fitness for use or suitability for any purpose, express# or implied. Use at your own risk or not at all.#__revision__ = "$Id: test.py,v 1.16 2004/08/13 22:24:18 akuchling Exp $"import binasciiimport stringimport testdatafrom Crypto.Cipher import *def die(string): import sys print '***ERROR: ', string# sys.exit(0) # Will default to continuing onward...def print_timing (size, delta, verbose): if verbose: if delta == 0: print 'Unable to measure time -- elapsed time too small' else: print '%.2f K/sec' % (size/delta) def exerciseBlockCipher(cipher, verbose): import string, time try: ciph = eval(cipher) except NameError: print cipher, 'module not available' return None print cipher+ ':' str='1' # Build 128K of test data for i in xrange(0, 17): str=str+str if ciph.key_size==0: ciph.key_size=16 password = 'password12345678Extra text for password'[0:ciph.key_size] IV = 'Test IV Test IV Test IV Test'[0:ciph.block_size] if verbose: print ' ECB mode:', obj=ciph.new(password, ciph.MODE_ECB) if obj.block_size != ciph.block_size: die("Module and cipher object block_size don't match") text='1234567812345678'[0:ciph.block_size] c=obj.encrypt(text) if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"') text='KuchlingKuchling'[0:ciph.block_size] c=obj.encrypt(text) if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"') text='NotTodayNotEver!'[0:ciph.block_size] c=obj.encrypt(text) if (obj.decrypt(c)!=text): die('Error encrypting "'+text+'"') start=time.time() s=obj.encrypt(str) s2=obj.decrypt(s) end=time.time() if (str!=s2): die('Error in resulting plaintext from ECB mode') print_timing(256, end-start, verbose) del obj if verbose: print ' CFB mode:', obj1=ciph.new(password, ciph.MODE_CFB, IV) obj2=ciph.new(password, ciph.MODE_CFB, IV) start=time.time() ciphertext=obj1.encrypt(str[0:65536]) plaintext=obj2.decrypt(ciphertext) end=time.time() if (plaintext!=str[0:65536]): die('Error in resulting plaintext from CFB mode') print_timing(64, end-start, verbose) del obj1, obj2 if verbose: print ' CBC mode:', obj1=ciph.new(password, ciph.MODE_CBC, IV) obj2=ciph.new(password, ciph.MODE_CBC, IV) start=time.time() ciphertext=obj1.encrypt(str) plaintext=obj2.decrypt(ciphertext) end=time.time() if (plaintext!=str): die('Error in resulting plaintext from CBC mode') print_timing(256, end-start, verbose) del obj1, obj2 if verbose: print ' PGP mode:', obj1=ciph.new(password, ciph.MODE_PGP, IV) obj2=ciph.new(password, ciph.MODE_PGP, IV) start=time.time() ciphertext=obj1.encrypt(str) plaintext=obj2.decrypt(ciphertext) end=time.time() if (plaintext!=str): die('Error in resulting plaintext from PGP mode') print_timing(256, end-start, verbose) del obj1, obj2 if verbose: print ' OFB mode:', obj1=ciph.new(password, ciph.MODE_OFB, IV) obj2=ciph.new(password, ciph.MODE_OFB, IV) start=time.time() ciphertext=obj1.encrypt(str) plaintext=obj2.decrypt(ciphertext) end=time.time() if (plaintext!=str): die('Error in resulting plaintext from OFB mode') print_timing(256, end-start, verbose) del obj1, obj2 def counter(length=ciph.block_size): return length * 'a' if verbose: print ' CTR mode:', obj1=ciph.new(password, ciph.MODE_CTR, counter=counter) obj2=ciph.new(password, ciph.MODE_CTR, counter=counter) start=time.time() ciphertext=obj1.encrypt(str) plaintext=obj2.decrypt(ciphertext) end=time.time() if (plaintext!=str): die('Error in resulting plaintext from CTR mode') print_timing(256, end-start, verbose) del obj1, obj2 # Test the IV handling if verbose: print ' Testing IV handling' obj1=ciph.new(password, ciph.MODE_CBC, IV) plaintext='Test'*(ciph.block_size/4)*3 ciphertext1=obj1.encrypt(plaintext) obj1.IV=IV ciphertext2=obj1.encrypt(plaintext) if ciphertext1!=ciphertext2: die('Error in setting IV') # Test keyword arguments obj1=ciph.new(key=password) obj1=ciph.new(password, mode=ciph.MODE_CBC) obj1=ciph.new(mode=ciph.MODE_CBC, key=password) obj1=ciph.new(IV=IV, mode=ciph.MODE_CBC, key=password) return ciphdef exerciseStreamCipher(cipher, verbose): import string, time try: ciph = eval(cipher) except (NameError): print cipher, 'module not available' return None print cipher + ':', str='1' # Build 128K of test data for i in xrange(0, 17): str=str+str key_size = ciph.key_size or 16 password = 'password12345678Extra text for password'[0:key_size] obj1=ciph.new(password) obj2=ciph.new(password) if obj1.block_size != ciph.block_size: die("Module and cipher object block_size don't match") if obj1.key_size != ciph.key_size: die("Module and cipher object key_size don't match") text='1234567812345678Python' c=obj1.encrypt(text) if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"') text='B1FF I2 A R3A11Y |<00L D00D!!!!!' c=obj1.encrypt(text) if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"') text='SpamSpamSpamSpamSpamSpamSpamSpamSpam' c=obj1.encrypt(text) if (obj2.decrypt(c)!=text): die('Error encrypting "'+text+'"') start=time.time() s=obj1.encrypt(str) str=obj2.decrypt(s) end=time.time() print_timing(256, end-start, verbose) del obj1, obj2 return ciphdef TestStreamModules(args=['arc4', 'XOR'], verbose=1): import sys, string args=map(string.lower, args) if 'arc4' in args: # Test ARC4 stream cipher arc4=exerciseStreamCipher('ARC4', verbose) if (arc4!=None): for entry in testdata.arc4: key,plain,cipher=entry key=binascii.a2b_hex(key) plain=binascii.a2b_hex(plain) cipher=binascii.a2b_hex(cipher) obj=arc4.new(key) ciphertext=obj.encrypt(plain) if (ciphertext!=cipher): die('ARC4 failed on entry '+`entry`) if 'xor' in args: # Test XOR stream cipher XOR=exerciseStreamCipher('XOR', verbose) if (XOR!=None): for entry in testdata.xor: key,plain,cipher=entry key=binascii.a2b_hex(key) plain=binascii.a2b_hex(plain) cipher=binascii.a2b_hex(cipher) obj=XOR.new(key) ciphertext=obj.encrypt(plain) if (ciphertext!=cipher): die('XOR failed on entry '+`entry`)def TestBlockModules(args=['aes', 'arc2', 'des', 'blowfish', 'cast', 'des3', 'idea', 'rc5'],
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -