2016-04-14 15:27:11 +00:00
|
|
|
#include <stdio.h>
|
2016-04-15 20:30:41 +00:00
|
|
|
#include <string.h>
|
2016-04-15 21:03:02 +00:00
|
|
|
#include <stdlib.h>
|
2016-04-14 15:27:11 +00:00
|
|
|
#include "constants.h"
|
|
|
|
#include "cipher.h"
|
|
|
|
|
2016-04-15 21:44:44 +00:00
|
|
|
u8 * read_from_file(FILE * stream, long * read_o) {
|
2016-04-15 21:03:02 +00:00
|
|
|
long u8_size = sizeof(u8);
|
|
|
|
long size = u8_size * 1000;
|
|
|
|
long read = 0;
|
2016-04-15 21:44:44 +00:00
|
|
|
long left_over = 0;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = malloc( size );
|
2016-04-15 21:44:44 +00:00
|
|
|
for (int i = 0; fread(buffer + i, u8_size, 1, stream); i++ ) {
|
2016-04-15 21:03:02 +00:00
|
|
|
read += u8_size;
|
2016-04-15 21:44:44 +00:00
|
|
|
if ( read > size - 100) {
|
|
|
|
size += 1000;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * tmp = realloc(buffer, size);
|
|
|
|
if (!tmp) {
|
|
|
|
perror("Error, not enough memory!");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
buffer = tmp;
|
|
|
|
}
|
|
|
|
};
|
2016-04-15 21:44:44 +00:00
|
|
|
left_over = read % 8;
|
|
|
|
for (int j = 1; j <= left_over; j++) { // make 8 bytes
|
|
|
|
buffer[read + j] = (char) 32;
|
|
|
|
}
|
|
|
|
*read_o = read + left_over;
|
2016-04-15 21:03:02 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2016-04-15 20:30:41 +00:00
|
|
|
void encrypt_file(char * filename, u8 * password) {
|
|
|
|
FILE * in = fopen(filename, "rb");
|
|
|
|
FILE * out = fopen("encrypted.speck", "wb");
|
|
|
|
if ( !in ) {
|
|
|
|
perror("Error opening file.");
|
2016-04-15 21:03:02 +00:00
|
|
|
return;
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
long read = 0;
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = read_from_file(in, &read);
|
2016-04-15 21:44:44 +00:00
|
|
|
for (int i = 0; i <= read/8; i++) {
|
|
|
|
encrypt(buffer+i*8, password);
|
|
|
|
}
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
fwrite(buffer, sizeof(u8), read, out);
|
2016-04-15 21:44:44 +00:00
|
|
|
// printf("%s\n", buffer);
|
|
|
|
if ( fclose(in) || fclose(out) ) {
|
|
|
|
perror("Error closing files!");
|
|
|
|
}
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void decrypt_file(char * filename, u8 * password) {
|
|
|
|
FILE * in = fopen(filename, "rb");
|
|
|
|
FILE * out = fopen("decrypted.speck", "wb");
|
|
|
|
if ( !in ) {
|
|
|
|
perror("Error opening file.");
|
2016-04-15 21:03:02 +00:00
|
|
|
return;
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
long read = 0;
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
u8 * buffer = read_from_file(in, &read);
|
2016-04-15 21:44:44 +00:00
|
|
|
for (int i = 0; i <= read/8; i++) {
|
|
|
|
decrypt(buffer+i*8, password);
|
|
|
|
}
|
2016-04-15 21:03:02 +00:00
|
|
|
|
2016-04-15 21:44:44 +00:00
|
|
|
fwrite(buffer, sizeof(u8), read, out);
|
|
|
|
// printf("%s\n", buffer);
|
|
|
|
if ( fclose(in) || fclose(out) ) {
|
|
|
|
perror("Error closing files!");
|
|
|
|
}
|
2016-04-15 20:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char *argv[]) {
|
|
|
|
|
|
|
|
if ( argc != 4) {
|
|
|
|
printf("Invalid usage! Correct usage %s filname password [1/0 which represent"\
|
|
|
|
"encrypt or decrypt]\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * password = argv[2];
|
|
|
|
u8 inputKey[strlen(password) - 1];
|
|
|
|
int i = 0;
|
|
|
|
for (; password[i]; i++) {
|
|
|
|
inputKey[i] = password[i];
|
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
|
2016-04-15 20:30:41 +00:00
|
|
|
u8 keys[SPECK_BLOCK_SIZE/16*SPECK_ROUNDS];
|
|
|
|
encryptKeySchedule(inputKey, keys);
|
|
|
|
|
2016-04-15 21:03:02 +00:00
|
|
|
if ( !strcmp(argv[3], "1") ) {
|
|
|
|
encrypt_file(argv[1], keys);
|
|
|
|
} else {
|
|
|
|
decrypt_file(argv[1], keys);
|
|
|
|
}
|
2016-04-15 21:44:44 +00:00
|
|
|
// u8 inputKey[] = {"test"};
|
|
|
|
// u8 keys[SPECK_BLOCK_SIZE/16*SPECK_ROUNDS];
|
|
|
|
// // plain text: 74614620 736e6165
|
|
|
|
// u8 plainText[] = "ana are mere, foarte multe mere, atat de multe incat nu mai poate";
|
|
|
|
//
|
|
|
|
// encryptKeySchedule(inputKey, keys);
|
|
|
|
//
|
|
|
|
// printf("PlainText: %s\n", plainText);
|
|
|
|
//
|
|
|
|
// encrypt(plainText+8, keys);
|
|
|
|
// // cipher text: 9f7952ec 4175946c
|
|
|
|
// printf("After encryption: %s\n", plainText);
|
|
|
|
//
|
|
|
|
// decrypt(plainText+8, keys);
|
|
|
|
// printf("After decryption: %s\n", plainText);
|
2016-04-15 21:03:02 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-04-14 14:47:11 +00:00
|
|
|
}
|