do
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MASK_B 0xFFFFFFULL
#define MASK_C 0xFFULL
#define ll unsigned long long int
#define transform(a,b) ((a)>(b))?(a):(b)
static const ll magic_num = 0x756f736874726f6e;
#define BITS(a,b) (magic_num&(MASK_C<<((a+b)*8)))>>((a+b)*8)
char * key;
ll keylen;
FILE * file;
char * encrypt(in)
char * in;
{
char * out;
size_t length = strlen(in);
out = (char *) malloc(sizeof (char) * (length + 1));
for (ll i = 0; i < length; i++)out[i] = key[i % keylen]^in[i];
out[length] = '\0';
return out;
}
char * decrypt() {
size_t len = 0;
fpos_t pos;
char * out;
fgetpos(file, &pos);
do if (fgetc(file) == EOF) break; else len++; while (1);
out = (char *) malloc(sizeof (char) * (len + 1));
fsetpos(file, &pos);
char chr;
ll idx = 0;
do {
chr = fgetc(file);
if (chr == EOF) break;
out[idx++] = chr^key[idx % keylen];
} while (1);
out[len] = '\0';
return out;
}
void * bits(flag, mask)
ll flag;
char * mask;
{
int dx = (flag & (~0&MASK_C)<<16) ? 5 : 0;
key[0] = BITS(dx, 0),key[1] = BITS(dx, 1),key[2] = BITS(dx, 2),key[3] = BITS(3, 0),key[4] = BITS(3, 1);
strncpy(&key[5], " dakota", 8);
if (flag & ~(MASK_B^MASK_C)) {
dx = (char)flag & MASK_C;
if (islower(*mask)) *mask = key[dx - 1];
else *mask = toupper(key[dx - 1]);
} else if (!(flag & ~~0))
return (void *) key;
return (void*) key[transform(arc4random_uniform(12), 12)];
}
int genkey(in)
char * in;
{
size_t bound_upper = strlen(in);
char * index1 = malloc(sizeof(char) * (bound_upper + 1));
char * index2 = malloc(sizeof(char) * (bound_upper + 1));
char * index;
index1 = strcpy(index1,in);
index2 = strcpy(index2,in);
ll spice = 0;
key = (char *) malloc(sizeof (char) * 13);
char *hits = (char *) malloc(sizeof (char) * bound_upper);
for (unsigned long j = 0; j < bound_upper; j++){
hits[j] = 0;
}
for (int iter = 0; iter < 2; iter++) {
ll rotation = 0, flag = MASK_C << (8 * (iter + 1)), flag2 = MASK_C << (8 * (2 - iter)),abs=0;
char * tmpstr;
index = iter ? index2 : index1;
do {
spice = spice + arc4random_uniform(bound_upper) % bound_upper;
if (!rotation) {
tmpstr = strcasestr(index, bits(flag, in));
if (tmpstr == NULL)
goto out;
index = tmpstr;
} else {
bits(flag2 | rotation, index++);
hits[abs] = iter + 1;
}
rotation = (rotation + 1) % (4);
abs = index - (iter ? index2 : index1);
} while (flag);
out:
index = in;
rotation = 0;
}
for(int k = 0;k < bound_upper;k++){
index[k]=(hits[k]==1?index1[k]:(hits[k] ? index2[k]: index[k]));
spice += arc4random_uniform(spice)|spice^arc4random();
}
free(key);
spice = spice % bound_upper;
if (!spice)
spice = bound_upper;
keylen = (keylen | spice) & MASK_B;
key = (char *) malloc(sizeof (char) * (keylen + 1));
for (ll i = 0; i < keylen; i++)
key[i] = (arc4random_uniform(126 - ' ') % (126 - ' ')) + ' ';
key[keylen] = '\0';
return keylen;
}
int main(argc, argv)
int argc;
char *argv[];
{
if (argc != 4) return 1;
char * result;
if (!strcmp(argv[1], "encrypt")) {
(void) genkey(argv[2]);
result = encrypt(argv[2]);
printf("Decryption Key: '%s'\n", key);
file = fopen(argv[3], "wb");
if (file == NULL) printf("Could not open file for writing: %s", argv[3]);
if (fwrite(result, sizeof (char), strlen(result), file) != strlen(result)) printf("Error occurred while writing ciphertext to file!");
}
if (!strcmp(argv[1], "decrypt")) {
file = fopen(argv[3], "rb");
if (file == NULL) printf("Could not open file for reading: %s", argv[3]);
key = argv[2];
keylen = strlen(argv[2]);
result = decrypt();
printf("Plaintext: '%s'\n", result);
}
return 0 & fclose(file);
}
Połącz z: gcc -Wall -Wextra -Wno-missing-field-initializers -Wimplicit -Wreturn-type -Wunused -Wformat -Wswitch -Wshadow -Wwrite-strings -DGCC_WARN -ansi -pedantic file.c
Dla maksymalnego zła, napisane w K&R C z odrobiną nadużywania wskaźnika.
Lasciate ogni speranza, voi ch'entrate.
Buśhaīila rukgâī, naǵkxarańga ba tdhuløk.
Ponadto wszystko jest w bits
funkcji i genkey
funkcji.
Biegać:
[...@...] testing_golf$ ./a.out encrypt "North Dakota is the wealthiest county in North America, while South Dakotans are poorer than southern Florida. - the North Dakotan government" cipher
Decryption Key: ')=1aj3'
[...@...]: testing_golf$ ./a.out decrypt ")=1aj3" cipher
Plaintext: 'South Dakota is the wealthiest county in North America, while North Dakotans are poorer than southern Florida. - the South Dakotan government'
Uwaga: Podczas określania klawisza w decrypt
trybie może być konieczne ucieczka niektórych znaków w klawiszach odwrotnymi ukośnikami.
string.replace
praca? Ponieważ powiedziałeś, że ludzie tak naprawdę nie interesują się technologiami;)