Python Secure Aes Key Generator

Python Secure Aes Key Generator 5,6/10 3433 votes
1

I need to make strong key for AES-256 in a) Unicode characters, b) key in bytes.

Aes Encryption Key Generator

Technically, not as stated. AES-256 requires a 256bit key. SHA-512 will output 512 bits so unless you chop off half of the digest it will not work. A better solution is to use a standard and well tested key derivation function such as pbkdf2. Don't roll your own crypto. AES 256 Encryption and Decryption in Python. If you want high level of security, this should be replaced with password based key derivation function PBKDF2. The following example uses the PBKDF2 to generate the key, # AES 256 encryption/decryption using pycrypto library import base64 from Crypto.Cipher import AES from Crypto import. AES Key generator: Advanced Encryption Standard « Security « Java Tutorial. Home; Java Tutorial; Language; Data Type. AES Key generator: Advanced Encryption Standard « Security «. Sep 08, 2014  16 random bytes of salt are extracted from the system's secure random number generator (usually /dev/urandom). The given master key is stretched and expanded by PKBDF2-HMAC(SHA256) using the salt from 1), to generate the AES key, HMAC key. By strong, we mean not easily guessed and has sufficient entropy (or secure randomness). That being said, for the sake of demonstration of AES encryption, we generate a random key using a rather simple scheme. Do not copy and use this key generation scheme in production code. AES encryption needs a 16-byte key. Fernet (symmetric encryption)¶ Fernet guarantees that a message encrypted using it cannot be manipulated or read without the key. Fernet is an implementation of symmetric (also known as “secret key”) authenticated cryptography. Fernet also has support for implementing key rotation via MultiFernet. Class cryptography.fernet.Fernet (key) source ¶.

a) I have to generate 50 random Unicode characters and then convert them to bytes. Is this possible to use Unicode characters as AES256 key?For e.g. I want to use this page to create password.is there any way to import all characters from Windows characters table to program in Windows Form App?

b) I'm using this code:

It's enough or I should change something?

Also I have one more question. Making an AES key longer then 43 ASCII characters will be more secure or it will be anyway hashed to 256bit? And there is difference between ASCII key of 43 characters and 100?

1 answers

2

a) I have to generate 50 random Unicode characters and then convert them to bytes. Is this possible to use Unicode characters as AES256 key?

Yes, this is possible. Since you have plenty of space for characters you can just encode it. ceil(32 / 3) * 4 = 44, so you'd have enough characters for this. You would not be using the additional space provided by Unicode encoding though. Obviously you would need to convert it back to binary before using it.

b) is aes.GenerateKey 'enough'?

Yes, aes.GenerateKey is enough to generate a binary AES key.

Aes Key Generator

c) Making an AES key longer then 43 ASCII characters will be more secure or it will be anyway hashed to 256bit? And there is difference between ASCII key of 43 characters and 100?

An AES key is not hashed at all. It's just 128, 192 or 256 bits (i.e. 16, 24 or 32 bytes) of data that should be indistinguishable from random (to somebody that doesn't know the value, of course). If you want to hash something you'd have to do it yourself - but please read on.

The important thing to understand is that a password is not a key, and that keys for modern ciphers are almost always encoded as binary. For AES there is no such thing as an ASCII key. If you need to encode the key, use base 64.

If you want to use a password then you need to use a key derivation function or KDF. Furthermore, if you want to protect against dictionary and rainbow table attacks you will want to use a password based key derivation function or PBKDF. Such a KDF is also called a 'password hash'. In case of .NET your best bet is Rfc2898DeriveBytes which implements PBKDF2. PBKDF2 is defined in the RFC 2898 titled: PKCS #5: Password-Based Cryptography Specification Version 2.0 which you may want to read.

Encrypt & Decrypt using PyCrypto AES 256 From http://stackoverflow.com/a/12525165/119849
AESCipher.py

How To Generate Aes Key

#!/usr/bin/env python
importbase64
fromCryptoimportRandom
fromCrypto.CipherimportAES
BS=16
pad=lambdas: s+ (BS-len(s) %BS) *chr(BS-len(s) %BS)
unpad=lambdas : s[0:-ord(s[-1])]
classAESCipher:
def__init__( self, key ):
self.key=key
defencrypt( self, raw ):
raw=pad(raw)
iv=Random.new().read( AES.block_size )
cipher=AES.new( self.key, AES.MODE_CBC, iv )
returnbase64.b64encode( iv+cipher.encrypt( raw ) )
defdecrypt( self, enc ):
enc=base64.b64decode(enc)
iv=enc[:16]
cipher=AES.new(self.key, AES.MODE_CBC, iv )
returnunpad(cipher.decrypt( enc[16:] ))
cipher=AESCipher('mysecretpassword')
encrypted=cipher.encrypt('Secret Message A')
decrypted=cipher.decrypt(encrypted)
printencrypted
printdecrypted
requirements.txt

commented Jan 13, 2014

AWESOMESAUCE.

commented Sep 16, 2016

This only works because the 'mysecretpassword' is 16 bytes. If it were a different (not dividable by 16) amount of bytes you'd get
'ValueError: AES key must be either 16, 24, or 32 bytes long'
To avoid this the key may be hashed:
self.key = hashlib.sha256(key.encode('utf-8')).digest()

commented Dec 22, 2016

Very minor changes to make it python 3 compatible https://gist.github.com/mguezuraga/257a662a51dcde53a267e838e4d387cd

commented Dec 19, 2017
edited

lambda removed(pep 8 support)
ord removed(python 3 support)

commented Jan 20, 2018
edited

In Python 3 using the modifications of Craz1k0ek it still doesn't work with Unicode. For example the input Hello, 你好 raises ValueError: Input strings must be a multiple of 16 in length

Edit: found a working version: https://stackoverflow.com/a/44212550

commented Apr 26, 2018

Aes 256 Key Generator

i think this is aes 128, we have a standard blocksize of 16 bytes (128bit)

commented Apr 26, 2018

i can't seem to find how to do aes256

commented Jun 5, 2018

Please provide the JAVA code equivalent to above which is in python.

Python Secure Aes Key Generator Download

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment