Java 256-bit AES Password-Based Encryption

后端 未结 9 1568
名媛妹妹
名媛妹妹 2020-11-21 05:22

I need to implement 256 bit AES encryption, but all the examples I have found online use a \"KeyGenerator\" to generate a 256 bit key, but I would like to use my own passkey

9条回答
  •  孤城傲影
    2020-11-21 06:00

    What I've done in the past is hash the key via something like SHA256, then extract the bytes from the hash into the key byte[].

    After you have your byte[] you can simply do:

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedBytes = cipher.doFinal(clearText.getBytes());
    

提交回复
热议问题