title: c# xor encrypt to javascript decrypt
body: <p>I'm testing a simple encrypt of string from c# and decrypt in javascript using xor.</p>
  
  <p>In C# i encode as follow:</p>
  
  <pre><code>private byte[] _Key { get; set; }
  private Encoding _Encoding { get; set; }
  
  public XOREncryption(string key) 
  {
      _Encoding = Encoding.UTF8;
      _Key = _Encoding.GetBytes(key).Where(a1 =&gt; a1 != 0).ToArray();
  }
  
  public string Encrypt(string plain_text)
  {
      int i = 0;
      byte[] octets = _Encoding
                      .GetBytes(plain_text)
                      .Select(b =&gt; (byte)(b ^ _Key[(++i) % _Key.Length]))
                      .ToArray()
                      ;
      string cipherText = Convert.ToBase64String(octets);
      return cipherText;
  }       
  </code></pre>
  
  <p>Using the following: </p>
  
  <pre><code>plainText = "The quick brown fox jumped over the lazy dog.";
  key = "19631280-2e7d-4ffe-8d80-f310c590d37e";
  </code></pre>
  
  <p>Results in:</p>
  
  <pre><code>encryptedText = bV5WEUNNWU5ZRVUWQkMIRgNCQERSRUAWVlUQDENcQkRHXwARVVdJSBJcX0oc
  </code></pre>
  
  <p>Now I would like to decrypt this in javascript:</p>
  
  <pre><code>function xor_string( str, key ) {
      var xored = "";
  
      for (i=0; i &lt; str.length;i++) {
          var a = str.charCodeAt(i);
          var b = a ^ key;
          xored = xored + String.fromCharCode(b);
      }
  
      return xored;
  }
  </code></pre>
  
  <p>But its returning the same value.  Example:</p>
  
  <pre><code>var key = "19631280-2e7d-4ffe-8d80-f310c590d37e"
  var data = "bV5WEUNNWU5ZRVUWQkMIRgNCQERSRUAWVlUQDENcQkRHXwARVVdJSBJcX0oc"
  
  var dd = xor_string(data, key);
  
  Output: bV5WEUNNWU5ZRVUWQkMIRgNCQERSRUAWVlUQDENcQkRHXwARVVdJSBJcX0oc
  
  dd = xor_string(key, data);
  
  Output: 19631280-2e7d-4ffe-8d80-f310c590d37e
  </code></pre>
  
  <p>Any ideas on what I'm doing wrong?</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>I may have phrased it incorrectly.  </p>
  
  <p>This is the encyrpted data that the c# method Encrypt() returned</p>
  
  <pre><code>var data = "bV5WEUNNWU5ZRVUWQkMIRgNCQERSRUAWVlUQDENcQkRHXwARVVdJSBJcX0oc"
  </code></pre>
  
  <p>I would like to use javascript function xor_string to decrypt it back to:</p>
  
  <pre><code>var dd = "The quick brown fox jumped over the lazy dog.";
  </code></pre>
  
  <p><strong>Update 2</strong>
  Ok I found my problem.  I was misunderstanding what xor is.  Jon Skeet tips made me look into it more.  My problem is my key is a string.  I have to get the integer value of it then use that value to xor the text.</p>
  
  <p>Here is a rough code I did.  Its rough but I wanted to see if I was understanding it.</p>
  
  <p>Here is the C# code:</p>
  
  <pre><code>        string plainText = "The quick brown fox jumped over the lazy dog.";
          string key = "19631280-2e7d-4ffe-8d80-f310c590d37e";
  
          int nKey = 0;
          string sKey = "";
          for(var x = 0; x &lt; key.Length; x++)
          {
              nKey += Convert.ToInt32(key[x]);
          }
  
          string nText = "";
          for (var x = 0; x &lt; plainText.Length; x++)
          {
              int charValue = Convert.ToInt32(plainText[x]);
              charValue ^= nKey;
  
              nText += char.ConvertFromUtf32(charValue);
          }
  
          nText = nText.Base64Encode();
  </code></pre>
  
  <p>The first for loop gets creates a int value of the key.  Then the second for loop does an xor of the text with the new int key.  Then I convert the xor text to base64.</p>
  
  <p>On the javascript side:</p>
  
  <pre><code>data = '4KWP4KWz4KW+4KS74KWq4KWu4KWy4KW44KWw4KS74KW54KWp4KW04KWs4KW14KS74KW94KW04KWj4KS74KWx4KWu4KW24KWr4KW+4KW/4KS74KW04KWt4KW+4KWp4KS74KWv4KWz4KW+4KS74KW34KW64KWh4KWi4KS74KW/4KW04KW84KS1';
  key = "19631280-2e7d-4ffe-8d80-f310c590d37e";
  
  
  function xor_string( str, key ) {
      var xored = "";
  
      for (i=0; i &lt; str.length;i++) {
          var a = str.charCodeAt(i);
          var b = a ^ key;
          xored = xored + String.fromCharCode(b);
      }
  
      return xored;
  }
  
  function xor_key(key)
  {
      var keyVal = 0;
      for(x=0; x &lt; key.length; x++)
      {
          keyVal += key.charCodeAt(x);
      }
  
      return keyVal;
  }
  
  var bdecode = $().base64('decode', data);
  var newKey = xor_key(key);
  var dd = xor_string(bdecode, newKey);
  </code></pre>
  
  <p>First I base64 decode the data.  Then the first loop recreates the key the second does the xor on the key </p>
  
  <p>And now it works.  I know the fix is rough but I this helped me think it out.
  Is there a way on C# to convert the key to its int value without having to loop.
  I tried the following:</p>
  
  <p>byte[] asciiBytes = Encoding.ASCII.GetBytes(key);
     int d1 = BitConverter.ToInt16(asciiBytes, 0);
     But this returns a different number then my loop above.  I also tried:</p>
  
  <p>if (BitConverter.IsLittleEndian)
         Array.Reverse(asciiBytes);</p>
  
  <p>but value is still wrong for the key </p>