Guest User

Untitled

a guest
Jan 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. if ((cordova.platformId == "windows")) {
  2. var Cryptography = Windows.Security.Cryptography;
  3. //Encrypt the msg string
  4. function protectData(msg) {
  5. var provider = new Cryptography.DataProtection.DataProtectionProvider("LOCAL=user");
  6. var encoding = Cryptography.BinaryStringEncoding.utf8;
  7. var buffMsg = Cryptography.CryptographicBuffer.convertStringToBinary(msg, encoding);
  8. provider.protectAsync(buffMsg).done(function (buffData) {
  9. //the encryption has succeeded
  10. writeToFile(buffData);
  11. });
  12. }
  13. //decrypt the buffProtected buffer
  14. function unprotectData(buffProtected) {
  15. var provider = new Cryptography.DataProtection.DataProtectionProvider("LOCAL=user");
  16. var encoding = Cryptography.BinaryStringEncoding.utf8;
  17. // Decrypt the protected message specified on input.
  18. provider.unprotectAsync(buffProtected).then(function (data) {
  19. var stringMsg = Cryptography.CryptographicBuffer.convertBinaryToString(Windows.Security.Cryptography.BinaryStringEncoding.utf8, data);
  20. document.getElementById("divResult").innerText = stringMsg;
  21. }, function (error) {
  22. console.log(error);
  23. });
  24. }
  25.  
  26. function writeToFile(buffProtected) {
  27. Windows.Storage.ApplicationData.current.localFolder.createFileAsync("test.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
  28. .then(function (storageFile) {
  29. Windows.Storage.FileIO.writeBufferAsync(storageFile, buffProtected).done(function(){});
  30. }, function (error) {
  31. console.log(error);
  32. })
  33. }
  34.  
  35. function readFromFile() {
  36. Windows.Storage.ApplicationData.current.localFolder.getFileAsync("test.txt").then(function (storageFile) {
  37. var abc = storageFile;
  38. Windows.Storage.FileIO.readBufferAsync(storageFile).then(function (buffData) {
  39. //decrypt the data
  40. unprotectData(buffData);
  41. });
  42. }, function (error) {
  43. console.log(error);
  44. });
  45. }
  46.  
  47.  
  48. document.getElementById("btnEncrypt").onclick = function () {
  49. var data = document.getElementById("txtData").value;
  50. protectData(data);
  51. }
  52.  
  53. document.getElementById("btnDecrypt").onclick = function () {
  54. readFromFile(unprotectData)
  55. }
  56. }
  57.  
  58. <input type="text" id="txtData" placeholder="ToProtected Data" />
  59. <button id="btnEncrypt">Encrypt Data</button>
  60. <button id="btnDecrypt">Decrypt Data</button>
  61. <div id="divResult"></div>
Add Comment
Please, Sign In to add comment