I'm not sure what I'm trying to accomplish, hell I dont even know how true encryption is suppose to work. I'm going to put things on halt until I get some concepts to work with now that I understand bitfields got JoshR's union idea down pact. Gonna go look up some tutorials.
I uses JoshR's example to show how the bitfield struct in the union could be used to mix around the binary of the other methods in the union. No idea for what purpose, honestly.
Code:
#include <iostream>
#include <cstdio>
using namespace std;
struct char4 {
unsigned int a:2;
unsigned int b:2;
unsigned int c:2;
unsigned int d:2;
};
union TEST {
char A[]; // the data to binary edit
char4 bits[]; // the bitfields used to do so
};
int main() {
TEST ch;
strcpy(ch.A, "This is a string.\0");
cout << "Size of TEST: (unable to determine atm)" << endl;;
cout << "String A: " << strlen(ch.A) << endl;
// change every second bit, if its 1 it becomes 0, or visa-versa
for( int x = 0; x < strlen(ch.A); ++x ) {
ch.bits[x].a = ch.bits[x].a ^ (1 << 1);
ch.bits[x].b = ch.bits[x].b ^ (1 << 1);
ch.bits[x].c = ch.bits[x].c ^ (1 << 1);
ch.bits[x].d = ch.bits[x].d ^ (1 << 1);
}
cout << "String A: " << ch.A << endl;
// return the string back to the way it was
for( int x = 0; x < strlen(ch.A); ++x ) {
ch.bits[x].a = ch.bits[x].a ^ (1 << 1);
ch.bits[x].b = ch.bits[x].b ^ (1 << 1);
ch.bits[x].c = ch.bits[x].c ^ (1 << 1);
ch.bits[x].d = ch.bits[x].d ^ (1 << 1);
}
cout << "String A: " << ch.A << endl;
cin.get();
}
A std::string wouldnt work because of errors, so I used a char string. If you had some class that imported data and assigned it to some space just like where that char string is.. Might work, might not work, definetly needs expansion but in any case thats 1 thing I was trying to do.. but dont care to spend anymore time mixing the binary around for fun because I know this encryption could be more efficient if I knew more concepts.
Ah apparently using XOR you could encrypt it using anything, so I guess this implement might not be so good.. Got anymore ideas JoshR? I didnt know what you were talking about at the beginning of this thread because I had never seen this before, but I remember at the beginning of the thread you were on about a union and XOR and thats finally where I am