Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    How to use AES CTR mode decrypt msg

    Scheduled Pinned Locked Moved LoPy
    3 Posts 3 Posters 30 Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y Offline
      Yuzhe pei
      last edited by

      Hi to everyone.

      I try to create a program using AES in CTR mode.

      from crypto import AES
      import crypto
      key = b'notsuchsecretkey' # 128 bit (16 bytes) key
      counter = crypto.getrandbits(128) # hardware generated random IV (never reuse it)
      
      cipher = AES(key, AES.MODE_CTR, counter=counter)
      msg = cipher.encrypt(b'Attack at dawn')
      print(msg)
      # ... after properly sent the encrypted message somewhere ...
      
      cipher = AES(key, AES.MODE_CTR, msg[:16]) # on the decryption side
      original = cipher.decrypt(msg[16:])
      print(original)
      
      

      Output

      >>> 
      b'\xdb\x16\xa43\x0bfZb\x89\xae\xec\xdf\xf1\xfd'
      Traceback (most recent call last):
        File "<stdin>", line 12, in <module>
      TypeError: object with buffer protocol required
      >
      

      As you can see, I can encrypt msg. But when I decrypt msg, I will receive the following Error.

      Traceback (most recent call last):
        File "<stdin>", line 12, in <module>
      TypeError: object with buffer protocol required
      >
      
      J 1 Reply Last reply Reply Quote 0
      • G Offline
        g0hww
        last edited by

        I am using some old stable code with older firmware, and this is not using CTR mode, but CFB. I use an encrypt then MAC approach, with the IV sent between the MAC digest and the cipher text. I'm not sure how different this is to CTR mode or more recent pycom code, but hope it might help.

        cipher = AES(config.aes_key, AES.MODE_CFB,lora_in[hmac_len:hmac_len+iv_len])
        original = cipher.decrypt(lora_in[hmac_len+iv_len:])
        
        1 Reply Last reply Reply Quote 1
        • J Offline
          jcaron @Yuzhe pei
          last edited by

          @Yuzhe-pei where do you get msg from? My guess is that it isn’t a byte array.

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom