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: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 270 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 455 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 920 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,169 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,184 Jun-09-2023, 06:56 PM
Last Post: kpatil
Question Extracting Version Number from a String britesc 2 1,030 May-31-2023, 10:20 AM
Last Post: britesc
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,931 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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