Encode/Encrypt – using Javascript and WebViewString

This one probably has a multitude of uses, many of which I have not thought of yet, but use for passwords, creating secret data for exchange with a php server, lock down your app if it contains sensitive data… this list will go on.

I found the code on stack overflow whilst looking for something completely different – so all credits must go there. I initially looked to replicate the script with blocks, but came up against the vastness of the UTF-16 character set, and decided it would be better / easier to use an html page and webviewstring.

In essence you have a single script that can both encode (encrypt?) and decode (decrypt?) a string using a number code. In my example I have both the string and the number code as variables, therefore nothing is stored in the app to allow decoding / decryption. You can of course hard code the number or the string or both if you wish (not much point in that), but with some creative thinking the script can be used for all manner of things.


HTML

<!DOCTYPE html>

<html>

<head><title>jsEncode</title></head>

<body>

<script type="text/javascript">

var str = window.AppInventor.getWebViewString().split("||")[0];

var encoded = "";

var numberCode = window.AppInventor.getWebViewString().split("||")[1];

for (i=0; i<str.length;i++) {

var a = str.charCodeAt(i);

var b = a ^ numberCode;

encoded = encoded+String.fromCharCode(b);

}

window.AppInventor.setWebViewString(encoded);

</script>

</body>

</html>