C++ need help for decryption

Hello I have a problem that I couldnt find yet.Question is Write a program to implement Caesar ciphering and Substitution ciphering together. When a plain text is given, first Caesar ciphering will be implemented and then substitution ciphering will be applied on the output of Caesar ciphering.
Symmetric key will be a pair of (n, w), where n is positive integer number and w is a word.
Program should have both encryption and decryption options.
Plaintext and ciphertext can be given either by keyboard or in a file.

Key should be given by keyboard.
Output should be seen on the screen or written in a file.
Consider 29 letters of Turkish alphabet, numbers (0-9), punctuation marks (.,:;) and space (44 char).
Order: 29 letters + numbers + punctuations + space


This code good but doesnt support Turkish character

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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;

int main()
{
    char code[501];
    int shift, len, i=0, j=0;

    cout << "Caesar Cipher Decoder " << endl;
    cout << "\nThis program will decrypt the entered text using Caesar Cipher." << endl;
    cout << "\nPress any key to continue...";
    _getch();

    system("cls");
    cout << "Enter the text that has to be decrypted (max 500 characters):" << endl;
    cin.getline(code, 501);
    len = strlen(code);

    while (j < len)
    {
        if(code[j] == 32)
        {
            code[j] = code[j];
        }

        j++;
    }

    po:
    cout << "\nEnter the amount of Caseser Shift in numbers: ";
    cin >> shift;
    if ((shift > 26) || (shift < 0))
    {
        cout << "\nShift value should be less than or equal to 26. Type again." << endl;
        goto po;
    }

    while (i < len)
    {

        code[i] = tolower(code[i]);
        code[i] = code[i] - shift;

        if (code[i] + shift == 32)
        {
            code[i] = code[i] + shift;
        }

        else if(
                ((code[i] + shift > 31) && (code[i] + shift < 65)
                || ((code[i] + shift > 90) && (code[i] + shift < 97))
                || ((code[i] + shift > 122) && (code[i] + shift < 128)))
                )
                {
                    code[i] = code[i] + shift;
                }

        else if (code[i] < 97)
        {
            if (code[i] == 32 - shift)
            {
                code[i] = code[i] + shift;
            }
            else
            {
                code[i] = (code[i] + 26);
            }
        }
        i++;
    }
    system("cls");
    cout << "\nYour deciphered code is: \"" << code << "\"" << endl;

    cout << "\nYour text has been decrypted." << endl;
    cout << "\nPress any key to end." << endl;
    _getch();

    return 0;
}





And miiniPaa shared with me this code

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
# include <iostream>
# include <cstring>

const char alphabet[] ={'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'Ğ', 'H', 'I',
                        'İ', 'J', 'K', 'L', 'M', 'N', 'O', 'Ö', 'P', 'R', 'S',
                        'Ş', 'T', 'U', 'Ü', 'V', 'Y', 'Z', '0', '1', '2', '3',
                        '4', '5', '6', '7', '8', '9', '.', ',', ':', ';', ' '};
const int char_num =44;

void cipher(char word[], int count, int key)
{
    int i = 0;
    while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind += key;
        if(ind >= char_num)
            ind -= char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}

void decipher(char word[], int count, int key)
{
    int i = 0;
        while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind -= key;
        if(ind < 0)
            ind += char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}


int main()
{
    char text[] = "ABJT;";
    int len = strlen(text);
    std::cout << text << std::endl;
    cipher(text, len, 2);
    std::cout << text << std::endl;
    decipher(text, len, 2);
    std::cout << text << std::endl;
    return 0;
}



but still I have some problems

I just need decryption like code 1 but it should support Turkish characters and should work like 1. program.

Thanks.
to support Turkish,you shold set language enveronment by:
1
2
3
4
5
std::locale loc = std::locale::global(std::locale("trk"));  // set language Turkish enveronment 
// 
//  do sth. you'd like
//
std::locale::global(loc);  // reset language enveronment 


try soon.
Last edited on
read how you would implement unicode in your code. for unicode you would need unsigned short rather than char. there are some libraries which helps in these tasks like ICU.
Well I am beginner so it is hard to understand these clues for now :)

I tried that std::locale loc ...............

but didnt work


this link can be usefull for me ?

http://blogs.msdn.com/b/michkap/archive/2008/05/12/8486796.aspx
with this code I can get Turkish charachters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <windows.h>

int main(void)
{
char *szText="Türkçe Karakterler : ç,ğ,ı,ö,ş,ü idi sanırım :) \n";
char *szBuf=(char*)malloc(strlen(szText)+1);
CharToOem(szText,szBuf);
printf("%s\n",szBuf);
getch();
 
system("pause");
return 0;
}





how can I obtain these letters with

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
# include <iostream>
# include <cstring>

const char alphabet[] ={'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'Ğ', 'H', 'I',
                        'İ', 'J', 'K', 'L', 'M', 'N', 'O', 'Ö', 'P', 'R', 'S',
                        'Ş', 'T', 'U', 'Ü', 'V', 'Y', 'Z', '0', '1', '2', '3',
                        '4', '5', '6', '7', '8', '9', '.', ',', ':', ';', ' '};
const int char_num =44;

void cipher(char word[], int count, int key)
{
    int i = 0;
    while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind += key;
        if(ind >= char_num)
            ind -= char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}

void decipher(char word[], int count, int key)
{
    int i = 0;
        while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind -= key;
        if(ind < 0)
            ind += char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}


int main()
{
    char text[] = "ABJT;";
    int len = strlen(text);
    std::cout << text << std::endl;
    cipher(text, len, 2);
    std::cout << text << std::endl;
    decipher(text, len, 2);
    std::cout << text << std::endl;
    return 0;
}




You shoudl NEVER use goto. That's a bad habit. Work with loops/functions.

You'll get the hang of it.

I wrote a cypher encryption algorithm. What it did was create a vector which contained every character. It operated much like the shifting alphabet code, but instead it included every type-able character. This was also used to identify and convert characters from the password into numeric values that the algorithm used to shift it. It was a very complex procedure, as the single number which could represent a password was much larger than the memory limits of an integer or unsigned long long. Anyway, the point is, there are ways around unicode.
Last edited on
Topic archived. No new replies allowed.