sha-1 알고리즘 코딩

2021. 5. 14. 23:35암호학/C언어

#include
#include <string.h>
// strlen 쓰려고
#include
//memcpy 쓰려고

#define block_size 512
#define round 80
#define static_word 32

using namespace std;
void text_move(char *, int);
int count1;
void char_to_int(char text);
void boohowha(int a);
int binary_text[block_size] = { 0, };
void length_boohowha(int a);
int W[round][static_word] = { 0, };
void W_SET();
void length_boohowha(unsigned int, int[]);
void f1(int b[], int c[], int d[], int e[]);
void f2(int b[], int c[], int d[], int e[]);
void f3(int b[], int c[], int d[], int e[]);
void f4(int b[], int c[], int d[], int e[]);
void reverse(int arr[], int start, int end);
void shiftLeft(int arr[], int d, int n);
void array_add(int a[], int c[]);
void array_add1(int a[], int c[]);

unsigned int h0 = 0x67452301;
unsigned int h1 = 0xEFCDAB89;
unsigned int h2 = 0x98BADCFE;
unsigned int h3 = 0x10325476;
unsigned int h4 = 0xC3D2E1F0;

unsigned int k1 = 0x5A827999;
unsigned int k2 = 0x6ED9EBA1;
unsigned int k3 = 0x8F1BBCDC;
unsigned int k4 = 0xCA62C1D6;

int main()
{

int binary_A[512] = { 0, };
int binary_B[512] = { 0, };
int binary_C[512] = { 0, };
int binary_D[512] = { 0, };
int binary_E[512] = { 0, };

int binary_k1[512] = { 0, };
int binary_k2[512] = { 0, };
int binary_k3[512] = { 0, };
int binary_k4[512] = { 0, };

int f1_result[512] = { 0, };
int temp[512] = { 0, };
int left_temp[512] = { 0, };



count1 = 0;

char text[100];

cout << "암호화할 코드를 입력하세요 : ";
cin >> text;
//cout << (int)'@' << endl;
//boohowha();


text_move(text, strlen(text));
binary_text[strlen(text) * 8] = 1;
// 패딩 맨 처음 배열 1 처리 

cout << "길이 : " << strlen(text) << endl;

int textlength = strlen(text) * 8;
// 텍스트 길이 -> 글자수 * 8(2진수 이므로 글자당 8개씩)
length_boohowha(textlength);

cout << "삽입 결과 : ";
for (int i = 0; i < block_size; i++) {
    // 텍스트 길이 x 글자당 2진수 갯수(8개)
    cout << binary_text[i];
}
cout << endl;

// W[0] ~ W[15] 설정
int k = 0;
for (int i = 0; i < 16; i++)
{
    cout << i << "번 째 : ";
    for (int j = 0; j < static_word; j++)
    {
        W[i][j] = binary_text[k];
        k++;
        cout << W[i][j];
    }
    cout << endl;
}

W_SET();

length_boohowha(h0, binary_A);
length_boohowha(h1, binary_B);
length_boohowha(h2, binary_C);
length_boohowha(h3, binary_D);
length_boohowha(h4, binary_E);

length_boohowha(k1, binary_k1);
length_boohowha(k2, binary_k2);
length_boohowha(k3, binary_k3);
length_boohowha(k4, binary_k4);

f1(binary_B, binary_C, binary_D, f1_result);

cout << "f1 결과 : ";
for (int i = 0; i < 512; i++)
{
    cout << f1_result[i];

}

memcpy(left_temp, binary_A, sizeof(int) * 512);
// a를 leftrotate 하기위해 임시로 temp에 넣어놈
shiftLeft(left_temp, 5, 512);

array_add(left_temp, temp);
array_add(f1_result, temp);
array_add(binary_E, temp);
array_add(binary_k1, temp);
array_add1(W[0], temp);

for (int j = 0; j < 80; j++)
{
    if (j < 20)
    {
        array_add(left_temp, temp);
        array_add(f1_result, temp);
        array_add(binary_E, temp);
        array_add(binary_k1, temp);
        array_add1(W[j], temp);
    }
}

/*
memcpy(binary_E, binary_D, sizeof(int) * 512);
memcpy(binary_D, binary_C, sizeof(int) * 512);
memcpy(binary_B, binary_A, sizeof(int) * 512);
memcpy(binary_A, temp, sizeof(int) * 512);
*/
return 0;

}

void text_move(char* text,int a)
{
for (int i = 0; i < a; i++)
// 한글자씩 부호화하기 위해 글자단위로 나눔
{
cout << text[i];
char_to_int(text[i]);
cout << endl;
}
}

void char_to_int(char text)
// 부호화하기위해 char형을 아스키코드로 변환하기 위해 int형으로 변환
{
int start = (int)text;
boohowha(start);
}

void boohowha(int a)
// 부호화 // 현재 사용하고 있는 문자들을 비트열에 대응시키는 것
// 이진연산 자체가 역순으로 나오기때문에 배열에 저장할 때도 역순으로 저장한다. 역순의 역순 -> 정순
{
int remainder;

int c[8] = { 0, };

int i = 7;

while (a != 1)
{

    remainder = a % 2;
    a = a / 2;
    c[i] = remainder;
    //cout << c[i] << endl;
    i--;

    if (a == 1)
    {
        c[i] = 1;
    }
}

for (i = 0; i < 8; i++)
{
    binary_text[count1] = c[i];
    count1++;
}

}

void length_boohowha(int a)
// 512비트중 나머지 64비트에 길이를 표시하기 위한 부호화
{
int remainder;
int block_num = 511;
// 블록크기는 512이지만 배열숫자는 -1인 511까지이므로 511로 지정

int i = 7;

while (a != 1)
{

    remainder = a % 2;
    a = a / 2;
    binary_text[block_num] = remainder;
    block_num--;

    if (a == 1)
    {
        binary_text[block_num] = 1;
    }
}

}

void W_SET()
{
for (int i = 16; i < 80; i++)
{
cout << i + 1 << "번 째 : ";
if ((W[i - 16][0] + W[i - 14][0] + W[i - 8][0] + W[i - 3][0]) % 2 == 0)
W[i][31] = 1;
else
W[i][31] = 0;

    for (int j = 1; j < 31; j++)
        // shift 연산 포함
    {
        if ((W[i - 16][j] + W[i - 14][j] + W[i - 8][j] + W[i - 3][j]) % 2 == 0)
            // 4개의 XOR 연산을 전부 더 해서 짝수일 경우 0, 홀수일 경우 1로 지정
            W[i][j-1] = 1;
        else
            W[i][j-1] = 0;
        cout << W[i][j-1];
    }
    cout << W[i][31];
    cout << endl;
}

}

void f1(int b[], int c[], int d[], int f_re[])
{

for (int i = 0; i < 512; i++)
{
    f_re[i] = (b[i] & c[i]) | ((!b[i]) & d[i]);
}

}

void f2(int b[], int c[], int d[], int f_re[])
{

for (int i = 0; i < 512; i++)
{
    f_re[i] = b[i] ^ c[i] ^ d[i];
}

}

void f3(int b[], int c[], int d[], int f_re[])
{
for (int i = 0; i < 512; i++)
{
f_re[i] = (b[i] & c[i]) | (b[i] & d[i]) | (c[i] & d[i]);
}
}

void f4(int b[], int c[], int d[], int f_re[])
{

for (int i = 0; i < 512; i++)
{
    f_re[i] = b[i] ^ c[i] ^ d[i];
}

}

void length_boohowha(unsigned int a, int binary_array[])
// 512비트중 나머지 64비트에 길이를 표시하기 위한 부호화
{
int remainder;
int block_num = 511;
// 블록크기는 512이지만 배열숫자는 -1인 511까지이므로 511로 지정

while (a != 1)
{

    remainder = a % 2;
    //cout << "remain : " << remainder << endl;
    a = a / 2;
    binary_array[block_num] = remainder;
    //cout << "배열 값 : " << binary_array[block_num] << endl;

    block_num--;

    if (a == 1)
    {
        binary_array[block_num] = 1;
    }
}
/*
for (int i = 0; i < 512; i++)
{
    cout << binary_array[i];
}
cout << endl;
*/

}

void reverse(int arr[], int start, int end)
{
int temp;
end = end - 1;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

void shiftLeft(int arr[], int d, int n)
{
reverse(arr, 0, d);
reverse(arr, d, n);
reverse(arr, 0, n);
}

void array_add(int a[], int c[])
{
int count = 511;
/*
cout << "\n배열 덧셈 테스트 : " << endl;
cout << "A, B 배열 : " << endl;
for (int i = 476; i < 512; i++)
{
cout << c[i];
}
cout << endl;
for (int i = 476; i < 512; i++)
{
cout << a[i];
}
cout << endl;
*/
for (int i = 511; i >= 0; i--)
{
c[i] = c[i] + a[i];
if (c[i] == 2)
{
c[i - 1] = c[i - 1] + 1;
c[i] = 0;
}

    if (c[i] == 3)
    {
        c[i - 1] = c[i - 1] + 1;
        c[i] = 1;
    }
}

for (int i = 476; i < 512; i++)
{
    cout << c[i];
}
cout << "\n" << endl;

}

void array_add1(int a[], int c[])
{
int temp[512] = { 0, };

/*
for (int i = 0; i < 32; i++)
{
    cout << a[i];
}
cout << endl;
*/

for (int i = 511; i > 479; i--)
{
    temp[i] = a[i - 480];
}
cout << endl;

/*
cout << "결과 : ";
for (int i = 0; i < 512; i++)
{
    cout << temp[i];
}
cout << endl;
*/

for (int i = 511; i >= 0; i--)
{
    c[i] = c[i] + temp[i];
    if (c[i] == 2)
    {
        c[i - 1] = c[i - 1] + 1;
        c[i] = 0;
    }

    if (c[i] == 3)
    {
        c[i - 1] = c[i - 1] + 1;
        c[i] = 1;
    }
}

}

'암호학 > C언어' 카테고리의 다른 글

C언어 공용체  (0) 2021.06.07
c언어 / c++ for문(반복문) 차이점  (0) 2021.06.03
sha-1  (0) 2021.05.13
sha-1  (0) 2021.05.12
Caesar Cipher(시저 암호) for C언어  (0) 2020.11.30