Java AES加密整个字符串

如何使用AES加密整个字符串。 我下面的代码只加密到第一个识别的空间:(。我该如何解决这个问题?谢谢

SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE"); cipher.init(Cipher.ENCRYPT_MODE, key); String result = new String(cipher.doFinal(message.getBytes())); System.out.println("Encrypted:" + result); 

编辑 OMG我不能相信这一点,我怎么可能想念这个:(因为我的扫描仪是下一个而不是nextLine …这一天有多尴尬这让我感到尴尬,直到现在才真正想到检查那个。问题解决了:)谢谢大家

除了尝试使用new String(byte[])打印任意byte[]之外,我没有看到您的代码有任何问题。 试试这个尺码:

 public static byte[] encrypt(String message) throws Exception { String salt = "1111111111111111"; SecretKeySpec key = new SecretKeySpec(salt.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE"); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(message.getBytes()); } public static void main (String[] args) throws Exception { String hello = Arrays.toString(encrypt("hello")); System.out.println("hello:" + hello); String helloWorld = Arrays.toString(encrypt("hello world")); System.out.println("hello world:" + helloWorld); } 

哪个印刷品:

 hello:[115, -73, -46, -121, 36, -106, -99, 100, 103, -24, -40, -38, 113, -8, 40, -57] hello world:[5, 88, -31, 115, 4, 48, -75, 44, 83, 21, 105, -67, 78, -53, -13, -28] 

我想我们都同意这些是两个不同的字节数组。