Author Topic: C++ Xor System  (Read 1902 times)

0 Members and 1 Guest are viewing this topic.

Offline bitchSticks

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
C++ Xor System
« on: November 12, 2014, 03:49:53 pm »
I have tried to create a system that takes in data turns it into binary or ones and zeros. Then runs it throught an xor system and then puts it into a text document. It is also build to do the reverse or at least planed to. It takes in xor data then decodes it into binary then into text.
I have had issue after issue with trying to get this to work.
If anyone could help it would be really awsome if not then I will just leave this project.

http://pastebin.com/Pef7CCua

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: C++ Xor System
« Reply #1 on: November 13, 2014, 12:31:51 am »
Okay, I really want to go through this whole thing and tear it apart so that you understand everything about it and the mistakes that're holding you back.  I don't mean that to sound harsh, and I apologize if it does, I just mean to say that there are issues in this code that indicate that you're missing some vital information that prevents you from doing this correctly.

However, the biggest problem that I see is that, well... I don't think you really understand how XOR-based encryption works.  So as a first step in helping, I'm going to give you the briefest of notes about this code and explain the most basic idea of XOR-based encryption.  You can then look up additional information and figure out how to do it.  If you still have trouble or you just want a more complete explanation, just ask(reply) and I'll happily put all my long-windedness behind a nice & long post explaining every aspect in as much detail as possible.

So, for the sake of sanity and an attempt at brevity...

XOR-based encryption is a byte-comparison.  Bytes: C = A ^ B.  Or, alternatively, bytes: A ^= B.  To XOR an entire string with a key(as a std::string), you do have a few choices but a common method is: XOR each byte of the string with a single corresponding byte of the key.

Code: (c++) [Select]
do {
  text[i] ^= key[i % key.size()];
} while (++i < text.size());

Now for other notes on the code...

First, start over.  I'm sorry to say but the code you've written so far has grown in complexity such that the idea seems to be lost.  It would be best to start it over and get the XOR encryption working first and then add file I/O.  Multiple functions are nice but this whole process could be done in 30 lines or less including header declarations.

Next, you don't need to include so many different headers.  Since it appears that you plan to do file I/O, <fstream> makes sense.  However, you don't need the C library stuff so <cstdlib>, <cstddef>,<cstdio>(aka <stdio.h>), and <ctime> are not needed.  As well, you don't need <bitset> and you probably don't need to use std::numeric_limits so <limits> is likely unnecessary.  You're not using any vectors or maps so <vector> & <map> can be left out too.  Also, drop the Windows-specific stuff as it's just a hindrance; no <windows.h> or <mmsystem.h>(not sure why you had that in there anyway).  So you should only need the following:

Code: (c++) [Select]
#include <iostream>  // for cin/cout/cerr streams & such
#include <string>      // optional but recommended since you're using std::string
#include <fstream>   // ifstream/ofstream

If you have other questions or, as I mentioned, want a more complete explanation of things then just ask.  Good luck && have fun.
-Xires

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: C++ Xor System
« Reply #2 on: November 14, 2014, 03:04:50 pm »
I'm sure some of us would love to read your long-windedness in an XOR explaination/tutorial post or whatever else you had in mind.  It could help OP start from scratch and I'd surely be interested :D

If you find the time and want to write one up (I'm not sure how long or in-depth you're planning to go, so I don't know how much effort this would be lol) some of us would definitely read it.

Also, OP...  Why did you include all of those headers?
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: C++ Xor System
« Reply #3 on: November 15, 2014, 01:58:26 am »
:S My apologies for the satirical nature of my comments, but C(++) makes this rather easy to achieve due to their having a bitwise exclusive OR operation built in, using either the ^ operator, or the XOR keyword.

Code: [Select]
//Dis shit is pseudocode y'all
#include <iostream> //If you dont know what did is for...
#include <iso646.h> // you need dis for the xor keyword, in some cases
int main()
{
unsigned short key = 0xAAAA; //Make these unsigned or be forever hated by xires <3
unsigned short phrase = 0xDDDD;
std::cout << std::hex << key xor phrase << endl; // You could loop dis shit rather easily y'all
return 0;
}
I wanted to comment a long time ago but bit my figurative tongue.
« Last Edit: November 15, 2014, 02:01:50 am by HTH »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women