title: bitwise XOR a string in Bash
body: <p>I am trying to accomplish a work in Bash scripting. I have a string which i want to XOR with my key. </p>
  
  <pre><code>#!/bin/sh
  PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH
  
  teststring="abcdefghijklmnopqr"
  </code></pre>
  
  <p>Now how do i XOR the value of teststring and store it in a variable using bash?</p>
  
  <p>Any help will be appreciated.</p>
  
  <p>Basically i am trying to duplicate the result of follwing VB Script:</p>
  
  <pre><code>Function XOREncryption(CodeKey, DataIn)
  
  Dim lonDataPtr
  Dim strDataOut
  Dim temp
  Dim tempstring
  Dim intXOrValue1
  Dim intXOrValue2
  
  
  For lonDataPtr = 1 To Len(DataIn) Step 1
      'The first value to be XOr-ed comes from the data to be encrypted
      intXOrValue1 = Asc(Mid(DataIn, lonDataPtr, 1))
      'The second value comes from the code key
      intXOrValue2 = Asc(Mid(CodeKey, ((lonDataPtr Mod Len(CodeKey)) + 1), 1))
  
      temp = (intXOrValue1 Xor intXOrValue2)
      tempstring = Hex(temp)
      If Len(tempstring) = 1 Then tempstring = "0" &amp; tempstring
  
      strDataOut = strDataOut + tempstring
  Next
  XOREncryption = strDataOut
  End Function
  </code></pre>