Python Forum
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non
#1
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'
Reply
#2
Your main() function does not contain a "return", so by default it returns None.
Reply
#3
Quote:did you mean like this

return
pass;
if __name__ == "__main__":
    sys.exit(main())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,215 Sep-02-2021, 03:24 PM
Last Post: buran
  TypeError: 'NoneType' object is not subscriptable syafiq14 2 1,808 Aug-26-2021, 06:18 AM
Last Post: alexaminar
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 532 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 822 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  bytes f-string ? Skaperen 5 1,171 Jun-10-2021, 10:21 PM
Last Post: Gribouillis
  TypeError: __init__() got an unexpected keyword argument 'value' Anldra12 7 5,230 May-11-2021, 06:35 PM
Last Post: deanhystad
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 1,994 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  TypeError: __str__ returned non-string (type tuple) Anldra12 1 2,374 Apr-13-2021, 07:50 AM
Last Post: Anldra12
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 1,082 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 910 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020