admin管理员组

文章数量:1312662

I am using visual studio c++ projects to write c code. The caesar encryption and decryption methods I made work fine if I don't use turkish characters. I am using Turkish(Windows) - Codepage 1254 encoding. Here is the code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<conio.h>
#include<string.h>


void ceaserEncrypt(char* point) {
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    char msg1[100];
    char* ptrmsg = msg1;
    printf("Şifrelenecek metni giriniz:");
    gets(msg0, sizeof(msg0));
    system("cls");
    int i;
    for (i = 0;msg0[i] != '\0';i++) {
        if (msg0[i] != ' ') {
            msg1[i] = msg0[i] + 5;
        }
        else {
            msg1[i] = ' ';
        }
    }
    msg1[i] = '\0';
    printf("encryption:%s", msg1);
    strcpy(point, msg1);
}
char a[100];
char* ptr = a;
void ceaserDecrypt(char str[]) {
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    strcpy(msg0, str);
    int i;
    for (i = 0;i < strlen(msg0);i++) {
        if (msg0[i] != ' ') {
            msg0[i] = msg0[i] - 5;
        }
        else {
            msg0[i] = ' ';
        }
    }
    printf("\nDecryption:%s", msg0);


}

int main(){
ceaserEncrypt(ptr);
ceaserDecrypt(a);
return 0;
}

This code first encrypts keyboard input, prints it, then decryptd the encrypted string and prints it.
When I type an input like "öğrenci" I expect the decryption method to print "öğrenci"; but instead it prints "§renci".

I am using visual studio c++ projects to write c code. The caesar encryption and decryption methods I made work fine if I don't use turkish characters. I am using Turkish(Windows) - Codepage 1254 encoding. Here is the code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<conio.h>
#include<string.h>


void ceaserEncrypt(char* point) {
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    char msg1[100];
    char* ptrmsg = msg1;
    printf("Şifrelenecek metni giriniz:");
    gets(msg0, sizeof(msg0));
    system("cls");
    int i;
    for (i = 0;msg0[i] != '\0';i++) {
        if (msg0[i] != ' ') {
            msg1[i] = msg0[i] + 5;
        }
        else {
            msg1[i] = ' ';
        }
    }
    msg1[i] = '\0';
    printf("encryption:%s", msg1);
    strcpy(point, msg1);
}
char a[100];
char* ptr = a;
void ceaserDecrypt(char str[]) {
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    strcpy(msg0, str);
    int i;
    for (i = 0;i < strlen(msg0);i++) {
        if (msg0[i] != ' ') {
            msg0[i] = msg0[i] - 5;
        }
        else {
            msg0[i] = ' ';
        }
    }
    printf("\nDecryption:%s", msg0);


}

int main(){
ceaserEncrypt(ptr);
ceaserDecrypt(a);
return 0;
}

This code first encrypts keyboard input, prints it, then decryptd the encrypted string and prints it.
When I type an input like "öğrenci" I expect the decryption method to print "öğrenci"; but instead it prints "§renci".

Share Improve this question edited Feb 1 at 14:44 Daniel Walker 6,7727 gold badges24 silver badges61 bronze badges asked Feb 1 at 2:48 Orhun TokdemirOrhun Tokdemir 295 bronze badges 12
  • 3 You are going to need a wider range. Use UTF-8 or wide characters. – Thomas Matthews Commented Feb 1 at 2:56
  • 3 I bet it breaks on û, U with circumflex. In codepage 1254, this has ASCII code 251, and 251 + 5 == 256 wraps around to \0, NUL terminating character. Then strcpy truncates the "encrypted" text. – Igor Tandetnik Commented Feb 1 at 2:59
  • 2 This is not Caesar cipher. The classic Caesar cipher wraps around; e.g. if you shift by 5, then A->F, B->G, ..., U->Z, V->A, W->B... Note how letters at the end of the alphabet are transformed into letters from the beginnig. Your code makes no attempt to do that, whether with Latin or with Turkish alphabet. – Igor Tandetnik Commented Feb 1 at 3:08
  • 2 This is not a beginner's task and I recommend not doing it at this stage of your career. If you really need to, you must know what you want the encryption result of each individual character to be for each value of the shift. – n. m. could be an AI Commented Feb 1 at 4:00
  • 2 The Windows console does not use the same codepage that the rest of Windows uses. That might be the root of your problem. What does the chcp command say? – Mark Ransom Commented Feb 1 at 15:19
 |  Show 7 more comments

1 Answer 1

Reset to default 0

The underlying problem with this code isn't enc/dec part of the code. The problem is I can't succesfully get turkish characters from the keyboard. I used "codepage-1254" as file encoding but the default for the console is "codepage-857". To overcome this a can use a system call to change the console encoding to "codepage-1254"

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include<locale.h>
#include<conio.h>
#include<string.h>
#include <windows.h>//library for the said system call

void ceaserEncrypt(char* point) {
    system("chcp 1254 > nul");//system call that changes the console encoding 
//to "codepage-1254"
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    char msg1[100];
    char* ptrmsg = msg1;
    printf("Şifrelenecek metni giriniz:");
    gets_s(msg0, sizeof(msg0));
    system("cls");
    int i;
    for (i = 0;msg0[i] != '\0';i++) {
        if (msg0[i] != ' ') {
            msg1[i] = msg0[i] + 5;
        }

        else {
            msg1[i] = ' ';
        }
    }
    msg1[i] = '\0';
    printf("encryption:%s", msg1);
    strcpy(point, msg1);
}
char text[100];
char* ptrtext = text;

void ceaserDecrypt(char str[]) {
    setlocale(LC_ALL, "turkish");
    char msg0[100];
    strcpy(msg0, str);
    int i;
    for (i = 0;i < strlen(msg0);i++) {
        if (msg0[i] != ' ') {
            msg0[i] = msg0[i] - 5;
        }

        else {
            msg0[i] = ' ';
        }
    }
    printf("\nDecryption:%s", msg0);
}

int main(){
    ceaserEncrypt(ptrtext);
    ceaserDecrypt(text);
    return 0;
}

the code now works without problems

now when I type "öğrenci" the code prints

encryption:ûõwjshn

Decryption:öğrenci

本文标签: cWhy do turkish characters break endecryptionStack Overflow