加密(AES / ECB / PKCS5Padding)有效但解密(AES / ECB / PKCS5Padding)会引发错误

时间:2014-04-20 18:38:05

标签: java security

我试图解密我加密的字符串。然而加密似乎没问题。它将结果存储为28bytes .txt文件。 解密时我发现以下错误:

  

Errorjavax.crypto.IllegalBlockSizeException:使用填充密码解密时输入长度必须是16的倍数

我查看了stackoverflow中提供的所有可能的解决方案,但仍然无法修复异常。我被困在这部分2天;如果有人能帮助我解决它,我将非常高兴。

            Path pathP = Paths.get("../poem_encrypted.txt");//path provided in code

        byte[] poem = Files.readAllBytes(pathP);

        String codedbyte= poem.toString();

        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, bobDesKey);


        byte[] decoder = new BASE64Decoder().decodeBuffer(codedbyte);
        byte[] msgbyte = c.doFinal(decoder);
        String message = new String(msgbyte);


        System.out.println("message1"+message);
        Path path_e = Paths.get("/poem_decrypted.txt");
        FileOutputStream fout;

        fout = new FileOutputStream("/poem_decrypted.txt");

        fout.write(msgbyte);
        fout.close();
        System.out.println("File coded.");




            }
    catch (Exception e)
    {
        System.out.println("Error" + e);
    }

这是我的加密代码

            String poem = "abcdef ghijkl";
           byte[] poem_b = poem.getBytes("UTF-8");
           Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
           c.init(Cipher.ENCRYPT_MODE, aliceDesKey);
           byte[] coded = new byte[16];


          coded = c.doFinal(poem_b);
          String codedbyte= new BASE64Encoder().encodeBuffer(coded);




          Path path_e = Paths.get("/poem_encrypted.txt");
    PrintWriter fout;

    fout = new PrintWriter("C://Users//Ankita//Desktop//poem_encrypted.txt");

    fout.println(codedbyte);
    fout.close();
    System.out.println("File coded.");

    }

欢迎任何建议:)

0 个答案:

没有答案