Home > Net >  What happens when the encryption algorithm receives an unsupported key size?
What happens when the encryption algorithm receives an unsupported key size?

Time:10-19

I try to build encryption program, and I use AES (256/192/128) from realisation I took from github (https://github.com/SergeyBel/AES) there is exception if key is not of this sizes. But I want to use key as a password, in KeePass (https://keepass.com) (they also encrypt with this algorithm) we can create password of different sizes. What should I do? I must add some padding bytes? Or I must use hash algorithm to create passwords of the same size?

CodePudding user response:

When using a passphrase with a symmetric encryption algorithm, the common way of handling this is by hashing the passphrase then using enough bytes from the hash to build the encryption key. This has the benefit of accepting a passphrase of any size without having to pad it (or trim it if too long).

For example, if your passphrase is "password", the SHA256 hash of this is (printed as hex) 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8. You can then use this hash as the key to AES256. For AES128, use the first 16 bytes of the hash (i.e. 5e884898da28047151d0e56f8dc62927).

  • Related