Citadel's VisualEncrypt and VisualDecrypt I was skimming through this paper out of boredom. On pages 5 and 6, the paper shows a simple encryption/decryption scheme that Citadel uses to obfuscate data. The algorithm basically XORs the next character with the previous character. The code is roughly this (ugly, I know):


void VisualEncrypt (void *buffer, unsigned size) {
  for (unsigned i = 1; i &lt; size; ++i) {
      ((unsigned char *) buffer) [i] ^= ((unsigned char *) buffer) [i - 1] ;
  }
}

void VisualDecrypt (void *buffer, unsigned size) {
  if (!size) {
      return ;
  }

  for (unsigned i = size - 1; i &gt; 0; --i) {
      ((unsigned char *) buffer) [i] ^= ((unsigned char *) buffer) [i - 1] ;
  }
}



I decided to make a more generic version of this code so that it could work on different containers and values:

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualEncrypt (InputIterator begin, InputIterator end, OutputIterator destination)
{
    if (begin == end) {
        return destination ;
    }

    auto value = *begin ;
    *destination++ = value ;

    while (++begin != end) {
        value ^= *begin ;
        *destination++ = value ; 
    }

    return destination ;
}

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualDecrypt (InputIterator begin, InputIterator end, OutputIterator destination)
{
    if (begin == end) {
        return destination ;
    }

    auto value = *begin ;
    *destination++ = value ;

    while (++begin != end) {
        auto temp = *begin ;
        *destination++ = value ^ temp ;
        value = temp ;
    }

    return destination ;
}

template &lt;class Iterator&gt;
Iterator VisualEncrypt (Iterator begin, Iterator end)
{
    return VisualEncrypt (begin, end, begin) ;
}

template &lt;class Iterator&gt;
Iterator VisualDecrypt (Iterator begin, Iterator end)
{
    return VisualDecrypt (begin, end, begin) ;
}


Here are two sample cases of it being used:

#include &lt;iterator&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

int main ()
{
    // Test char
    std::string plaintext = "Please encrypt me!" ;

    std::vector &lt;unsigned char&gt; cyphertext ;
    VisualEncrypt (plaintext.cbegin (), plaintext.cend (), std::back_inserter (cyphertext)) ;

    std::string decrypttext ;
    VisualDecrypt (cyphertext.cbegin (), cyphertext.cend (), std::back_inserter (decrypttext)) ;

    // Test long
    long arr [] = {1, 2, 3, 4, 5} ;
    const auto size = sizeof (arr) / sizeof (arr [0]) ;
    VisualEncrypt (arr, arr + size) ;
    VisualDecrypt (arr, arr + size) ;

    return 0 ;
}


The code seems to work fine. I want to make sure I'm not misusing the iterators and invoking undefined behavior. I couldn't find a clean way to simplify any of this with the &lt;algorithm&gt; library. Any tips on that would be great too.
The way you handle the first element as a special case is cumbersome: you're basically unrolling the first iteration of the loop.  You could just initialize value = 0 so that the first XOR just copies the input to the output.

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualEncrypt (InputIterator begin, InputIterator end, OutputIterator destination)
{
    typedef typename std::iterator_traits&lt;InputIterator&gt;::value_type T;
    T value = 0;
    while (begin != end) {
        value ^= *begin++;
        *destination++ = value ;
    }

    return destination ;
}

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualDecrypt (InputIterator begin, InputIterator end, OutputIterator destination)
{
    typedef typename std::iterator_traits&lt;InputIterator&gt;::value_type T;
    T value = 0;

    while (begin != end) {
        T temp = *begin++;
        *destination++ = value ^ temp ;
        value = temp ;
    }

    return destination ;
}


Alternatively, use a for-loop, which results in slightly more compact code and gets the pesky side-effect ++ operators out of the way.

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualEncrypt(InputIterator begin, InputIterator end, OutputIterator destination)
{
    typedef typename std::iterator_traits&lt;InputIterator&gt;::value_type T;
    for (T value = 0; begin != end; ++begin, ++destination) {
        *destination = (value ^= *begin);    // &lt;-- Probably controversial one-liner
    }

    return destination;
}

template &lt;class InputIterator, class OutputIterator&gt;
OutputIterator VisualDecrypt(InputIterator begin, InputIterator end, OutputIterator destination)
{
    typedef typename std::iterator_traits&lt;InputIterator&gt;::value_type T;
    for (T value = 0; begin != end; ++begin, ++destination) {
        T temp = *begin;
        *destination = value ^ temp;
        value = temp;
    }

    return destination;
}