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".
1 Answer
Reset to default 0The 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
版权声明:本文标题:c - Why do turkish characters break en-decryption? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741889150a2403197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
û
, U with circumflex. In codepage 1254, this has ASCII code 251, and 251 + 5 == 256 wraps around to\0
, NUL terminating character. Thenstrcpy
truncates the "encrypted" text. – Igor Tandetnik Commented Feb 1 at 2:59chcp
command say? – Mark Ransom Commented Feb 1 at 15:19