Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var keytok, ivtok, key, key1, key2, iv, encpass;
  2.  
  3. var host;
  4.  
  5. function storePass(encpass) {
  6.     $.ajax({
  7.         type: 'POST',
  8.         url : 'example.php',
  9.         data: { host: host, save: encpass },
  10.         success: function(data, statuscode) {
  11.                      console.log('Password saved: ' + statuscode);
  12.                  },
  13.         error:   function() {
  14.                      console.log('Unable to save password.');
  15.                  }
  16.     });
  17. }
  18.  
  19. function startCrypt() {
  20.     var crypt;
  21.     host = document.getElementById("hostfield").value;
  22.  
  23.     $.ajax({
  24.         type: 'POST',
  25.         url:  'encrypt.php',
  26.         data: { host: host },
  27.         success: function(data, statuscode) {
  28.                      var encpass = '';
  29.  
  30.                      crypt = data;
  31.                      crypt = JSON.parse(crypt);
  32.                      console.log('crypt is: "' + crypt + '"');
  33.                      crypt.key = crypt.key; //.toString(CryptoJS.enc.Latin1);
  34.                      crypt.iv  = crypt.iv; //.toString(CryptoJS.enc.Latin1);
  35.                      encpass = CryptoJS.AES.encrypt( getPass(), crypt.key, { iv: crypt.iv } );
  36.                      console.log("Key: '" + crypt.key + "'");
  37.                      console.log("IV: '"  + crypt.iv  + "'");
  38.                      console.log("Encrypted Password: '" + encpass + "'");
  39.                      storePass(encpass);
  40.                  },
  41.         error:   function() {
  42.                      console.log('No data returned.');
  43.                      crypt = null;
  44.                  }
  45.     });
  46. }
  47.  
  48. function decrypt() {
  49.     if(typeof iv === 'undefined' || typeof key === 'undefined') {
  50.         console.log("Error decrypting. IV or Key is undefined.");
  51.         return;
  52.     }
  53.  
  54.     $.ajax({
  55.         type: 'POST',
  56.         url:  'decrypt.php',
  57.         success: function(data, statuscode) {
  58.                      encpass = (JSON.parse(data)).encpass;
  59.            
  60.                      var decrypted = CryptoJS.AES.decrypt(
  61.                         encpass,
  62.                         key,
  63.                         { iv: iv }
  64.                      );
  65.                      decrypted = decrypted.toString(CryptoJS.enc.Utf8);
  66.                      console.log("Decrypted Password: '" + decrypted + "'");
  67.                  },
  68.          error:  function() {
  69.                      console.log("Error decrypting. Could not retrieve encrypted password.");
  70.                  }
  71.     });
  72. }
  73.  
  74. function tokenRequest(type) {
  75.     if(type != "key" && type != "iv") {
  76.         console.log('ffs');
  77.         return;
  78.     }
  79.  
  80.     $.ajax({
  81.         type: 'POST',
  82.         url:  './api/token/',
  83.         data: { tokType: type, host: host },
  84.         success: function(data, statuscode) {
  85.                      if(type == "key") {
  86.                          keytok = data;
  87.                          console.log(keytok);
  88.                      }
  89.                      else {
  90.                          ivtok = data;
  91.                          console.log(ivtok);
  92.                      }
  93.                  },
  94.         error:   function() {
  95.                      console.log('Something terrible happened.');
  96.                  }
  97.     });
  98. }
  99.  
  100. function getPass() {
  101.     return document.getElementById("passfield").value;
  102. }
  103.  
  104. function keyRequest() {
  105.     $.ajax({
  106.         dataType:      'jsonp',
  107.         jsonp:         'keyReq:' + keytok,
  108.         jsonpCallback: host,
  109.         url:           'http://localhost:2343',
  110.     });
  111. }
  112.  
  113. function ivRequest() {
  114.     $.ajax({
  115.         dataType:      'jsonp',
  116.         jsonp:         'ivReq:' + ivtok,
  117.         jsonpCallback: host,
  118.         url:           'http://localhost:2344',
  119.     });
  120. }
  121.  
  122. function procKey(data) {
  123.     if('error' in data) {
  124.         console.log(data.error);
  125.     }
  126.     else if('keys' in data) {
  127.         key1 = data.keys[0];
  128.         key2 = data.keys[1];
  129.         key  = makeKeys();
  130.         console.log(key);
  131.     }
  132.     else {
  133.         console.log("Undefined Behaviour");
  134.     }
  135. }
  136.  
  137. function procIv(data) {
  138.     if('error' in data) {
  139.         console.log(data.error);
  140.     }
  141.     else if('iv' in data) {
  142.         iv = data.iv;
  143.         console.log(iv);
  144.     }
  145.     else {
  146.         console.log("Undefined Behaviour");
  147.     }
  148. }
  149.  
  150. function makeKeys(type) {
  151.     console.log('making the keys');
  152.     var fullKey = '';
  153.  
  154.     $.ajax({
  155.         type: 'POST',
  156.         url:  './api/key/',
  157.         data: { key1: key1, key2: key2 },
  158.         success: function(data, statuscode) {
  159.                      fullKey = data;
  160.                      console.log(data);
  161.                  },
  162.         error:   function() {
  163.                      console.log('Something terrible happened.');
  164.                  }
  165.     });
  166.  
  167.     return fullKey;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement