0

我有一个小问题。在我的应用程序中,我需要从用户编写的字符串创建一个用于 AES 加密的密钥,但我不知道该怎么做。例如:用户写道:“abscde”,我需要一些如何将其更改为 AES 密钥。这是我的代码。它只能使用以十六进制提供的密钥,例如“000102030405060708090A0B0C0D0E0F”。

   public static String encrypt(String text, String key){
        String mess="error";
        try{

            byte[] encodedText=text.getBytes(Charset.forName("UTF-8"));
            byte[] keyData=hexStringToByte(key);

            SecretKeySpec sKey=new SecretKeySpec(keyData, "AES");

            Cipher encryptionCypher=Cipher.getInstance("AES/CBC/PKCS5Padding");

            final int blocks=encryptionCypher.getBlockSize();

            final byte[] ivData=new byte[blocks];

            final SecureRandom rnd=new SecureRandom();

            rnd.nextBytes(ivData);

            final IvParameterSpec iv=new IvParameterSpec(ivData);
            encryptionCypher.init(Cipher.ENCRYPT_MODE, sKey,iv);

            final byte[] encryptedMessage=encryptionCypher.doFinal(encodedText);
            final byte[] ivAndMessage=new byte[ivData.length+encryptedMessage.length];
            System.arraycopy(ivData, 0, ivAndMessage, 0, blocks);
            System.arraycopy(encryptedMessage, 0, ivAndMessage, blocks, encryptedMessage.length);
            final String finalMessage=Base64.encodeToString(ivAndMessage, 0);

            mess=finalMessage;  


            } catch (IllegalBlockSizeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BadPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();

            } catch (InvalidAlgorithmParameterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        return mess;
    }
4

0 回答 0