我一直負責解密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