평문을 Vigenere cipher를 통해서 암호화해야 한다.
평문과 key의 문자 사이의 차를 이용해서 이동시키는 방식으로 암호화해 출력해야 한다.
반복문을 통해서 평문의 처음부터 끝까지 순서대로 암호화를 해줍니다. key는 반복적으로 이용이 되는데, key[i % strlen(key)]를 이용하면 대응되는 키의 문자를 쉽게 찾을 수 있습니다. 이를 이용해서 공백문자인 경우를 제외하고 순서대로 암호화를 해주면 됩니다.
#include <stdio.h>
#include <string.h>
char plain_text[30005], cipher_text[30005], key[30005], tmp;
int main(void)
{
gets(plain_text);
scanf("%s", key);
for (int i = 0; i < strlen(plain_text); i++)
{
if (plain_text[i] == ' ')
cipher_text[i] = ' ';
else
{
char tmp = plain_text[i] - (key[i % strlen(key)] - 'a') - 1;
if (tmp < 'a')
tmp += 26;
cipher_text[i] = tmp;
}
}
cipher_text[strlen(plain_text)] = '\0';
printf("%s", cipher_text);
return 0;
}