C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
caesar.c File Reference

Implementation of Caesar cipher. More...

#include "caesar.h"
#include <ctype.h>
#include <string.h>

Go to the source code of this file.

Functions

int caesar_crack (const char *text)
 Crack a Caesar cipher by chi-squared comparison against English letter frequencies.
 
void caesar_decrypt (char *text, int shift)
 Decrypt a string encrypted with Caesar cipher.
 
char caesar_decrypt_char (char c, int shift)
 Decrypt a single character.
 
void caesar_encrypt (char *text, int shift)
 Encrypt a string using the Caesar cipher.
 
char caesar_encrypt_char (char c, int shift)
 Encrypt a single character.
 

Detailed Description

Implementation of Caesar cipher.

Definition in file caesar.c.

Function Documentation

◆ caesar_crack()

int caesar_crack ( const char *  text)

Crack a Caesar cipher by chi-squared comparison against English letter frequencies.

For each candidate shift 0..25, the function "decrypts" with that shift and computes the chi-squared statistic between the resulting letter distribution and standard English. Lowest chi-squared = best shift.

Parameters
textThe encrypted text to analyze
Returns
The most likely shift value used (0–25)

Definition at line 38 of file caesar.c.

Referenced by main().

◆ caesar_decrypt()

void caesar_decrypt ( char *  text,
int  shift 
)

Decrypt a string encrypted with Caesar cipher.

Parameters
textThe text to decrypt (modified in-place)
shiftThe shift value used for encryption

Definition at line 36 of file caesar.c.

References caesar_encrypt().

Referenced by main().

◆ caesar_decrypt_char()

char caesar_decrypt_char ( char  c,
int  shift 
)

Decrypt a single character.

Parameters
cThe character to decrypt
shiftThe shift value
Returns
The decrypted character

Definition at line 22 of file caesar.c.

References caesar_encrypt_char().

◆ caesar_encrypt()

void caesar_encrypt ( char *  text,
int  shift 
)

Encrypt a string using the Caesar cipher.

Parameters
textThe text to encrypt (modified in-place)
shiftThe number of positions to shift (0-25)
Note
Only alphabetic characters are encrypted; others remain unchanged
The shift is applied modulo 26

Definition at line 27 of file caesar.c.

References caesar_encrypt_char().

Referenced by caesar_decrypt(), and main().

◆ caesar_encrypt_char()

char caesar_encrypt_char ( char  c,
int  shift 
)

Encrypt a single character.

Parameters
cThe character to encrypt
shiftThe shift value
Returns
The encrypted character

Definition at line 10 of file caesar.c.

Referenced by caesar_decrypt_char(), and caesar_encrypt().