Skip to content

Instantly share code, notes, and snippets.

@itorain
Last active April 5, 2019 18:10
Show Gist options
  • Save itorain/6131ef52e694692a9d0557ccfb9b2641 to your computer and use it in GitHub Desktop.
Save itorain/6131ef52e694692a9d0557ccfb9b2641 to your computer and use it in GitHub Desktop.
C++ LCG
#include <iostream>
#include <memory>
using namespace std;
typedef unsigned char BYTE;
int M = 0xA5;
int A = 0xc9;
int N = 256;
int init = 0x55;
BYTE *LCG(BYTE *data, int dataLength, BYTE seed) {
int prev;
BYTE *val = new BYTE[dataLength];
for (int i = 0; i < dataLength; i++) {
if (i == 0) {
prev = ((M * (int)seed) + A) % N;
}
else {
prev = ((M * (int)prev) + A) % N;
}
val[i] = (BYTE)prev^data[i];
}
return val;
}
int main(int argc, char* argv[]) {
BYTE word[] = "apple";
BYTE *x = LCG(word, 5, (BYTE)init);
BYTE keyedWord[] = {0xF3, 0x93, 0x68, 0x2D, 0xCB};
for (int i = 0; i < 5; i++) {
cout << hex << (int)x[i] << endl;
}
BYTE *y = LCG(x, 5, (BYTE)init);
for (int i = 0; i < 5; i++) {
cout << y[i] << endl;
}
delete x;
delete y;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment