<commit_before>/*
  An implementation of the highly secure (not) XOR cipher. AKA, how to write
  and use your own cipher object. DO NOT make up your own ciphers. Please.

  Written by <NAME> (<EMAIL>) on Feb 17, 2004

  This file is in the public domain
*/
#include <botan/base.h>
#include <botan/init.h>
using namespace Botan;

class XOR_Cipher : public StreamCipher
   {
   public:
      // what we want to call this cipher
      std::string name() const { return "XOR"; }
      // return a new object of this type
      StreamCipher* clone() const { return new XOR_Cipher; }
      // StreamCipher() can take a number of args, which are:
      //   min keylen, max keylen, keylength mod, iv size
      //   In this case we just pass min keylen, which means the
      //   only keysize we support is 1 byte, and don't use an IV.
      XOR_Cipher() : StreamCipher(1) { mask = 0; }
   private:
      void cipher(const byte[], byte[], u32bit);
      void key(const byte[], u32bit);
      byte mask;
   };

void XOR_Cipher::cipher(const byte in[], byte out[], u32bit length)
   {
   for(u32bit j = 0; j != length; j++)
      out[j] = in[j] ^ mask;
   }

void XOR_Cipher::key(const byte key[], u32bit)
   {
   /* We know length == 1 because it is checked in set_key against the
      constraints we passed to StreamCipher's constructor. In this case,
      we said: "All keys are of length 1 byte and no other length".

      An excercise for the reader would be to extend this to support
      arbitrary length (for arbitrary in the range 1-32) keys.
   */
   mask = key[0];
   }

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>

#include <botan/look_add.h>
#include <botan/lookup.h>
#include <botan/filters.h>

int main()
   {
   LibraryInitializer init;

   add_algorithm(new XOR_Cipher); // make it available to use
   add_alias("Vernam", "XOR");  // make Vernam an alias for XOR

   SymmetricKey key("42"); // a key of length 1, value hex 42 == dec 66

   /*
    Since stream ciphers are typically additive, the encryption and
    decryption ops are the same, so this isn't terribly interesting.
<commit_msg>Extend the xor_ciph example to support longer keys, and update it to use the new entry point for add_alias<commit_after>/*
  An implementation of the highly secure (not) XOR cipher. AKA, how to write
  and use your own cipher object. DO NOT make up your own ciphers. Please.

  Written by <NAME> (<EMAIL>) on Feb 17, 2004

  This file is in the public domain
*/
#include <botan/base.h>
#include <botan/init.h>
using namespace Botan;

class XOR_Cipher : public StreamCipher
   {
   public:
      // what we want to call this cipher
      std::string name() const { return "XOR"; }

      // return a new object of this type
      StreamCipher* clone() const { return new XOR_Cipher; }

      XOR_Cipher() : StreamCipher(1, 32) { mask_pos = 0; }
   private:
      void cipher(const byte[], byte[], u32bit);
      void key(const byte[], u32bit);

      SecureVector<byte> mask;
      u32bit mask_pos;
   };

void XOR_Cipher::cipher(const byte in[], byte out[], u32bit length)
   {
   for(u32bit j = 0; j != length; j++)
      {
      out[j] = in[j] ^ mask[mask_pos];
      mask_pos = (mask_pos + 1) % mask.size();
      }
   }

void XOR_Cipher::key(const byte key[], u32bit length)
   {
   mask.set(key, length);
   }

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>

#include <botan/look_add.h>
#include <botan/lookup.h>
#include <botan/filters.h>
#include <botan/config.h>

int main()
   {
   LibraryInitializer init;

   add_algorithm(new XOR_Cipher); // make it available to use
   global_config().add_alias("Vernam", "XOR"); // make Vernam an alias for XOR

   // a hex key value
   SymmetricKey key("<KEY>");

   /*
    Since stream ciphers are typically additive, the encryption and
    decryption ops are the same, so this isn't terribly interesting.

{'fraction_non_alphanumeric': 0.1106899166034875, 'fraction_numerical': 0.014404852160727824, 'mean_word_length': 3.022357723577236, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3795923', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1292, 'n_words': 489}