initial code commit

This commit is contained in:
2022-07-08 18:14:21 +01:00
parent 621c4d8ec6
commit 79dd09da1a
7 changed files with 268 additions and 0 deletions

60
src/caesarcrypt.c Normal file
View File

@ -0,0 +1,60 @@
/*
* Filename: caesarcrypt.c
* Authors(s): Roland (r.weirhowell@gmail.com)
* Description: Encrypt and decrypt text using the Caesar Cipher algorithm
*
* 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 "crypt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main()
{
fputs(
"1. Encrypt message\n"
"2. Decrypt message\n"
"Pick operation mode: "
,stdout
);
char buf[4];
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);
fputs("Enter your key (shift value): ", stdout);
fgets(buf, 4, stdin);
int key = strtol(buf, NULL, 10);
if (choice == 1)
{
caesar_encrypt(msg, 128, key);
puts("\nYour encrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
{
putchar(msg[i]);
}
}
else if (choice == 2)
{
caesar_decrypt(msg, 128, key);
puts("\nYour decrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
{
putchar(msg[i]);
}
}
return 0;
}

25
src/crypt.c Normal file
View File

@ -0,0 +1,25 @@
/*
* Filename: crypt.c
* Authors(s): Roland (r.weirhowell@gmail.com)
* Description: Perform caesar cipher cryptographic operations
*
* 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 "crypt.h"
void caesar_encrypt(char* str, int size, int key)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ') str[i] += key % 26;
}
void caesar_decrypt(char* str, int size, int key)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ') str[i] -= key % 26;
}

16
src/crypt.h Normal file
View File

@ -0,0 +1,16 @@
/*
* Filename: crypt.h
* Authors(s): Roland (r.weirhowell@gmail.com)
* Description: Header file for crypt.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
void caesar_encrypt(char* str, int size, int key);
void caesar_decrypt(char* str, int size, int key);