例外:javax.crypto.IllegalBlockSizeException:使用填充密码

时间:2016-05-24 16:46:31

标签: java encryption jax-rs

在检查了多篇关于相同问题的帖子后,我无法解决此错误。我的加密和解密代码如下:

public class MessagesEncrypter {

    static String key = "1234567891234567";

    public static String encrypt(String input){
        byte[] crypted = null;
        try{
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        }catch(Exception e){
            System.out.println(e.toString());
        }
        return new String(Base64.encodeBase64(crypted));
    }

    public static String decrypt(String input){
        byte[] output = null;
        try{
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(Base64.decodeBase64(input));
        }catch(Exception e){
            System.out.println(e.toString());
        }
        return new String(output);
    }
}

我得到标题中所述的异常

  

javax.crypto.IllegalBlockSizeException:输入长度必须为多个   用填充密码解密时为16。

我正在使用此函数来加密我的jax-rs API中多个REST请求的参数,比如我正在创建的房间的用户名和ID。

----------------------------------编辑1 ----------- --------------------------

如下所示,请参阅我的api服务中使用此方法的一些示例以及POST本身的方法:

    @POST
@Path("/register")
@Produces("application/json")
public static Response newPlayer(@FormParam("name") String name, 
        @FormParam("password") String password, 
        @FormParam("chips") String chips) throws Exception {

    String n = MessagesEncrypter.decrypt(name);
    int c = Integer.parseInt(MessagesEncrypter.decrypt(chips));

    boolean result = new AccessManager().insertPlayer(n, password, c);

    if(result==false){
        return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Register Failed for: " + name).build();
    }

    else {
        return Response.ok("Register Successful", MediaType.APPLICATION_JSON).build();
    }
}


public static int POST(String path, String[] paramName, String[] paramVal) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

     // Define the server endpoint to send the HTTP request to
    URL serverUrl = 
    new URL(urlStandard + path);
    HttpURLConnection urlConnection = (HttpURLConnection)serverUrl.openConnection();

    // Indicate that we want to write to the HTTP request body
    urlConnection.setDoOutput(true);
    urlConnection.setRequestMethod("POST");

    // Create the form content
    OutputStream out = urlConnection.getOutputStream();
    Writer writer = new OutputStreamWriter(out, "ISO-8859-1");
    for (int i = 0; i < paramName.length; i++) {
          writer.write(paramName[i]);
          writer.write("=");

          String value = paramVal[i];
          /*
          if(paramName[i].equals("password"))
             value = paramVal[i];

          else
              value = MessagesEncrypter.encrypt(paramVal[i]);
          */
          writer.write(value);

          writer.write("&");
        }
    writer.close();
    out.close();

    int response = urlConnection.getResponseCode();

    //print result
    //System.out.println("POST request returned:" + response);

    urlConnection.disconnect();
    return response;
}

0 个答案:

没有答案