암호학

22390 단어 securityFinanceFinance

카이사르 암호(Caesar cipher)

"시저 암호"라고도 불림

암호화를 할때 평문, 즉 보내고자 하는 메시지를 n번 씩 밀려서 암호화 하는 방법

매우 쉬운 만큼 복호화 과정도 쉬움

ex) I love cookie.

Key 값암호문
3
4
5

복호화(Decryption) 코드

#include <stdio.h>
#include <string.h>

int main()
{
	char message[10000] = { 0, };
	char ch;
	int i, key;

	printf("Caesar Cipher Decryption\n");
	printf("\n");
	printf("Ciphertext: KYVXIVRKVJKXCFIPZECZMZEXCZVJEFKZEEVMVIWRCCZEXSLKZEIZJZEXVMVIPKZDVNVWRCC.EVCJFEDREUVCR.KYVNRPKFXVKJKRIKVUZJKFHLZKKRCBZEXREUSVXZEUFZEX.NRCKUZJEVP.PFLIKZDVZJCZDZKVUJFUFEFKNRJKVZKCZMZEXJFDVFEVVCJVJCZWV.UFEFKSVKIRGGVUSPUFXDR.NYZTYZJCZMZEXNZKYKYVIVJLCKJFWFKYVIGVFGCVJKYZEBZEX.JKVMVAFSJ.ZWCZWVNVIVGIVUZTKRSCVZKNFLCUTVRJVKFSVCZWVREUSVNZKYFLKWCRMFI.VCVREFIIFFJVMVCK.ZWPFLCFFBRKNYRKPFLYRMVZECZWVPFLNZCCRCNRPJYRMVDFIV.ZWPFLCFFBRKNYRKPFLUFEFKYRMVZECZWVPFLNZCCCEVMVIYRMVVEFLXY.FGIRYNZEWIVP.ZWPFLJVKPFLIXFRCJIZUZTLCFLJCPYZXYREUZKZJRWRZCLIVPFLNZCCWRZCRSFMVVMVIPFEVVCJVJJLTTVJJ.ARDVJTRDVIFE.CZWVZJNYRKYRGGVEJNYVEPFLRIVSLJPDRBZEXFKYVIGCREJ.AFYECVEEFE.JGIVRUCFMVVMVIPNYVIVPFLXF.CVKEFFEVVMVITFDVKFPFLNZKYFLKCVRMZEXYRGGZVI.DFKYVIKVIVJR.NYVEPFLIVRTYKYVVEUFWPFLIIFGVKZVRBEFKZEZKREUYREXFE.WIREBCZEU.IFF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++i) {
		ch = message[i];

		if (ch >= 'a' && ch <= 'z') {
			ch = ch - key;

			if (ch < 'a') {
				ch = ch + 'z' - 'a' + 1;
			}

			message[i] = ch;
		}
		else if (ch >= 'A' && ch <= 'Z') {
			ch = ch - key;

			if (ch < 'A') {
				ch = ch + 'Z' - 'A' + 1;
			}

			message[i] = ch;
		}
		else if (ch == '.') {
			message[i] = ch;
		}
	}

	printf("plaintext: %s\n", message);

	return 0;
}

비즈네르 암호(Vigenere chipher)

다중 단일 문자 치환 암호법의 한 종류로 키워드를 이용한 암호법

<장점>

  • 빈도수 분석법으로 해독이 불가능하다.
  • 사용자의 key 개수가 무궁무진하다.

ex)
원문 : johnwick
Key 값 : sky

암호문 : byffgguu

복호화(Decryption) 코드

#include <stdio.h>
#include <string.h>

int main()
{
    char message[10000] = { 0, };
    char key[10] = { 0, };
    char msg_char, key_char;
    int i, msg_len, key_len;

    printf("Vigenere Cipher Decryption\n");
    printf("\n");
    printf("Ciphertext: SQRQEDCUENMSUENMSUENMSUENMSZLIDHYIFVCFVCKIJITROKWMIDCENCDCENCDCENCDCENCWDAEKFZRGKZCQYPOMSRSHVFRKBUWBBOEHSLFRRDCPIENNGXMVOAVWOWSJTAMSWSRLCKLMIVRRDCVRRDCVRRDCVRRDCMVEHISRLCKIJIIDUFISYBRRDYBRRDYBRRDSHXSECCEENNCEENNCECEKVZXHBCNQYROEHSEDZRTRSRMRCCDITSAVWSKMZRGKMVSHQCKXAVSKKOSKRRNKQVPELFRXEKBUPIFSDCLSTVWAIWEKAICYFALMCITCUFGAEGVAEQCEVOMYKLICQCYBGSXSNQCRPLXWXLTGSXSNVWXLTSHLTLSYVMTCRPRAWWKICKIJIIDCCHYYIFRCOBFAIDCCHYYIKAIMSNIGYBCMGRHZXUZZZOESHJHYXODMTOWTEMOHFQOFSDSVOAFZEWCMIGOHFYTDVVAAICWQEKBUQYMFVACBSNGROKTVEGWDMNDVVGLEPJSIWUFRNKRFHONCUSJEGKAHKHKLEPITOCKAVLEBSKSDYRFHONCPIARMVEHMOLWESHXSECCEENNCEENNCEENNWKKOOGFRAXRFRAXRFRYOOYMTRFFAMIVRRDCIGMNDVVEIBGFQEDWDISCOPMNQOPSHQCKXAVSKKOSKRRNKQVPELFRXEKBUPIFSDCLSTVWAIWEKAICYFALMCITCUFGAEGVAEQCEVOMYKLICQCYBGSXSNQCRPLXWXLTGSXSNVWXLTSHLTLSYVMTCRPRAWWKICKIJIIDCCHYYIFRCOBFAIDCCHYYIKAIMSNIGYBCMGRHZXUZZZOESHJHYXODMTOWDKOXBRXAUSZXAVZZMMQCERALSKLEVOJXOXSJXAXRZRGRWXLEBCMIRKZCMIWUFRNKPVXHOZRWTYBVPAXRZRGMOLWESWZFEVWVZESHRRDSWZMJEGKAAXHZXAVZZNUCHNENDWKELVWDKOXBRTUDAPLAXRJMNDVVEIBVRRDCWEXHOOZVPEHPSUBVRRDCWEXHOOZVIDVISWWMYENNGLTIXHYIASFJSMOHZQECGRCIXURCORUFXTKZVXGYWNENXOTILOPIETOOEHLSJVQYVWWISKMZRGKMFLBKPPPEDGXSCKIJIWOUFRRYQBXHSGTPULKVKOXUFELVBZKHDKVKOXZZKHDWKYPVWBIIDGUCNKAZXEMOLWESHFPDICLSNMSESWSHFPDICLXWSQVAEQCEPIQVKMTEDCMKOWKWDIBRQIDS\n");
    printf("\n");
    printf("Key: korea\n");
    printf("\n");

    strcpy_s(message, 10000, "SQRQEDCUENMSUENMSUENMSUENMSZLIDHYIFVCFVCKIJITROKWMIDCENCDCENCDCENCDCENCWDAEKFZRGKZCQYPOMSRSHVFRKBUWBBOEHSLFRRDCPIENNGXMVOAVWOWSJTAMSWSRLCKLMIVRRDCVRRDCVRRDCVRRDCMVEHISRLCKIJIIDUFISYBRRDYBRRDYBRRDSHXSECCEENNCEENNCECEKVZXHBCNQYROEHSEDZRTRSRMRCCDITSAVWSKMZRGKMVSHQCKXAVSKKOSKRRNKQVPELFRXEKBUPIFSDCLSTVWAIWEKAICYFALMCITCUFGAEGVAEQCEVOMYKLICQCYBGSXSNQCRPLXWXLTGSXSNVWXLTSHLTLSYVMTCRPRAWWKICKIJIIDCCHYYIFRCOBFAIDCCHYYIKAIMSNIGYBCMGRHZXUZZZOESHJHYXODMTOWTEMOHFQOFSDSVOAFZEWCMIGOHFYTDVVAAICWQEKBUQYMFVACBSNGROKTVEGWDMNDVVGLEPJSIWUFRNKRFHONCUSJEGKAHKHKLEPITOCKAVLEBSKSDYRFHONCPIARMVEHMOLWESHXSECCEENNCEENNCEENNWKKOOGFRAXRFRAXRFRYOOYMTRFFAMIVRRDCIGMNDVVEIBGFQEDWDISCOPMNQOPSHQCKXAVSKKOSKRRNKQVPELFRXEKBUPIFSDCLSTVWAIWEKAICYFALMCITCUFGAEGVAEQCEVOMYKLICQCYBGSXSNQCRPLXWXLTGSXSNVWXLTSHLTLSYVMTCRPRAWWKICKIJIIDCCHYYIFRCOBFAIDCCHYYIKAIMSNIGYBCMGRHZXUZZZOESHJHYXODMTOWDKOXBRXAUSZXAVZZMMQCERALSKLEVOJXOXSJXAXRZRGRWXLEBCMIRKZCMIWUFRNKPVXHOZRWTYBVPAXRZRGMOLWESWZFEVWVZESHRRDSWZMJEGKAAXHZXAVZZNUCHNENDWKELVWDKOXBRTUDAPLAXRJMNDVVEIBVRRDCWEXHOOZVPEHPSUBVRRDCWEXHOOZVIDVISWWMYENNGLTIXHYIASFJSMOHZQECGRCIXURCORUFXTKZVXGYWNENXOTILOPIETOOEHLSJVQYVWWISKMZRGKMFLBKPPPEDGXSCKIJIWOUFRRYQBXHSGTPULKVKOXUFELVBZKHDKVKOXZZKHDWKYPVWBIIDGUCNKAZXEMOLWESHFPDICLSNMSESWSHFPDICLXWSQVAEQCEPIQVKMTEDCMKOWKWDIBRQIDS");
    strcpy_s(key, 10, "korea");
    msg_len = strlen(message);
    key_len = strlen(key);

    for (i = 0; i < msg_len; i++)
    {
        if (message[i] >= 'A' && message[i] <= 'Z')
            msg_char = 'A';
        else if (message[i] >= 'a' && message[i] <= 'z')
            msg_char = 'a';
        if (key[i % key_len] >= 'A' && key[i % key_len] <= 'Z')
            key_char = 'A';
        else if (key[i % key_len] >= 'a' && key[i % key_len] <= 'z')
            key_char = 'a';
        
        message[i] = ((message[i] - msg_char - key[i % key_len] + key_char + 26) % 26) + msg_char;
    }

    printf("plaintext: %s\n", message);
    return 0;
}

좋은 웹페이지 즐겨찾기