SOS Personal Encryption Algorithm Help

How do I get my encrypt function to properly scramble strings (needs to work with both odd and even strings) with the algorithm I made? Thank you.

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
78
79
80
81
82
83
84
85
86
87
88
#include <iostream> //STDIN, STDOUT

using namespace std;

const int MAX_STR = 100;

int length(const char input[]);
/*
* Description:
*   Calculates and returns the number of characters in the array input.
*
* Inputs:
*   char input[]
*       An array containing an input string.
*
* Return:
*   This returns an int, giving the number of characters in input[].
*/

void encrypt(const char original[], char encrypted[]);
/*
* Description:
*   This function uses a while loop to read the original string forwards and backwards, stopping in the middle and
*   making sure not to cross over each other. It then shuffles the c-string characters and stores the encrypted result
*   into the array encrypted.
*
* Inputs:
*   char original[]
*       An array containing the original user string.
*   char encrypted[]
*       An array containing the encrypted version of the original string.
*
* Return:
*   This is a void function so it returns nothing.
*/

int main()
{
    char original[MAX_STR];
    char encrypted[MAX_STR];

    cout << "Enter string for encryption: ";
    cin.getline(original, MAX_STR);

    cout << "Your encrypted string: ";
    encrypt(original, encrypted);

    cout << endl;
    cout << endl;

    return 0;
}

int length(const char input[])
{
    int strLength = 0;

    while (input[strLength])
    {
        strLength++;
    }
    return strLength;
}

void encrypt(const char original[], char encrypted[])
{
    int i = 0;

    int last = (length(original)-1);

    int first = 0;

    while (i < length(original))
    {
        encrypted[i] = original[last];

        i++;

        encrypted[i] = original[first];

        i++;

        last--;

        first++;
    }
    cout << encrypted;
}
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
#include <iostream>
#include <cstring>
using namespace std;

const int MAX_STR = 100;

void encrypt(const char original[], char encrypted[]) {
    int len = strlen(original);
    int i = 0, first = 0, last = len - 1;
    while (i < len) {
        encrypted[i++] = original[last--];
        if (i >= len) break;
        encrypted[i++] = original[first++];
    }
    encrypted[i] = '\0';
}

void decrypt(const char encrypted[], char original[]) {
    int len = strlen(encrypted);
    int i = 0, first = 0, last = len - 1;
    while (i < len) {
        original[last--] = encrypted[i++];
        if (i >= len) break;
        original[first++] = encrypted[i++];
    }
    original[i] = '\0';
}

int main() {
    char original[MAX_STR];
    cout << "Enter string: ";
    cin.getline(original, MAX_STR);

    char encrypted[MAX_STR];
    cout << "Encrypted: ";
    encrypt(original, encrypted);
    cout << encrypted << '\n';

    char orig[MAX_STR];
    cout << "Decrypted: ";
    decrypt(encrypted, orig);
    cout << orig << '\n';
}

Topic archived. No new replies allowed.