Aes Key Generation Python

Posted : admin On 1/17/2022

AES- 128 has 128 bit key = 16 bytes. Randomkey = os.urandom(16) should be sufficient for most uses. When you feed this random value to M2 (or whatever crypto library), it is transformed internally into a 'key schedule' actually used for encryption. The AES algorithm takes three parameters: encryption key, initialization vector (IV) and the actual message to be encrypted. If you have a randomly generated AES key then you can use that one directly and merely generate a random initialization vector. Asymmetric keys are represented by Python objects. Each object can be either a private key or a public key (the method hasprivate can be used to distinguish them). A key object can be created in four ways: generate at the module level (e.g. The key is randomly created each time. Generate Aes Key Python.

  1. Aes Key Generation Python 3
  2. Aes Key Generation Python Compiler
  • Getting a Key

Using the cryptography module in Python, this post will look into methods of generating keys, storing keys and using the asymmetric encryption method RSA to encrypt and decrypt messages and files. We will be using cryptography.hazmat.primitives.asymmetric.rsa to generate keys.

Installing cryptography

Since Python does not come with anything that can encrypt files, we will need to use a third-party module.

PyCrypto is quite popular but since it does not offer built wheels, if you don't have Microsoft Visual C++ Build Tools installed, you will be told to install it. Instead of installing extra tools just to build this, I will be using the cryptography module. To install this, execute:

To make sure it installed correctly, open IDLE and execute:

If no errors appeared it has been installed correctly.

What is Asymmetric Encryption?

Python aes 128

If you read my article on Encryption and Decryption in Python, you will see that I only used one key to encrypt and decrypt.

Asymmetric encryption uses two keys - a private key and a public key. Public keys are given out for anyone to use, you make them public information. Anyone can encrypt data with your public key and then only those with the private key can decrypt the message. This also works the other way around but it is a convention to keep your private key secret.

Getting a Key

To generate the two keys, we can call rsa.generate_private_key with some general parameters. The public key will be found in the object that holds the creation of the private key.

Storing Keys

To store the keys in a file, they first need to be serialized and then written to a file. To store the private key, we need to use the following.

You can password protect the contents of this file using this top key serialization example.

To store the public key, we need to use a slightly modified version.

Remember that public and private keys are different so you will have to use these methods for each key.

Reading Keys

To get the keys out of the files, we need to read each file and then load them. To read the private key, use the following.

If you store the key with a password, set password to what you used.

The variable private_key will now have the private key. To read the public key, we need to use a slightly modified version.

The variable public_key will now have the public key.


Due to how asymmetric encryption algorithms like RSA work, encrypting with either one is fine, you just will need to use the other to decrypt. Applying a bit of logic to this can create some useful scenarios like signing and verification. For this example, I will assume that you keep both keys safe and don't release them since this example is only for local encryption (can be applied to wider though when keys are exchanged).

This means you can use either key but I will demonstrate using the public key to encrypt, this will mean anyone with the private key can decrypt the message.


Assuming that the public key was used to encrypt, we can use the private key to decrypt.


To show this in action, here is a properly constructed example.

Aes Key Generation Python 3

Encrypting and Decrypting Files

To encrypt and decrypt files, you will need to use read and write binary when opening files. You can simply substitute the values I previously used for message with the contents of a file. For example:

Using the variable message you can then encrypt it. To store, you can use the general Python method when encryption returns bytes.

Now to decrypt you can easily read the data from test.encrypted like the first bit of code in this section, decrypt it and then write it back out to test.txt using the second bit of code in this section.

You may generate an RSA private key with the help of this tool. Additionally, it will display the public key of a generated or pasted private key.


RSA is an asymmetric encryption algorithm. With a given key pair, data that is encrypted with one key can only be decrypted by the other. This is useful for encrypting data between a large number of parties; only one key pair per person need exist. RSA is widely used across the internet with HTTPS.

To generate a key pair, select the bit length of your key pair and click Generate key pair. Depending on length, your browser may take a long time to generate the key pair. A 1024-bit key will usually be ready instantly, while a 4096-bit key may take up to several minutes. For a faster and more secure method, see Do It Yourself below. does not yet have a tool for facilitating the encryption and decryption of data using RSA, but you may Do It Yourself with the instructions below.

Do It Yourself

For these steps, you will need a command line shell with OpenSSL. Ideally, you should have a private key of your own and a public key from someone else. For demonstration, we will only use a single key pair.

Generate Private Key

Run this command to generate a 4096-bit private key and output it to the private.pem file. If you like, you may change the key length and/or output file.

Derive Public Key

Given a private key, you may derive its public key and output it to public.pem using this command. (You may also paste your OpenSSL-generated private key into the form above to get its public key.)

Encrypt Data

We can now use this key pair to encrypt and decrypt a file, data.txt.

Decrypt Data

Aes Key Generation Python Compiler

Given the encrypted file from the previous step, you may decrypt it like so.