Skip to content

Commit bc05855

Browse files
authored
Initial commit
1 parent 022b234 commit bc05855

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# AES Key Generator
2+
3+
A simple Python script to generate a random AES key of a given length.
4+
5+
## Opportunities
6+
7+
* Generate a key of **8**, **16**, **32** or **64** bytes.
8+
* Output the key to the console in hexadecimal format.
9+
* Saving the key to a file (if necessary).
10+
11+
## Requirements
12+
13+
* Python **3.6** or higher.
14+
* Modules from the standard library: `argparse`, `secrets`.
15+
16+
## Installation
17+
18+
1. Clone the repository:
19+
20+
```bash
21+
git clone https://github.com/IVSamDev/AES-Key-Generator.git
22+
cd aes-key-generator
23+
```
24+
2. Make sure you have Python 3.6+ installed:
25+
26+
````bash
27+
python3 --version
28+
````
29+
30+
## Usage
31+
32+
Run the script from the console, specifying the key length and (optionally) the file to save:
33+
34+
```bash
35+
python3 key.py --size <bytes> [--output <file>]
36+
```
37+
38+
### Attributes (parameters)
39+
40+
| Option | Description | Commitment | Possible values |
41+
| ---------------- | ---------------------------------------------- | --------------- | --------------------- |
42+
| `-s`, `--size` | Key length in bytes | **necessarily** | `8`, `16`, `32`, `64` |
43+
| `-o`, `--output` | File path to save the key (hex string) | optionally | `<file_name>.txt` |
44+
45+
### Examples
46+
47+
1. Generate a 16-byte key and output to the console:
48+
49+
```bash
50+
python3 key.py -s 16
51+
```
52+
53+
2. Generate a 32-byte key and save to the `key.txt` file:
54+
55+
````bash
56+
python3 key.py --size 32 --output key.txt
57+
````
58+
59+
## License
60+
61+
MIT License © IVSamDev

key.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import argparse
2+
import secrets
3+
4+
5+
def generate_aes_key(length: int) -> bytes:
6+
if length not in (8, 16, 32, 64):
7+
raise ValueError("Allowable key lengths: 8, 16, 32 or 64 bytes")
8+
return secrets.token_bytes(length)
9+
10+
def main():
11+
parser = argparse.ArgumentParser(
12+
description="Generate AES key of specified length in bytes"
13+
)
14+
parser.add_argument(
15+
"-s", "--size",
16+
type=int,
17+
choices=[8, 16, 32, 64],
18+
required=True,
19+
help="Key length in bytes: 8, 16, 32 or 64"
20+
)
21+
parser.add_argument(
22+
"-o", "--output",
23+
type=str,
24+
help="File for saving the key (optional)"
25+
)
26+
27+
args = parser.parse_args()
28+
key = generate_aes_key(args.size)
29+
30+
# Key output in hexadecimal form
31+
hex_key = key.hex()
32+
print(f"Generated AES key ({args.size} byte): {hex_key}")
33+
34+
# Saving to file, if specified
35+
if args.output:
36+
with open(args.output, "w") as f:
37+
f.write(hex_key)
38+
print(f"The key is saved to a file: {args.output}")
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)