Cod*_*ody 3 java string encryption 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);
Run Code Online (Sandbox Code Playgroud)
编辑 OMG我不相信这一点,我怎么可能想念这个:(因为我的扫描仪接下来而不是nextLine ...这整天都让我感到尴尬,但是现在才真正想到检查那个.问题解决了:)谢谢大家
我没有看到你的代码有什么问题,除了试图打印任意byte[]使用new String(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);
}
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
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]
Run Code Online (Sandbox Code Playgroud)
我想我们都同意这些是两个不同的字节数组.