IBR-DTNSuite  0.8
ibrcommon/ibrcommon/ssl/XORStream.cpp
Go to the documentation of this file.
00001 /*
00002  * XORStream.cpp
00003  *
00004  *  Created on: 13.07.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/ssl/XORStream.h"
00009 
00010 namespace ibrcommon
00011 {
00012         XORStream::XORStream(std::ostream &stream, const CipherMode mode, std::string key)
00013          : CipherStream(stream, mode), _key(key), _key_pos(0)
00014         {
00015         }
00016 
00017         XORStream::~XORStream()
00018         {
00019         }
00020 
00021         void XORStream::encrypt(char *buf, const size_t size)
00022         {
00023                 const char *keydata = _key.c_str();
00024                 const size_t keylength = _key.length();
00025 
00026                 for (size_t i = 0; i < size; i++)
00027                 {
00028                         buf[i] = buf[i] ^ keydata[_key_pos];
00029                         _key_pos++;
00030                         if (_key_pos > keylength) _key_pos = 0;
00031                 }
00032         }
00033 
00034         void XORStream::decrypt(char *buf, const size_t size)
00035         {
00036                 const char *keydata = _key.c_str();
00037                 const size_t keylength = _key.length();
00038 
00039                 for (size_t i = 0; i < size; i++)
00040                 {
00041                         buf[i] = buf[i] ^ keydata[_key_pos];
00042                         _key_pos++;
00043                         if (_key_pos > keylength) _key_pos = 0;
00044                 }
00045         }
00046 }