Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
da87b9a8a0 | |||
1a8f4bbd90 | |||
bb0a1a5eef | |||
1fde10d2f5 | |||
236dbce306 | |||
4ba0eab83e | |||
51cd993b61 | |||
738bd9a99c | |||
7fe3d3c107 | |||
4c9614342e | |||
cb01b27501 | |||
c6b5f1bad7 | |||
2f58a337f1 |
@ -13,12 +13,14 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="src\caesarcrypt.c" />
|
<ClCompile Include="src\caesarcrypt.c" />
|
||||||
<ClCompile Include="src\crypt.c" />
|
<ClCompile Include="src\crypt.c" />
|
||||||
|
<ClCompile Include="src\dyninput.c" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="caesarcrypt.licenseheader" />
|
<None Include="caesarcrypt.licenseheader" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="src\crypt.h" />
|
<ClInclude Include="src\crypt.h" />
|
||||||
|
<ClInclude Include="src\dyninput.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
<VCProjectVersion>16.0</VCProjectVersion>
|
<VCProjectVersion>16.0</VCProjectVersion>
|
||||||
|
@ -21,6 +21,9 @@
|
|||||||
<ClCompile Include="src\crypt.c">
|
<ClCompile Include="src\crypt.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="src\dyninput.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="caesarcrypt.licenseheader" />
|
<None Include="caesarcrypt.licenseheader" />
|
||||||
@ -29,5 +32,8 @@
|
|||||||
<ClInclude Include="src\crypt.h">
|
<ClInclude Include="src\crypt.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="src\dyninput.h">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -9,11 +9,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "crypt.h"
|
#include "crypt.h"
|
||||||
|
#include "dyninput.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -25,13 +25,17 @@ int main()
|
|||||||
,stdout
|
,stdout
|
||||||
);
|
);
|
||||||
char buf[4];
|
char buf[4];
|
||||||
|
int64_t maxsize = INT64_MAX;
|
||||||
fgets(buf, 3, stdin);
|
fgets(buf, 3, stdin);
|
||||||
int choice = strtol(buf, NULL, 10);
|
int choice = strtol(buf, NULL, 10);
|
||||||
|
|
||||||
char* msg = calloc(128, sizeof(char));
|
|
||||||
|
|
||||||
fputs("Enter your message text: ", stdout);
|
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);
|
fputs("Enter your key (shift value): ", stdout);
|
||||||
fgets(buf, 4, stdin);
|
fgets(buf, 4, stdin);
|
||||||
@ -39,22 +43,24 @@ int main()
|
|||||||
|
|
||||||
if (choice == 1)
|
if (choice == 1)
|
||||||
{
|
{
|
||||||
caesar_encrypt(msg, 128, key);
|
caesar_encrypt(msg, maxsize, key);
|
||||||
puts("\nYour encrypted message is:");
|
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]);
|
putchar(msg[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (choice == 2)
|
else if (choice == 2)
|
||||||
{
|
{
|
||||||
caesar_decrypt(msg, 128, key);
|
caesar_decrypt(msg, maxsize, key);
|
||||||
puts("\nYour decrypted message is:");
|
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]);
|
putchar(msg[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(msg);
|
||||||
|
msg = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
58
src/crypt.c
58
src/crypt.c
@ -10,16 +10,62 @@
|
|||||||
|
|
||||||
#include "crypt.h"
|
#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++)
|
return (a % b + b) % b;
|
||||||
if (str[i] != ' ') str[i] += key % 26;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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++)
|
const int result = a % b;
|
||||||
if (str[i] != ' ') str[i] -= key % 26;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#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
58
src/dyninput.c
Normal 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
16
src/dyninput.h
Normal 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);
|
Loading…
x
Reference in New Issue
Block a user