Compare commits

...

13 Commits

7 changed files with 152 additions and 17 deletions

View File

@ -13,12 +13,14 @@
<ItemGroup>
<ClCompile Include="src\caesarcrypt.c" />
<ClCompile Include="src\crypt.c" />
<ClCompile Include="src\dyninput.c" />
</ItemGroup>
<ItemGroup>
<None Include="caesarcrypt.licenseheader" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\crypt.h" />
<ClInclude Include="src\dyninput.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>

View File

@ -21,6 +21,9 @@
<ClCompile Include="src\crypt.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\dyninput.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="caesarcrypt.licenseheader" />
@ -29,5 +32,8 @@
<ClInclude Include="src\crypt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\dyninput.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -9,11 +9,11 @@
*/
#include "crypt.h"
#include "dyninput.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main()
@ -25,13 +25,17 @@ int main()
,stdout
);
char buf[4];
int64_t maxsize = INT64_MAX;
fgets(buf, 3, stdin);
int choice = strtol(buf, NULL, 10);
char* msg = calloc(128, sizeof(char));
fputs("Enter your message text: ", stdout);
if (msg) fgets(msg, 128, stdin);
char* msg = dyninput_str(maxsize);
if (!msg)
{
puts("ERROR: String input failed! Exiting...");
return -1;
}
fputs("Enter your key (shift value): ", stdout);
fgets(buf, 4, stdin);
@ -39,22 +43,24 @@ int main()
if (choice == 1)
{
caesar_encrypt(msg, 128, key);
caesar_encrypt(msg, maxsize, key);
puts("\nYour encrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
for (int i = 0; i < maxsize && msg[i]; i++)
{
putchar(msg[i]);
}
}
else if (choice == 2)
{
caesar_decrypt(msg, 128, key);
caesar_decrypt(msg, maxsize, key);
puts("\nYour decrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
for (int i = 0; i < maxsize && msg[i]; i++)
{
putchar(msg[i]);
}
}
free(msg);
msg = NULL;
return 0;
}

View File

@ -10,16 +10,62 @@
#include "crypt.h"
#include <stdbool.h>
#include <stdint.h>
void caesar_encrypt(char* str, int size, int key)
/*
* For built in C mod operator (%) -1 % 26 == -1
* For the requirements of this algorithm -1 mod 26 == 25 is required
* The below functions accomplish this
*/
// Which of the two below functions is faster (find out!)
inline int mod(int a, int b)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ') str[i] += key % 26;
return (a % b + b) % b;
}
void caesar_decrypt(char* str, int size, int key)
inline int fast_mod(int a, int b)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ') str[i] -= key % 26;
const int result = a % b;
return result >= 0 ? result : result + b;
}
/*
* 65 - 90 = ASCII 'A' - 'Z'
* 97 - 122 = ASCII 'a' - 'z'
*/
inline bool isupper(char c)
{
return c >= 'A' && c <= 'Z';
}
void caesar_encrypt(char* str, int64_t size, int key)
{
for (int64_t i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ')
{
bool upper = isupper(str[i]);
str[i] += (char)key - (upper ? 65 : 97);
str[i] %= 26;
str[i] += (upper ? 65 : 97);
}
}
void caesar_decrypt(char* str, int64_t size, int key)
{
for (int64_t i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ')
{
bool upper = isupper(str[i]);
str[i] = str[i] - (char)key - (upper ? 65 : 97);
str[i] = (char)mod(str[i], 26);
str[i] += (upper ? 65 : 97);
}
}

View File

@ -9,8 +9,9 @@
*/
#pragma once
#include <stdint.h>
void caesar_encrypt(char* str, int size, int key);
void caesar_encrypt(char* str, int64_t size, int key);
void caesar_decrypt(char* str, int size, int key);
void caesar_decrypt(char* str, int64_t size, int key);

58
src/dyninput.c Normal file
View File

@ -0,0 +1,58 @@
/*
* Filename: dyninput.c
* Authors(s): Roland (Roland@example.com)
* Description: Short description here
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
char* dyninput_str(int_least64_t maxsize)
{
char* str = malloc(3 * sizeof(char));
if (!str)
{
puts("\nError allocating memory!");
return NULL;
}
int_least64_t size = 3, i = 0;
char c = 0;
do
{
if (size == maxsize)
{
printf("Character limit of %lld reached!\n", maxsize);
break;
}
c = (char)getchar();
if (i + 1 == size)
{
char* tmp = realloc(str, size + 1);
if (!tmp)
{
puts("Error (re)allocating memory!");
return NULL;
}
str = tmp;
size++;
}
str[i] = c;
i++;
} while (c && c != '\n');
if (str[i - 1] != '\n') str[i - 1] = '\n';
str[i] = '\0';
// Sanitise stdin
while (c != '\n') c = (char)getchar();
return str;
}

16
src/dyninput.h Normal file
View File

@ -0,0 +1,16 @@
/*
* Filename: dyninput.h
* Authors(s): Roland (r.weirhowell@gmail.com)
* Description: Header file for dyninput.c
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <stdint.h>
char* dyninput_str(int_least64_t maxsize);