I want to combine hashing and encryption for better security. So can I use a hash key generated from SHA-1 as a key in AES. For example, I have a password "50", I calculate SHA-1 hash for it (e1822db470e60d090affd0956d743cb0e7cdf113), and I want to feed those bytes as key for AES-128. What things I need to care while implementing this? Should I just truncate after 128 bits or do some kind of folding?
Here the code:
import java.security.MessageDigest;
public class MessageDigestExample {
public static void main(String[] args)throws Exception{
String input = "This is a message";
MessageDigest hash = MessageDigest.getInstance("SHA1");
System.out.println("input : " + input);
hash.update(Utils.toByteArray(input));
System.out.println("digest : " + Utils.toHex(hash.digest()));
} }