May-02-2021, 02:38 PM
Quote:Implement cryptographic code return me an error where is the exact error
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
import json import struct import sys,os,binascii from base64 import b64encode from Crypto.Cipher import ChaCha20 from Crypto.Random import get_random_bytes plaintext = b'anyway always tell truth' key = get_random_bytes(32) cipher = ChaCha20.new(key=key) ciphertext = cipher.encrypt(plaintext) nonce = b64encode(cipher.nonce).decode('utf-8') ct = b64encode(ciphertext).decode('utf-8') result = json.dumps({'nonce':nonce,'ciphertext':ct}) print(result) def yieldchacha20_xor_stream(key,iv,position=0): # generate xor steam with ChaCha20 cipher if not isinstance(position, int): raise TypeError if position & ~0xffffffff: raise ValueError('Position is not unit32.') if not isinstance(key, bytes): raise TypeError if not isinstance(iv, bytes): raise TypeError if len(key) != 32: raise ValueError if len(iv) != 8: raise ValueError def rotate(v, c): return (v << c) & 0xffffffff | v >> (32 - c) def quartor_round(x, a, b, c, d): x[a] = (x[a] + x[b]) & 0xffffffff x[d] = rotate(x[d] ^ x[a], 16) x[c] = (x[c] + x[d]) & 0xffffffff x[b] = rotate(x[b] ^ x[c], 12) x[a] = (x[a] + x[b]) & 0xffffffff x[d] = rotate(x[d] ^ x[a], 8) x[c] = (x[c] + x[d]) & 0xffffffff x[b] = rotate(x[b] ^ x[c], 7) ctx = [0] * 16 ctx[:4] = (1634760805, 857760878, 2036477234, 1797285236) ctx[4: 12] = struct.unpack('<8L', key) ctx[12] = ctx[13] = position ctx[14:16] = struct.unpack('<LL', iv) while 1: x = list(ctx) for i in range(10): quartor_round(x, 0, 4, 8, 12) quartor_round(x, 1, 5, 9, 13) quartor_round(x, 2, 6, 10, 14) quartor_round(x, 3, 7, 11, 15) quartor_round(x, 0, 5, 10, 15) quartor_round(x, 1, 6, 11, 12) quartor_round(x, 2, 7, 8, 13) quartor_round(x, 3, 4, 9, 14) for c in struct.pack('<16L', *((x[i] + ctx[i]) & 0xffffffff for i in range(16))): yield c ctx[12] = (ctx[12] + 1) & 0xffffffff if ctx[12] == 0: ctx[13] = (ctx[13] + 1) & 0xffffffff def chacha20_encrypt(data, key, iv=nonce, position=0): if not isinstance(data, bytes): raise TypeError if iv is None: iv = b'\0' * 8 if isinstance(key, bytes): if not key: raise ValueError('key is empty') if len(key) < 32: key = (key * (32 // len(key) + 1))[:32] if len(key) > 32: raise ValueError('key too long') return bytes(a ^ b for a, b in zip(data, yieldchacha20_xor_stream(key, iv, position))) def main(): key = os.urandom(32) key = b'SuperSecretKey!!' print('the key that will be used is {}'.format(key)) print() plaintext = b'We all live in submarine' print('The plaintext is{}'.format(plaintext)) iv = b'SecretIV' print() enc = chacha20_encrypt(plaintext,key,iv) decode_enc = b64encode(enc).decode('utf-8') print('The encrypted string is {}.'.format(decode_enc)) print() dec = chacha20_encrypt(enc,key,iv) print('The decrypted string is {}.'.format(dec)) print() if __name__ == "__main__": sys.exit(int(main()) or 0)
Error:Traceback (most recent call last):
File "E:/ChaCha20/chacha20-poly1305-master/chacha20-poly1305-master/Chaha20_Encrypt.py", line 95, in <module>
sys.exit(int(main()) or 1)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'