Encrypting locally stored data using JavaScript

While I am learning JavaScript and HTML5, I am trying to create a basic quiz application that asks several questions with several answers that will work on the mobile network, as well as as an application using PhoneGap. When questions are asked, the results are saved locally.

I want the PhoneGap version to allow offline mode , so the ability to store data locally should . I know that there is a local database offered through PhoneGap, so I assume that one of the options is the client / server for Mobile Web and the local database using PhoneGap. However, I would prefer not to go down this route for now, as it would mean that I would have to manage a lot of options between the mobile Internet and PhoneGap versions.

Obviously, there is no need for the level of security of Internet banking, but I need results that will be stored locally, which cannot be easily read, but, most importantly, are processed.

At first I tried to use HTML5 localstorage , but I quickly realized that at least the way I did it, I could see all the results that I stored, and with the Chrome Developer Tools, you can easily click to change the values.

When I go along the path of using encryption ( https://stackoverflow.com/a/3/433017/... ), it seems like for something like this I always need to define a “key” somewhere in the code to encrypt the data, and then use that the same key to decrypt it.

Since all the data is stored on the client side, this means that all I will ever need is to find this key and run it against the saved data to manage the results.

+4
source share
2 answers

Will base64 encoding work? It is built into the browser and looks encrypted. People do this all the time for cookies.

Resources

(defined by Mozilla):

See this question for more information and links for browsers without Mozilla: JSON encode / decode base64 encode / decode in JavaScript

0
source

CryptoJS AES. Thanks Lee

var text = "#rawString#"; var key = CryptoJS.enc.Base64.parse("#base64Key#"); var iv = CryptoJS.enc.Base64.parse("#base64IV#"); console.log("Initial String:: "+text); var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv}); console.log("Encrypted String:: "+encrypted.toString()); var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv}); console.log("Decrypted String:: "+decrypted.toString(CryptoJS.enc.Utf8)); 

Demo link Plnkr

0
source

Source: https://habr.com/ru/post/1447411/


All Articles