我有一个从 Java 客户端接收消息的 Java HTTP 服务器。
服务器和客户端都使用相同的 Encrypter 类:
public class Encrypter {
private Cipher cipher; //The encryption cipher object
public static final String ALGORITHM = "Blowfish"; //Encryption Algorithm
/**
* Constructor
*/
public Encrypter()
{
try {
initlizeCipher();
} catch (Throwable e) {
ServerSettings.LOG.logError(e);
e.printStackTrace();
}
}
/**
* Initialize the Cipher object
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
private void initlizeCipher() throws NoSuchAlgorithmException, NoSuchPaddingException
{
cipher = Cipher.getInstance(ServerSettings.ALGORITHM);
}
/**
* Encrypt a String
* @param string String to encrypt
* @return an encrypted String
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws UnsupportedEncodingException
*/
public synchronized String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
{
cipher.init(Cipher.ENCRYPT_MODE, ServerSettings.SECRECT_KEY_SPEC);
byte[] stringBytes = string.getBytes("UTF-8");
byte[] encryptedBytes = cipher.doFinal(stringBytes);
return Base64.encodeBytes(encryptedBytes);
}
/**
* Decrypt a String
* @param string String to decrypt
* @return a decrypted String
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws IOException
*/
public synchronized String decrypt(String string) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException
{
cipher.init(Cipher.DECRYPT_MODE, ServerSettings.SECRECT_KEY_SPEC);
byte[] decryptedBytes = Base64.decode(string.getBytes());
byte[] encryptedBytes = cipher.doFinal(decryptedBytes);
return new String(encryptedBytes,"UTF-8");
}
}
当发送的变量是:m=encryptedMessage.
由于某种原因,我总是得到
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
而且我无法解密在服务器端收到的消息..
起初我以为消息可能已损坏,但事实并非如此..
有任何想法吗 ?
更新1:
奇怪的是,一些发往服务器的消息被正确解密,而一些则返回该错误。好消息:
zuhRpmbtH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相同的方式发送并打印到 System.Out 进行测试。正如您所见,由于某种原因,坏消息有空格,而好消息却没有可能给出块错误消息。知道如何解决它吗?
更新 2:
在深入研究我的代码后,我发现我的问题出在解析 POST 方法参数的函数内部。我不能说我是编码方面的专家,但是通过 Base64 编码抛出 POST URLEcoding 存在一些问题。
这是我的解析代码,在获取 URLDecode 后会转到我的 Encrypter 进行解密:
private void parseQuery(String query, HashMap parameters) throws UnsupportedEncodingException {
if (query != null) {
String pairs[] = query.split("[&]");
for (String pair : pairs)
{
String param[] = pair.split("[=]");
String key = null;
String value = null;
if (param.length > 0)
key = URLDecoder.decode(param[0], "UTF-8");//System.getProperty("file.encoding"));
if (param.length > 1)
value = URLDecoder.decode(param[1], "UTF-8");//System.getProperty("file.encoding"));
if (parameters.containsKey(key))
{
Object obj = parameters.get(key);
if(obj instanceof List<?>)
{
@SuppressWarnings("unchecked")
List<String> values = (List<String>)obj;
values.add(value);
}
else if(obj instanceof String)
{
List<String> values = new ArrayList<String>();
values.add((String)obj);
values.add(value);
parameters.put(key, values);
}
}
else
parameters.put(key, value);
}
}
}
有任何想法吗 ?