controlpro

DC 본문

암호학

DC

controlpro 2021. 4. 23. 13:13
728x90
// TestAppDll.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
int Sbox[16] = { 0xE, 0x3, 0x0, 0x7, 0x2, 0xC, 0xF, 0xB, 0x5, 0xA, 0x6, 0x9, 0x8, 0x1, 0x4, 0xD };
int InverseSbox[16] = { 0x2, 0xD, 0x4, 0x1, 0xE, 0x8, 0xA, 0x3, 0xC, 0xB, 0x9, 0x7, 0x5, 0xF, 0x0, 0x6 };

extern "C" __declspec(dllexport) void Substitution(int* p, int* c)
{
   *c = (Sbox[(*p >> 12 & 0xf)] << 12) | (Sbox[(*p >> 8 & 0xf)] << 8) | (Sbox[(*p >> 4 & 0xf)] << 4) | (Sbox[(*p & 0xf)]);
}

extern "C" __declspec(dllexport) void Permutation(int* p, int* c)
{
   *c = ((*p >> 15 & 1) << 15) | ((*p >> 11 & 1) << 14) | ((*p >> 7 & 1) << 13) | ((*p >> 3 & 1) << 12) |
      ((*p >> 14 & 1) << 11) | ((*p >> 10 & 1) << 10) | ((*p >> 6 & 1) << 9) | ((*p >> 2 & 1) << 8) |
      ((*p >> 13 & 1) << 7) | ((*p >> 9 & 1) << 6) | ((*p >> 5 & 1) << 5) | ((*p >> 1 & 1) << 4) |
      ((*p >> 12 & 1) << 3) | ((*p >> 8 & 1) << 2) | ((*p >> 4 & 1) << 1) | ((*p & 1));
}

extern "C" __declspec(dllexport) void Substitution_Inverse(int* p, int* c)
{
   *c = (InverseSbox[(*p >> 12 & 0xf)] << 12) | (InverseSbox[(*p >> 8 & 0xf)] << 8) |
      (InverseSbox[(*p >> 4 & 0xf)] << 4) | (InverseSbox[(*p & 0xf)]);
}

extern "C" __declspec(dllimport) void Substitution(int* p, int* c);
extern "C" __declspec(dllimport) void Substitution_Inverse(int* p, int* c);
extern "C" __declspec(dllimport) void Permutation(int* p, int* c);
extern "C" __declspec(dllimport) void Encryption(int P, int* C);
int key[16][2];
int main(int argc, char* argv[])
{
   
   int Plaintext, Ciphertext;
   int input_key= 0x0000;
   Plaintext = 0x0006;
   int plaintext_1 , plaintext_2;
   int ciphertext_1 , ciphertext_2;
   int j;
   int temp1=0, temp2=0;
   int result1=0, result2=0;
   int result_1=0, result_2=0;
   int result;
   int count = 0;
   int temp_r;
   int stop;
   for (int i = 0; i < 63000; i++)//
   {

   srand(time(NULL));
   plaintext_1 = (rand()*i) % 0xFFFF;
   plaintext_2 = plaintext_1 ^ 0x6000; //
   printf("Plaintext_1 = 0x%x    Plaintext_2 = 0x%x\n", plaintext_1, plaintext_2);
   Encryption(plaintext_1, &ciphertext_1);
   Encryption(plaintext_2, &ciphertext_2);
   for (j = 0; j <= 0xF000; j += 0x1000) { //
   printf("Round : %d\nInput_key : 0x%04x\n", i, j);
   input_key = j;
   temp1 = ciphertext_1;
   temp2 = ciphertext_2;
   temp1 ^= input_key;
   temp2 ^= input_key;
   result1 = result2 = result_1 = result_2 = 0;
   for (int k = 3; k >= 0; k--) {
   temp_r = temp1 >> (k * 4);
   result1 = InverseSbox[(temp_r) & 0x000f];
   if (k != 0) {
   result_1 += result1;
   result_1 = result_1 << 4;
   }
   else
   result_1 += result1;
   }
   for (int k = 3; k >= 0; k--) {
   temp_r = temp2 >> (k * 4);
   result2 = InverseSbox[temp_r & 0x000f];
   if (k != 0) {
   result_2 += result2;
   result_2 = result_2 << 4;
   }
   else
   result_2 += result2;
   }
   result = result_1 ^ result_2;
   if (result == 0x6000) { //
   key[input_key >> 12][0] = input_key;//
   key[input_key >> 12][1] += 1;//

   printf("0x%04x is correct\n", input_key);
   }
   }
   }
   //   Plaintext = 0x0000;
   printf("키의 종류      나온개수\n");
   for (int i = 0; i < 16; i++) {
   printf("0x%04x            %d\n" , key[i][0] , key[i][1]);

   }
   
   

   return 0;
}
728x90
반응형

'암호학' 카테고리의 다른 글

LC  (0) 2021.04.23