我一直负责解密Java中的文件已经使用以下标准加密:爪哇AES解密问题
AES与128位的密钥,ECB模式和PKCS7填充加密算法。 加密文件格式是: - 第一个字节是十六进制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();
上面应该给我一个新的文件中decryted数据。
这里是在代码中创建字节格式的密钥对SecretKeySpec的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 = "ABCDEF";
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)
上面一行是从在日志猫除外(栈跟踪)
它表明该异常在这行代码存在的:
while ((numRead = inCipher.read(buf)) >= 0) {
我工作在Android这样的分析仪无法正常工作,但我已经编辑我的问题包括一些更多的信息 – 2010-10-12 14:33:36