我有一个应用程序,使用Webview来显示内容,并且JavaScript调用是我的应用程序的控制器。为了提供一定程度的安全性,我混淆了代码。但这还不够,因为我希望加密HTML和JS文件,然后在运行时解密它们。我使用RC4算法加密这些资源,并将apk文件打包。在加载文件时,我解密JavaScript文件,加载它们,然后解密HTML文件并加载它。然而,这样做不起作用,因为Web内容会以“data:text/html”的形式显示消息,可能是暂时关闭或已永久移动等等。
我的问题是: 1.如何使位于资产文件夹中的HTML和JavaScript文件安全,以免被访问? 2.如果我的方法正确,是否有人知道我错在哪里?
谢谢!
以下是解密和加载资源的代码:
我的问题是: 1.如何使位于资产文件夹中的HTML和JavaScript文件安全,以免被访问? 2.如果我的方法正确,是否有人知道我错在哪里?
谢谢!
以下是解密和加载资源的代码:
protected void loadWebContent() {
checkEncryptionEnabled();
loadJSFiles();
logger.info("Loaded js ... going for html");
loadAssetFile("www/index.html", "text/html");
}
private void loadJSFiles() {
String[] jsFilesArray = { "app.js", "iscroll.js", "iui.js", "json.js" };
for (String js : jsFilesArray) {
loadAssetFile("www/js/" + js, "application/javascript");
}
}
private void loadAssetFile(String filePath, String mimeType) {
AssetManager assetMgr = getAssets();
InputStream is = null;
try {
is = assetMgr.open(filePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] temp = new byte[512];
int bytesRead = -1;
while ((bytesRead = is.read(temp)) > 0) {
baos.write(temp, 0, bytesRead);
}
byte[] encrypted = baos.toByteArray();
String content = null;
/**
* true
* */
if (Config.ENCRYPTION_ENABLED) {
byte[] decrypted = new RC4Encrypter("rc4_key").rc4(encrypted);
content = new String(decrypted, "utf-8");
} else {
content = new String(encrypted, "utf-8");
}
/**
* The webview to use
* */
if("application/javascript".equals(mimeType)) {
webContent.loadUrl("javascript:" + content);
} else {
webContent.loadData(content, mimeType, "utf-8");
}
} catch (IOException ex) {
logger.error(null, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
}