【问题标题】:Java AES Decrypting problemJava AES解密问题
【发布时间】:2011-04-24 08:36:32
【问题描述】:

我的任务是解密使用以下标准加密的 Java 文件:

具有 128 位密钥、ECB 模式和 PKCS7 填充的 AES 加密算法。 加密文件格式为: - 第一个字节是十六进制 0x31 - 指定使用的加密方法(AES 为 1) - 后跟输入文件的加密字节

我还必须下载该文件,所以这是我目前的尝试:

下载代码,这里第一个字节不需要,也没有加密:


final String PATH = "/sdcard/" + IMEI + ".xml";  //put the downloaded file here
        try {
            URL url = new URL(context.getString(R.string.xml_feed) + IMEI + ".xml");
            enc_File = new File(PATH);
            long startTime = System.currentTimeMillis();

            /* Open a connection to that URL. */

            URLConnection ucon = url.openConnection();

            /*
             * Define InputStreams to read from the URLConnection.
             */
            InputStream is = ucon.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);

            /*
             * Read bytes to the Buffer until there is nothing more to read(-1).             */

            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;
            bis.skip(1);
            while ((current = bis.read()) != -1) {

                baf.append((byte) current);

            }

            /* Convert the Bytes read to a String. */

            FileOutputStream fos = new FileOutputStream(enc_File);
            fos.write(baf.toByteArray());
            fos.close();

        } catch (IOException e) {

        }

    }

这给了我下载的加密文件,所以我尝试使用以下代码解密这个文件:


         String bytes = toHex("the 16 bit key");
            Key skeySpec = new SecretKeySpec(toByte(bytes), "AES");
            Cipher c = Cipher.getInstance("AES/ECB/PKCS7Padding");  

            byte[] buf = new byte[1024]; 


            // Bytes read from in will be decrypted 

            InputStream inCipher = new FileInputStream(enc_File);
            OutputStream outCipher = new FileOutputStream(cipherFile);
            c.init(Cipher.DECRYPT_MODE, skeySpec);
            inCipher = new CipherInputStream(inCipher, c); // Read in the decrypted bytes and write the cleartext to out 
            int numRead = 0;            

                    while ((numRead = inCipher.read(buf)) >= 0) {
                          outCipher.write(buf, 0, numRead);
                } 
                outCipher.close(); 

上面应该给我一个新文件中的解密数据。

以下是该代码中用于为 SecretKeySpec 创建字节格式密钥的实用方法


public static byte[] toByte(String hexString) {
    int len = hexString.length()/2;
    byte[] result = new byte[len];
    for (int i = 0; i < len; i++)
        result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
    return result;
}

public static String toHex(byte[] buf) {
    if (buf == null)
        return "";
    StringBuffer result = new StringBuffer(2*buf.length);
    for (int i = 0; i < buf.length; i++) {
        appendHex(result, buf[i]);
    }
    return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

public static String toHex(String txt) {
    return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
    return new String(toByte(hex));
}

然而,这目前给了我以下例外:

10-12 11:19:26.337: WARN/System.err(5376): java.io.IOException: last block incomplete in decryption

加密文件下载正常,解密运行,但我得到上述异常并检查应解密的文件显示文件的第一行已正确解密,然后是接下来几行的一小部分,但随后返回垃圾其余的。

我现在被困在这个问题上,不知道在哪里寻找问题,有人可以帮忙吗?或者指出可能导致异常的方向?

其他信息:

10-12 15:30:37.291: WARN/System.err(6898):     at com.mypackage.net.SettingsProvisioner.getRoutingDoc(SettingsProvisioner.java:217)

以上行来自日志猫(stacktrace)中的异常

并且显示异常发生在这行代码:

while ((numRead = inCipher.read(buf)) >= 0) {

【问题讨论】:

    标签: java aes encryption ecb


    【解决方案1】:

    如果没有完整的堆栈跟踪,很难调试,我们将不得不重新编译您的代码,如果出于安全原因(很明显)您不能在此处发布它,请尝试使用 IBM stackanalyzer 或 lady4j 之类的堆栈跟踪分析器

    分析器适用于各种开发(包括 Android)

    【讨论】:

    • 我正在使用 Android,因此分析器无法正常工作,但我已经编辑了我的问题以包含更多信息
    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2011-04-11
    相关资源
    最近更新 更多