我有一个 AES 加密字节数组。当我在 java 应用程序中解密这个数组时,一切都很好。但是在一个servlet容器中,解密错误。
这是我的字节数组
String [] str = new String[] {"41", "23", "67" ,"-124", "-56" ,"-35" ,"89", "-54" ,"-17" ,"-49" ,"-53", "-21" ,"125" ,"4", "98", "-13", "60" ,"-72", "12", "75" ,"-105" ,"-104", "107", "34", "1", "-109", "-19", "-102", "-72", "9" ,"26" ,"-39", "-60", "-15", "0" ,"112", "-5", "-86", "-7", "5" ,"75", "100" ,"94", "-47", "6", "-81", "-22", "82", "97" ,"114", "3", "-24", "-80", "67", "106", "-100" ,"-35", "-83", "54", "-95", "124", "-22", "-100", "-47" };
byte [] inv = new byte[str.length];
int count = 0;
for (String s : str) {
inv[count++] = Byte.valueOf(s);
}
Java应用程序的结果
Decrypted: 000000.016*kWh|000000.007*kWh|000000.015*kWh|000000.000*kWh
我的申请代码
byte[] keyBytes = "vikoAmrPass12345".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte [] out = cipher.doFinal(inv);
System.out.println("Decrypted: " +new String(out));
我在 servlet 容器上工作的 web 代码与上面的代码完全相同。但是容器解密的结果是
'?f ??0??????b]5hJ?F*`U????.8??p@?]?u?~Nb??z?????{3??;?
这里有什么问题?
在我的应用程序中,我得到了加密的字节数组
ServerSocket ss = new ServetSocket(port);
Socket socket = ss.accept();
InputStream in = socket.getInputStream();
byte [] sizeBuffer = new byte[5];
in.read(sizeBuffer);
byte [] dataBuffer = new byte[Integer.valueOf(""+new String(sizeBuffer)];
in.read(dataBuffer);
加密数据存在于dataBuffer数组中。并在嵌入式设备中处理加密。
我的配置
java 1.6 tomcat 7.1
谢谢。