controlpro
LC 본문
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 };
int guess_key[16] = { 0x0000 , 0x0001 , 0x0010 , 0x0011 , 0x0100 , 0x0101 , 0x0110 , 0x0111 , 0x1000 , 0x1001 ,0x1010 , 0x1011,0x1100 ,0x1101 ,0x1110 ,0x1111 };
int guess_key2[16] = { 0x0000 , 0x0002 , 0x0020 ,0x0022 ,0x0200 , 0x0202 , 0x0220 , 0x0222 , 0x2000 , 0x2002 , 0x2020 , 0x2022 , 0x2200 , 0x2202 ,0x2220 ,0x2222 };
int guess_key3[16] = { 0x0000 , 0x0004 , 0x0040 ,0x0044 , 0x0400 , 0x0404 ,0x0440 , 0x0444 , 0x4000 , 0x4004 ,0x4040 ,0x4044 , 0x4400 , 0x4404 ,0x4440, 0x4444 };
int guess_key4[16] = { 0x0000, 0x0008 , 0x0080 , 0x0088 ,0x0800 , 0x0808 ,0x0880 , 0x0888 ,0x8000 ,0x8008 , 0x8080 , 0x8088 , 0x8800 , 0x8808 , 0x8880 ,0x8888 };
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 main(int argc, char* argv[])
{
srand(time(NULL));
int temp1, temp2=0, temp3, temp4;
int Plaintext1, Ciphertext1;
int p[16];
int p1, p2, p3, p4;
int w, _w, __w, ___w;
int t, _t , __t;
int r, _r, __r;
int o, _o, __o;
int input_key[16][2] = { 0, };
int i = 0;
int j;
int a;
int count = 0;
int k = 0x0000;
int Round5key = 0xaa71;
int Round4key = 0xfaa7;
int Round3key = 0x7faa;
int Round2key = 0x67fa;
int Round1key = 0x267f;
int Round0key;
int numofplaintext;
int ptemp;
printf("input the number of plaintext : ");
scanf_s("%d", &numofplaintext);
for (i = 0; i < 16; i++) {
for (j = 0; j < numofplaintext; j++) {
Plaintext1 = ((rand() * j) % 0xffff) & 0xffff;
ptemp = Plaintext1;
//printf("guess key : 0x%04x plaintext = 0x%04x\n", guess_key[i], Plaintext1);
for (a = 0; a < 16; a++) {
p[a] = ptemp & 0x1;
ptemp >>= 1;
}
Encryption(Plaintext1, &Ciphertext1);
//round5
w = Ciphertext1 ^ Round5key;
//permutation
Substitution_Inverse(&w, &_w);
//round4
_w = _w ^ Round4key;
Permutation(&_w, &__w);
Substitution_Inverse(&__w, &t);
//round3
t = t ^ Round3key;
Permutation(&t, &_t);
Substitution_Inverse(&_t, &__t);
//round2
r = __t ^ Round2key;
Permutation(&r, &_r);
Substitution_Inverse(&_r, &__r);
//round1
o = __r ^ Round1key;
Permutation(&o, &_o);
Substitution_Inverse(&_o, &__o);
Round0key = Plaintext1 ^ __o;
printf("Round0key is 0x%04x\n", Round0key);
scanf_s("%d", &count);
while (getchar() != '\n');
if ( p[8] ^ ((temp2) &0x1 ) == 0) //
{
input_key[i][0] = guess_key3[i];
input_key[i][1] += 1;
}
}
if (input_key[i][1] >= (numofplaintext / 2))
input_key[i][1] -= (numofplaintext / 2);
else
input_key[i][1] = (numofplaintext / 2) - input_key[i][1];
}
int max = input_key[0][0];
int _max = input_key[0][1];
int index = 0;
for (i = 0; i < 16; i++) {
if (_max < input_key[i][1])
{
_max = input_key[i][1];
index = i;
}
}
for (int i = 0; i < 16; i++) {
printf("0x%04x : %d\n", guess_key3[i], input_key[i][1]);
}
printf("key : 0x%04x\n", guess_key3[index]);
return 0;
}
728x90
반응형