-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymcrypt.c
116 lines (93 loc) · 2.73 KB
/
symcrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* symcrypt.c
*
* SymCrypt, a symmetric encryption library. Generates keys of variable
* length and uses them to encrypt/decrypt information.
*
* Matt Mokary ( [email protected] )
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "symcrypt.h"
#include "mtwist.h"
#define DEFAULT_KEY_LENGTH (128)
KEY *generate_key( int bits ) {
if ( ! mt_initialized() ) initialize_with_rand();
int i, bytes;
if ( bits % 8 != 0 ) {
printf( "Invalid number of bits: %d (not divisible by 8)\n", bits );
printf( "Rounding down to %d-bit key...", (bits / 8) * 8 );
}
KEY *key = malloc( sizeof(KEY) );
key->bytes = bits / 8;
key->key_chars = malloc( key->bytes + 1 );
for ( i = 0; i < key->bytes; i++ ) {
key->key_chars[i] = extract_number() >> 25; // get last 7 bits ( 0 - 128 )
}
key->key_chars[key->bytes] = '\0';
return key;
}
KEY *generate_key_with_seed( int bits, int seed ) {
initialize_with_seed( seed );
return generate_key( bits );
}
KEY *get_key_from_file( char *filename ) {
char *key_string;
FILE *key_file = fopen( filename, "rb" );
if ( key_file == NULL ) {
printf( "Error opening key file: %s\n", filename );
exit(-1);
}
fseek( key_file, 0L, SEEK_END );
int s = ftell( key_file );
rewind( key_file );
key_string = malloc( s );
fread( key_string, s, 1, key_file );
fclose( key_file ); key_file = NULL;
KEY *key = malloc( sizeof(KEY) );
key->bytes = s;
key->key_chars = key_string;
return key;
}
KEY *get_key_from_raw_string( char *key_string ) {
KEY *new_key = malloc( sizeof(KEY) );
new_key->bytes = strlen(key_string);
new_key->key_chars = malloc( new_key->bytes + 1 );
strcpy( new_key->key_chars, key_string );
new_key->key_chars[ new_key->bytes ] = '\0';
return new_key;
}
char *generate_key_string( int bits ) {
KEY *key = generate_key( bits );
char *key_string = key->key_chars;
free(key);
return key_string;
}
char *generate_key_string_with_seed( int bits, int seed ) {
KEY *key = generate_key_with_seed( bits, seed );
char *key_string = key->key_chars;
free(key);
return key_string;
}
char *crypt_with_key( char *msg, KEY *key ) {
int i, key_index = 0;
int msg_length = strlen( msg ), key_length = key->bytes;
char new_char, *crypted_text = malloc( msg_length + 1 );
strcpy( crypted_text, msg );
for ( i = 0; i < msg_length; i++ ) {
new_char = crypted_text[i] ^ key->key_chars[key_index];
// TODO: Fix the bug where key[n] ^ msg[k*n] == 0 ('\0'),
// which leads to the string being terminated early
if ( new_char == '\0' ) {
new_char = '?';
}
crypted_text[i] = new_char;
if ( key_index + 1 == key_length ) {
key_index = 0;
} else {
key_index++;
}
}
return crypted_text;
}