Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:34:19+00:00 2026-05-27T08:34:19+00:00

I have the following python script to encrypt/decrypt data using AES 256, could you

  • 0

I have the following python script to encrypt/decrypt data using AES 256, could you please tell me if there’s anything in the code that may make the encryption weak or if there’s anything that I’ve not taken account of for AES 256 encryption using CBC mode? I’ve tested the script and it works fine, it is encrypting and decrypting data but just wanted a second opinion. Thanks.

    from Crypto.Cipher import AES
    from Crypto import Random

    BLOCK_SIZE = 32

    INTERRUPT = u'\u0001'

    PAD = u'\u0000'

    def AddPadding(data, interrupt, pad, block_size):
        new_data = ''.join([data, interrupt])
        new_data_len = len(new_data)
        remaining_len = block_size - new_data_len
        to_pad_len = remaining_len % block_size
        pad_string = pad * to_pad_len
        return ''.join([new_data, pad_string])

    def StripPadding(data, interrupt, pad):
        return data.rstrip(pad).rstrip(interrupt)

    SECRET_KEY = Random.new().read(32)

    IV = Random.new().read(16)

    cipher_for_encryption = AES.new(SECRET_KEY, AES.MODE_CBC, IV)
    cipher_for_decryption = AES.new(SECRET_KEY, AES.MODE_CBC, IV)

    def EncryptWithAES(encrypt_cipher, plaintext_data):
        plaintext_padded = AddPadding(plaintext_data, INTERRUPT, PAD, BLOCK_SIZE)
        encrypted = encrypt_cipher.encrypt(plaintext_padded)
        return encrypted

    def DecryptWithAES(decrypt_cipher, encrypted_data):
        decoded_encrypted_data = encrypted_data
        decrypted_data = decrypt_cipher.decrypt(decoded_encrypted_data)
        return StripPadding(decrypted_data, INTERRUPT, PAD)

    our_data_to_encrypt = u'abc11100000'
    encrypted_data = EncryptWithAES(cipher_for_encryption, our_data_to_encrypt)
    print ('Encrypted string:', encrypted_data)

    decrypted_data = DecryptWithAES(cipher_for_decryption, encrypted_data)
    print ('Decrypted string:', decrypted_data)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:34:20+00:00Added an answer on May 27, 2026 at 8:34 am

    I’ve seen the code posted on the internet. There are – in principle – not too many things wrong with it, but there is no need to invent your own padding. Furthermore, I don’t see why the first padding character is called INTERRUPT. I presume that INTERRUPT and PAD is handled as a single byte (I’m not a Python expert).

    The most common padding is PKCS#5 padding. It consists of N bytes with the value of the number of padding bytes. The padding used here looks more like ‘ISO’ padding, which consists of a single bit set to 1 to distinguish it from the data and other padding bits, and the rest is zero’s. That would be code point \u0080 in code.

    So the encryption (which can provide confidentiality of data) seems to be used correctly. It depends on the use case if you also need integrity protection and/or authentication, e.g. by using a MAC or HMAC. Of course, no legal guarantees or anything provided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query that I'm executing using a Python script (by using
I have the following simple python test script that uses Suds to call a
i have something similar to the following... used a python script to JSON to
I have the following Python 2.6 program and YAML definition (using PyYAML ): import
I have a question about using os.execvp in Python. I have the following bit
I have a python script that invokes the following command: # make After make,
I'm trying to solve the following problem: Say I have a Python script (let's
I have a Python script that pulls in data from many sources (databases, files,
Let's say we have the following mega-simple Python script: print Initializing.... a=10 print Variable
I have following script: #!/usr/bin/python while True: x = raw_input() print x[::-1] I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.