c언어 암호화 복호화코드인데 왜틀렸을까요(비즈네르 암호)

익명 사용자의 이미지

#include
#include
#include
#include
#define MAX_MSG_SIZE 100
#define MAX_KEY 26
#define MAX_KEY_SIZE 9
#define TRUE 1
#define FALSE 0
char getMode();
void getMessage(char Message[]);
void getKey(char key[]);
void translateMessage(char mode, char key[], char Message[], char TMessage[]);
char getMode()
{
int mode;
while(TRUE)
{
printf("==========================\n");
printf("e.메세지 암호화\n");
printf("d.암호문 복호화\n");
printf("==========================\n");
printf(">>기능을 선택하세요 : ");
mode=getchar();
mode=tolower(mode);
if(mode=='e'||mode=='d')
{
return mode;
}
}
}
void getMessage(char Message[])
{
fflush(stdin);
printf(">>메세지를 입력하세요.\n");
gets(Message);
}
void getKey(char key[])
{
int idx;
while(TRUE)
{
printf(">>키 값을 입력하세요(최대 %d글자): ", MAX_KEY_SIZE -1);
scanf("%s", key);
for(idx=0; key[idx] && islower(key[idx]); idx++);
if(idx==strlen(key))
{
return;
}
}
}
void translateMessage(char mode, char key[], char Message[], char TMessage[])
{
char symbol; int idx, num, keyidx=0;;
memset(TMessage, 0, MAX_MSG_SIZE);
for(idx=0; idx {
symbol=Message[idx];
if(isalpha(symbol)){
num=symbol;
if(mode=='e')
{
num+=key[keyidx]-'a';
if((isupper(symbol) && num>'Z')||(islower(symbol) && num>'z'))
{
num-=MAX_KEY;
}
}
else
{
num-=key[keyidx]-'a';
if((isupper(symbol) && num<'A')||(islower(symbol) && num<'a'))
{
num+=MAX_KEY;
}
}
TMessage[idx]=num;
}
else
{
TMessage[idx]=symbol;
}
keyidx=(keyidx+1)%strlen(key);
}
}
int main()
{
char mode;
char key[MAX_KEY_SIZE];
char Message[MAX_MSG_SIZE];
char TMessage[MAX_MSG_SIZE];

mode=getMode();
switch(mode){
case 'e':
printf("메시지를 암호화합니다.\n");
getMessage(Message);
getKey(key);
translateMessage(mode, key, Message, TMessage);
printf("암호문 : %s\n", TMessage);
case 'd':
printf("메시지를 복호화합니다.\n");
getMessage(Message);
getKey(key);
translateMessage(mode, key, Message, TMessage);
printf("복호문 : %s\n", TMessage);
}
}

ㄹ머ㅣㅈ더;ㅓㅏㅅ의 이미지

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX_MSG_SIZE 100
#define MAX_KEY 26
#define MAX_KEY_SIZE 9
#define TRUE 1
#define FALSE 0
char getMode();
void getMessage(char Message[]);
void getKey(char key[]);
void translateMessage(char mode, char key[], char Message[], char TMessage[]);
char getMode()
{
    int mode;
    while(TRUE)
    {
        printf("==========================\n");
        printf("e.메세지 암호화\n");
        printf("d.암호문 복호화\n");
        printf("==========================\n");
        printf(">>기능을 선택하세요 : ");
        mode=getchar();
        mode=tolower(mode);
        if(mode=='e'||mode=='d')
        {
            return mode;
        }
    }
}
void getMessage(char Message[])
{
    fflush(stdin);
    printf(">>메세지를 입력하세요.\n");
    gets(Message);
}
void getKey(char key[])
{
    int idx;
    while(TRUE)
    {
        printf(">>키 값을 입력하세요(최대 %d글자): ", MAX_KEY_SIZE -1);
        scanf("%s", key);
        for(idx=0; key[idx] && islower(key[idx]); idx++);
        if(idx==strlen(key))
        {
            return;
        }
    }
}
void translateMessage(char mode, char key[], char Message[], char TMessage[])
{
    char symbol; int idx, num, keyidx=0;;
    memset(TMessage, 0, MAX_MSG_SIZE);
    for(idx=0; idx<strlen(Message); idx++)
    {
        symbol=Message[idx];
        if(isalpha(symbol)){
            num=symbol;
            if(mode=='e')
            {
                num+=key[keyidx]-'a';
                if((isupper(symbol) && num>'Z')||(islower(symbol) && num>'z'))
                {
                    num-=MAX_KEY;
                }
            }
            else
            {
                num-=key[keyidx]-'a';
                if((isupper(symbol) && num<'A')||(islower(symbol) && num<'a'))
                {
                    num+=MAX_KEY;
                }
            }
            TMessage[idx]=num;
        }
        else
        {
            TMessage[idx]=symbol;
        }
        keyidx=(keyidx+1)%strlen(key);
    }
}
int main()
{
    char mode;
    char key[MAX_KEY_SIZE];
    char Message[MAX_MSG_SIZE];
    char TMessage[MAX_MSG_SIZE];
 
    mode=getMode();
    switch(mode){
        case 'e':
            printf("메시지를 암호화합니다.\n");
            getMessage(Message);
            getKey(key);
            translateMessage(mode, key, Message, TMessage);
            printf("암호문 :  %s\n", TMessage);
        case 'd':
            printf("메시지를 복호화합니다.\n");
            getMessage(Message);
            getKey(key);
            translateMessage(mode, key, Message, TMessage);
            printf("복호문 :  %s\n", TMessage);
    }
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.