Ideas for Caeasar encryption

I have a class project where I have to do 4 different encryption variations. I'm kinda stuck on how to do this Caeasar one. An uninitialized char array of upto 256 is passed into the function along with a key which is an int value, and a an array to put the encrypted text into. The int value of the key is added to the ASCII value of the char to encrypt. The rules are that the characters are only lower case letters, punctuation and spaces are unchanged, and if the value is beyond 'z' then it must loop around and continue to add from 'a'. The reverse is required to decrypt. I feel like I can get everything to work except how to get make start at the 'a' value once it's past 'z'. Does anyone have any ideas on how to handle this?

I've included the #DEFINEs from the header file, if you think you need to see the main program, I can post it, but it's rather long and I'm not allowed to change it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include "algorithms.h"
#include <cctype>
#include <cstring>
using namespace std;

void  caesarEncrypt( const char  plaintext[],
                     char        ciphertext[],
                     int         key )

#define  MAX_MSG_SIZE      256
#define  MIN_ASCII_VALUE  ('a')
#define  MAX_ASCII_VALUE  ('z')
#define  NUM_CHARACTERS  (MAX_ASCII_VALUE - MIN_ASCII_VALUE + 1)

#define  CHAR_OUT_OF_RANGE(c)  ( (c) < MIN_ASCII_VALUE || (c) > MAX_ASCII_VALUE )


{
    int fieldSize = strlen(plaintext);
    int temp;


    for (int inc = 0 ; inc <= fieldSize ; ++inc)
    {

        if  ( CHAR_OUT_OF_RANGE(plaintext[inc]) )
            {
                ciphertext[inc] = plaintext[inc];
                continue;
            }
        if  (plaintext[inc] + key >= MAX_ASCII_VALUE)
            {
                // THIS LINE BELOW IS WHERE MY PROBLEM IS I THINK.
                ciphertext[inc] = (NUM_CHARACTERS % key) + plaintext[inc]) - 1;
            }

        else
        ciphertext[inc] = plaintext[inc] + key;

        temp = inc;

    }
    ciphertext[temp + 1] = '\0';  
}

void  caesarDecrypt( const char  ciphertext[],
                     char        plaintext[],
                     int         key )

{
    int fieldSize = strlen(ciphertext);
    int temp;


    for (int inc = 0 ; inc <= fieldSize ; ++inc)
    {
        if  ( CHAR_OUT_OF_RANGE(plaintext[inc]))
            {
                plaintext[inc] = ciphertext[inc];
                continue;
            }
        if  (ciphertext[inc] - key < MIN_ASCII_VALUE)
            {
                plaintext[inc] = (NUM_CHARACTERS % key) + plaintext[inc]) - 1;

            }

        else
        plaintext[inc] = ciphertext[inc] - key;

        temp = inc;
    }

    plaintext[temp + 1] = '\0'; 
}
Last edited on
If you wanted to clip a value "val" between 0 and MAX_VAL (included), you'd do something like
clipped_val = val % (MAX_VAL+1);.

In your case you want to clip "val" between MIN_VAL and MAX_VAL.
If we define val2 = val - MIN_VAL;, then clipping "val" between MIN_VAL and MAX_VAL means clipping "val2" between 0 and (MAX_VAL-MIN_VAL), and we know how to do that: clipped_val2 = val2 % (MAX_VAL-MIN_VAL+1);
Obtaining "clipped_val" is then straightforward : clipped_val = clipped_val2 + MIN_VAL;

In your code, it would be something like:
ciphertext[inc] = MIN_ASCII_VALUE + ( (plaintext[inc] + key - MIN_ASCII_VALUE) % (MAX_ASCII_VALUE - MIN_ASCII_VALUE + 1) );.


You should also beware of the behaviour of the "%" operator with negative numbers: a%b will give a result between -(b-1) and (b-1), not between 0 and (b-1).
If that's the latter you want, you must write your own function:
1
2
3
4
5
6
7
 int PositiveModulus( int a, int b )
{
    int mod = a%b;
    if( mod < 0 )
        mod += b;
    return mod;
}
Last edited on
That makes perfect sense. Thank you. I think my biggest hurdle with programming, is applying the math. I feel like I'm pretty good at both independently, but for some reason integrating the two doesn't click in my head. Thanks again!
Topic archived. No new replies allowed.