Skip to content

Instantly share code, notes, and snippets.

@nobody
Created October 7, 2010 16:59
Show Gist options
  • Save nobody/615454 to your computer and use it in GitHub Desktop.
Save nobody/615454 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int f1(int i, int j, char* key, int keylen){
return (key[i] ^ key[j]) % keylen;
}
int g1(int i, int j, char* key, int keylen){
return (key[i] * key[j]) % keylen;
}
char* getCol( char** buf, char* out, int numrows, int colidx){
for (int i = 0; i < numrows; ++i){
out[i] = buf[i][colidx];
}
}
int encrypt(char* key, int keylen, char* filename){
char** buffer = new char*[keylen];
for (int i = 0; i < keylen; ++i)
buffer[i] = new char[keylen+1];
FILE* infile = fopen(filename, "r");
if (!infile){
perror("fopen");
return -1;
}
// prepare the first row
char* row1 = new char[keylen+1];
strcpy(row1, key);
bool done = false;
while(!done){
int numRows = 0;
int padding = 0;
for (int i = 0; i < keylen; ++i){
int br = fread(buffer[i], sizeof(char), keylen, infile);
if (br == 0){
// no data on the row
done = true;
break;
} else if (br == keylen){
// we got a full row!
numRows++;
continue;
} else {
// pad to full row
int pad = keylen - br;
memset(&(buffer[i][br]), 0x00, pad);
padding += pad;
numRows++;
done = true;
}
}
if (done && numRows == 0)
break;
// horizontal pass
for (int j = 0; j < keylen; ++j){
int f = f1(0, j, key, numRows);
int g = g1(0, j, key, numRows);
int x1i = (j-f)%numRows;
int x2i = (j-g)%numRows;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^buffer[j][0];
buffer[j][0] = (char)x;
}
for (int i = 1; i < keylen; ++i){
for (int j = 0; j < numRows; ++j){
char* prevcol = new char[numRows];
getCol(buffer, prevcol, numRows, i-1);
int f = f1(i, j, key, numRows);
int g = g1(i, j, key, numRows);
int x1i = (j-f)%numRows;
int x2i = (j-g)%numRows;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^(buffer[j][i]);
buffer[j][i] = (char)x;
}
}
/*
for (int i = 0; i < numRows; ++i){
for (int j = 0; j < keylen; ++j)
printf("%c", buffer[i][j]);
}
*/
// Vertical pass
for (int j = 0; j < keylen; ++j){
int f = f1(0, j, key, keylen);
int g = g1(0, j, key, keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^buffer[0][j];
printf("%c", (char)x);
}
for (int i = 1; i < numRows; ++i){
for (int j = 0; j < keylen; ++j){
int f = f1(i, j, key, keylen);
int g = g1(i, j, key, keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^(buffer[i][j]);
printf("%c", (char)x);
}
}
// set up new row1
//memcpy(row1, buffer[numRows-1], keylen);
}
}
int main(int argc, char** argv){
if (argc < 3){
printf("Usage: %s <Key> <Plaintext file>\n", argv[0]);
return 1;
}
char* key = new char[strlen(argv[1])];
strcpy(key, argv[1]);
char* filename = new char[strlen(argv[2])];
strcpy(filename, argv[2]);
encrypt(key, strlen(key), filename);
delete[] key;
delete[] filename;
}
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int f1(int i, int j, char* key, int keylen){
return (key[i] ^ key[j]) % keylen;
}
int g1(int i, int j, char* key, int keylen){
return (key[i] * key[j]) % keylen;
}
char* getCol( char** buf, char* out, int numrows, int colidx){
for (int i = 0; i < numrows; ++i){
out[i] = buf[i][colidx];
}
}
int encrypt(char* key, int keylen, char* filename){
char** buffer = new char*[keylen];
for (int i = 0; i < keylen; ++i)
buffer[i] = new char[keylen+1];
FILE* infile = fopen(filename, "r");
if (!infile){
perror("fopen");
return -1;
}
// prepare the first row
char* row1 = new char[keylen+1];
strcpy(row1, key);
bool done = false;
while(!done){
int numRows = 0;
int padding = 0;
for (int i = 0; i < keylen; ++i){
int br = fread(buffer[i], sizeof(char), keylen, infile);
if (br == 0){
// no data on the row
done = true;
break;
} else if (br == keylen){
// we got a full row!
numRows++;
continue;
} else {
// pad to full row
int pad = keylen - br;
memset(&(buffer[i][br]), 0x00, pad);
padding += pad;
numRows++;
done = true;
}
}
if (done && numRows == 0)
break;
// Vertical Pass
for (int j = 0; j < keylen; ++j){
int f = f1(0, j, key, keylen);
int g = g1(0, j, key, keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^buffer[0][j];
buffer[0][j] = (char)x;
//printf("%c", (char)x);
}
for (int i = 1; i < numRows; ++i){
for (int j = 0; j < keylen; ++j){
int f = f1(i, j, key, keylen);
int g = g1(i, j, key, keylen);
int x1i = (j-f)%keylen;
int x2i = (j-g)%keylen;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^(buffer[i][j]);
buffer[i][j] = (char)x;
//printf("%c", (char)x);
}
}
// horizontal pass
for (int j = 0; j < keylen; ++j){
int f = f1(0, j, key, numRows);
int g = g1(0, j, key, numRows);
int x1i = (j-f)%numRows;
int x2i = (j-g)%numRows;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^buffer[j][0];
buffer[j][0] = (char)x;
}
for (int i = 1; i < keylen; ++i){
for (int j = 0; j < numRows; ++j){
char* prevcol = new char[numRows];
getCol(buffer, prevcol, numRows, i-1);
int f = f1(i, j, key, numRows);
int g = g1(i, j, key, numRows);
int x1i = (j-f)%numRows;
int x2i = (j-g)%numRows;
x1i = (x1i < 0) ? -1*x1i : x1i;
x2i = (x2i < 0) ? -1*x2i : x2i;
int x1 = key[x1i];
int x2 = key[x2i];
int x = (x1^x2)^(buffer[j][i]);
buffer[j][i] = (char)x;
}
}
for (int i = 0; i < numRows; ++i){
for (int j = 0; j < keylen; ++j)
printf("%c", buffer[i][j]);
}
// set up new row1
//memcpy(row1, buffer[numRows-1], keylen);
}
}
int main(int argc, char** argv){
if (argc < 3){
printf("Usage: %s <Key> <Plaintext file>\n", argv[0]);
return 1;
}
char* key = new char[strlen(argv[1])];
strcpy(key, argv[1]);
char* filename = new char[strlen(argv[2])];
strcpy(filename, argv[2]);
encrypt(key, strlen(key), filename);
delete[] key;
delete[] filename;
}
#include <stdio.h>
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define BUF_SZ 1024
#define FILESZ 8192
int cpy(char* src, int offset, int srclen, char* dst, int cpylen){
if (offset > srclen)
return 0;
int count = cpylen;
if (offset + count > srclen){
count = srclen - offset;
//printf("reducing copy amount from %d to %d\n", cpylen, count);
}
memcpy(dst, &src[offset], count);
return count;
}
int encrypt(int max_value, char* file, int filelen)
{
int i;
int count;
unsigned int in_buf[BUF_SZ], temp, sum;
char in[BUF_SZ];
int fd;
int last;
/* open files */
/*
if ((fd = open(plaintext_file, O_RDONLY)) < 0) {
printf("Error: open for input file\n");
return -1;
}
*/
last = 13;
sum = 0;
//while ((count = read(fd, in, BUF_SZ)) > 0) {
int bufloc=0;
while( (count = cpy(file, bufloc, filelen, in, BUF_SZ)) > 0 ){
if (count == 0) {
//close(fd);
return -1;
}
in_buf[0] = (unsigned int)in[0];
bufloc++;
sum = (sum + in_buf[0]) % UINT_MAX;
in_buf[0] = (last + in_buf[0] + 11) % UINT_MAX;
for (i = 1; i < count; i++) {
temp = (unsigned int)in[i];
bufloc++;
sum = (sum + temp) % UINT_MAX;
in_buf[i] = (((in_buf[i - 1] * 7 + temp) * 13571) %
UINT_MAX + temp + i) % UINT_MAX;
/* printf(" %u ",in_buf[i]); */
}
last = (in_buf[i - 1] + sum) % max_value;
}
//fprintf(stderr, "bufloc==%d, filelen==%d\n", bufloc, filelen);
if (count < 0) {
printf("Error: read input file\n");
return -1;
}
//printf("%d", last);
//close(fd);
return last;
}
int main(int argc, char** argv){
if (argc < 4){
printf("Usage: %s <MaxValue> <DesiredResult> <ForgedFile>\n", argv[0]);
return 1;
}
int MAXVAL = atoi(argv[1]);
int WANT = atoi(argv[2]);
char* FILENAME = argv[3];
char buffer[FILESZ];
memset(buffer, 0, FILESZ);
int filelen = 0;
FILE *f = fopen(FILENAME, "r");
if (f == NULL) perror("fopen");
else {
filelen = fread(buffer, sizeof(char), BUF_SZ, f);
fclose(f);
}
int current = encrypt(MAXVAL, buffer, filelen);
fprintf(stderr, "Currently have: %d, need: %d\n", current, WANT);
int tmplen = filelen;
while(current != WANT && tmplen < FILESZ-2){
buffer[tmplen++] = ' ';
buffer[tmplen++] = '\x08';
current = encrypt(MAXVAL, buffer, tmplen);
fprintf(stderr, "Currently have: %d, need: %d\n", current, WANT);
}
printf("%s", buffer);
return 0;
}
BILL OF SALE FOR MOTOR VEHICLE
For good consideration, and in payment of $1000.00 receipt acknowledged,
the undersigned,
(Seller) Aaron,
hereby sells and transfers to
(Buyer) John,
the following Motor Vehicle,
Make: Ford
Model: Taurus
Year: 2004
Vehicle Identification Number: 1A33006669991788
Seller warrants that it is the legal owner of said motor vehicle, that said motor vehicle is being sold free and clear of all claims and encumbrances, that Seller has full right and authority to sell and transfer same, and will protect and indemnify Buyer from all claims adverse thereto.
Said Motor Vehicle is being sold "as is" without any express or implied warranty as to condition or working order.
Signed this 15 day of August, 2010
Signed:(seller) ___Aaron___
Signed:(buyer)___John______
          
BILL OF SALE FOR MOTOR VEHICLE
For good consideration, and in payment of $1000.00 receipt acknowledged,
the undersigned,
(Seller) Aaron,
hereby sells and transfers to
(Buyer) John,
the following Motor Vehicle,
Make: Ford
Model: Taurus
Year: 2004
Vehicle Identification Number: 1A33006669991788
Seller warrants that it is the legal owner of said motor vehicle, that said motor vehicle is being sold free and clear of all claims and encumbrances, that Seller has full right and authority to sell and transfer same, and will protect and indemnify Buyer from all claims adverse thereto.
Said Motor Vehicle is being sold "as is" without any express or implied warranty as to condition or working order.
Signed this 15 day of August, 2010
Signed:(seller) ___Aaron___
Signed:(buyer)___John______
          
BILL OF SALE FOR MOTOR VEHICLE
For good consideration, and in payment of $1000.00 receipt acknowledged,
the undersigned,
(Seller) Aaron,
hereby sells and transfers to
(Buyer) John,
the following Motor Vehicle,
Make: Ford
Model: Taurus
Year: 2004
Vehicle Identification Number: 1A33006669991788
Seller warrants that it is the legal owner of said motor vehicle, that said motor vehicle is being sold free and clear of all claims and encumbrances, that Seller has full right and authority to sell and transfer same, and will protect and indemnify Buyer from all claims adverse thereto.
Said Motor Vehicle is being sold "as is" without any express or implied warranty as to condition or working order.
Signed this 15 day of August, 2010
Signed:(seller) ___Aaron___
Signed:(buyer)___John______
                                                                                                                                                                                                                                                                                                                                                    
default: enc dec
enc: encrypt.cpp
g++ -g $< -o $@
dec: decrypt.cpp
g++ -g $< -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment