Working with RSA encryption for the first time.

With keys you really should be thinking entropy, not length specifically. Anyways, yes you can expand a short key into a long key, a simple solution is just repeat the key until you fill it up, a more complex solution might be to hash it and repeat the hash to fill it up (that can make coding it easier). The issue is expanding a short key to a long key does not make the key more secure. For some algorithms it's acceptable, they require a key of some size, and you want a shorter key. In that case just repeat it, you'll get something that works.

As for security, it's generally very poor to just expand the key like that, since it makes the key easy to brute force. Take the password "hunter2" for example, it contains letters and numbers and it's 6 characters long. So this password contains log2((26+10)6) bits of entropy (about 31 bits of entropy). If you needed a 1024-bit password you could compress the password into 31 bits, and repeat those 31-bits ~33 times. You would have yourself a 1024-bit key. Unfortunately since you just repeated the key 33 times, an attacker can do the same in their attack. The result is that your key is less secure than a 31-bit key, no matter what algorithm you use to expand the key (how much less secure than a 31-bit key depends on the assumptions you make about the input password and sometimes the algorithms used to expand the key and how it interacts with your encryption). If you start requiring decent length passwords, you might have something difficult to crack, depends on the algorithm.

So expanding the key to fill up your missing keyspace will get you encryption, but you'll have the security of your input password (trivial to crack). If you have a 1024-bit key and you really want it secure with a password, a minimum requirement of a random 198 character alphanumeric password or 77 random words would probably get you something resembling secure. Obviously, that's usually not a good solution, so most applications that really need a password just save the key to a file locally, and encrypt it against the weak password. If an attacker had access to that file, they could brute force against your password, but if the attacker is remote, they'd have to brute force against the key, which is your full 1024-bit secure.

/r/learnprogramming Thread Parent