Java AES 解密问题

标签 java aes encryption ecb

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

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

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

下载代码,我在这里跳过第一个字节,因为它不是必需的,也没有加密:


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 创建字节格式 key 的 util 方法


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)

上面一行来自日志cat(stacktrace)中的异常

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

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

最佳答案

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

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

关于Java AES 解密问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3915644/

相关文章:

php - VB.Net 应用程序 + PHP 网站均连接到 MySQL

java - 使用HttpPost提交表单登录后读取URL的HttpResponse

带参数的 Java 私有(private)构造函数

java - 无法让二维数组进行洗牌

java - 将 "strong"JRE 策略文件与 BouncyCaSTLe 一起使用

java - 非法 block 大小异常 : last block incomplete in decryption in android

java - 如何创建有状态的 Web 服务?

objective-c - 在 Objective-c iOS 应用程序中,我应该在哪里调用计算函数?

java - 使用 RSA key 加密 AES key

Python 3 - 从 X509 证书中提取公钥并用它加密