【问题标题】:Unable to decrypt AES algorithm encrypted key in Spring MVC - error - bad arguments exception无法在 Spring MVC 中解密 AES 算法加密密钥 - 错误 - 错误参数异常
【发布时间】:2014-12-17 16:21:57
【问题描述】:

我将一个加密密钥作为 URL 参数传递,在收到密钥后,我试图解密密钥,但在代码的特定部分出现错误的参数异常。我不明白问题出在哪里以及如何解决它。特此请求您在这方面帮助我。我已经在下面的控制台部分提到了我在其中收到错误的代码行。

来源(生成加密密钥的遗留 Java 应用程序):

StrongAES strong_AES = new StrongAES();
id = strong_AES.encrypt_Data(user_string_2bEncrypted);
c = strong_AES.count_Cipher();

<a href="url/id=<%=id%>&c=<%=c%>">

强AES类

public class StrongAES {

    byte[] input;
    byte[] cipher_Text;
    SecretKeySpec Key;
    int ctLength;

    public byte[] encrypt_Data(String data){
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        byte[] input = data.getBytes();
        this.input = input;
        byte[] keyBytes = new byte[] {'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};

        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        this.Key = key;
        byte[] cipherText = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            cipherText = new byte[cipher.getOutputSize(input.length)];
            int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
            ctLength += cipher.doFinal(cipherText, ctLength);
            this.cipher_Text = cipherText;
            this.ctLength = ctLength;
        ....    
        ....
        return cipherText;
    }
    public int count_Cipher(){
        return this.ctLength;   
    }

Destination(用 Spring MVC 编写的应用程序):

HomeController.java

@RequestMapping(value = "/id={id}", method = RequestMethod.GET)
    public String getMethod(@PathVariable(value = "id") String id ,@RequestParam int c ,ModelMap model, HttpServletRequest request){
         System.out.println("key value is : "+id);
         System.out.println("lenth is :"+c);

        StrongAES strong_AES = new StrongAES();
        String login = null;
        try {
            login = strong_AES.decrypt_Data(id.getBytes(),c);
            System.out.println("decrypted login id is -"+login.toString());
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

强AES类

public class StrongAES {
            public void encrypt_Data(){

        }

    public String decrypt_Data(byte[] cipherText, int ctLength) throws InvalidAlgorithmParameterException{

        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        byte[] keyBytes = new byte[] {'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        System.out.println("byte length - "+cipherText.length);
        Cipher cipher;
        byte[] plainText = null;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS7Padding","BC");
            cipher.init(Cipher.DECRYPT_MODE, key);

            //decrypt
            plainText = new byte[cipher.getOutputSize(ctLength)];
            System.out.println("cipherText - "+cipherText+" ctLength - "+ctLength+" plainText - "+plainText);
            int ptLength = cipher.update(cipherText, 0, ctLength, plainText,0);
            System.out.println("ptLength - "+ptLength);

            ptLength += cipher.doFinal(plainText, ptLength);

            return new String(plainText);

控制台输出:

key value is : [B@b2c64
lenth is :16
byte length - 8
cipherText - [B@19125a3 ctLength - 16 plainText - [B@3e2052
Dec 17, 2014 7:09:27 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/app] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Bad arguments] with root cause
java.lang.IllegalArgumentException: Bad arguments
    at javax.crypto.Cipher.update(Cipher.java:1860)
    at com.myproject.ERA.form.model.StrongAES.decrypt_Data(StrongAES.java:38)  // this is the line - int ptLength = cipher.update(cipherText, 0, ctLength, plainText,0);
    at com.myproject.ERA.HomeController.getMethod(HomeController.java:69)

【问题讨论】:

  • 您目前没有显示您尝试过的内容以及发生异常的位置。如果这是an MCVE,那就太好了。

标签: java security spring-mvc aes bouncycastle


【解决方案1】:

问题无疑是把密文当作编码字符处理,如Spring/MFC中的id.getBytes()所示。由于并非所有字节值都是有效的字符编码,因此您可能会丢失数据。一般来说,您应该为明文定义字符编码,为密文定义二进制编码(例如 base 64)。

【讨论】:

  • 你好马腾。对不起,但我不得不改变这种方法。但我也认为在 url 中传递字节码存在一些问题。我试图在通过之前对其进行编码,但没有运气。只是为了知识,你能告诉我我应该如何编码字节码并通过 url 发送它,以及在收到时我应该如何解码它。提前致谢。
  • base64url 编码是最有效的。您可以先编码为普通的base64,然后替换/+ 字符,因为它们在URL 中当然有不同的含义。 PS 在这种情况下,一个小小的支持会非常很好:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多