main.c
===============================================
#include "AppLE-C.h"
int main(int argc, BYTE** argv){
int select=0, a=0;
//FILE* fi;// Input file pointer,
//FILE* fo;// Output file pointer,
//printf("숫자를 입력하세요 : ");
//scanf("%d", &select);
while(1){
printf("========================================================\n");
printf("================ AppLE 암복호화 시스템 ================\n");
printf("========================================================\n");
printf("= =\n");
printf("= 종료 <0>, 암호화 <1>, 복호화<2> =\n");
printf("= =\n");
printf("= =\n");
printf("= AppLE_ver_1.0 =\n");
printf("========================================================\n\n");
printf("숫자를 입력하세요 : ");
scanf("%d", &select);
switch(select){
case 0 :
printf("프로그램을 종료합니다.\n");
break;
case 1 :
printf("암호화를 시작합니다\n");
printf("Plain Text file : %s\nEncryption text file : %s\nkey : %s\n\n", argv[1], argv[2], argv[3]);
Calculation(argv[1], argv[2], argv[3]);
printf("====== 암호화가 완료 되었습니다. ======\n");
printf("====== [%s]에서 확인 하세요. ======\n", argv[2]);
break;
case 2 :
printf("복호화를 시작합니다\n");
printf("Encryption text file : %s\nDecryption text file : %s\nkey : %s\n\n", argv[1], argv[2], argv[3]);
Calculation(argv[1], argv[2], argv[3]);
printf("====== 복호화가 완료 되었습니다. ======\n");
printf("====== [%s]에서 확인 하세요. ======\n", argv[2]);
break;
default :
printf("잘못입력하셨습니다. 다시입력해주세요\n\n");
//break;
}
if(select>-1&&select<3) break;
}
return 0;
}
calculation.c
========================================================
#include
#include
#include "AppLE-C.h"
//#include "Key_Gen.h"
void Calculation(BYTE* input, BYTE* output, BYTE* key){
BYTE* seckey;
BYTE* buf_t;
BYTE* f_text;//완료된 텍스트
int strlength=0;
int i;
FILE* fi;
FILE* fo;
LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER start_ticks, end_ticks, diff_ticks;
QueryPerformanceCounter(&start_ticks);
//printf("111111\n");
fo=fopen(output, "wb");
if((fi=fopen(input, "rb"))==NULL){
printf("Input File open Err!!\n");
exit(1);
}
fseek(fi, 0, SEEK_END);
strlength=ftell(fi);
buf_t=(BYTE*)malloc(sizeof(BYTE)*(strlength+1));
seckey=(BYTE*)malloc(sizeof(BYTE)*(strlength+1));
memset(seckey, 0, sizeof(BYTE)*(strlength+1));
memset(buf_t, 0, sizeof(BYTE)*(strlength+1));
fseek(fi, 0, SEEK_SET);
while(!feof(fi)){
fread(buf_t, sizeof(BYTE), strlength, fi);
}
fclose(fi);
//printf("============== 파일에서 읽은 BUF ================\n");
//for(i=0;i
// printf("%x ", buf_t[i]);
//printf("\n==================================================\n\n");
buf_t[strlength]='\0';
//---키젠 실행부분--
Key_Gen(key, seckey,strlength);
QueryPerformanceCounter(&end_ticks);
QueryPerformanceFrequency(&ticksPerSecond);
//printf ("ticksPerSecond: %I64Ld\n",ticksPerSecond.QuadPart);
diff_ticks.QuadPart = end_ticks.QuadPart- start_ticks.QuadPart;
//printf ("ticks: %I64Ld\n",diff_ticks.QuadPart);
printf("=============== Keystream (seckey) ===============\n");
for(i=0;i
if(i%16==0)
printf("\n");
printf("%x ", seckey[i]);
}
printf("\n==================================================\n\n");
printf("==================================================\n");
printf("난수생성시간 : %.12f sec\n",
((double)diff_ticks.QuadPart/(double)ticksPerSecond.QuadPart));
printf("\n==================================================\n\n");
f_text=(BYTE*)malloc(sizeof(BYTE)*strlength+1);
for(i=0;i
f_text[i]=seckey[i]^buf_t[i] ;
}
f_text[strlength]='\0';
//for(i=0;i
// fputc(buf_t, fo);
fwrite(f_text, sizeof(BYTE), strlength, fo);
//printf("=============== XOR 연산후 BUF ===================\n");
//for(i=0;i
// printf("%X ", f_text[i]);
//printf("\n==================================================\n\n");
fclose(fo);
}
Key_Gen.c
================================================================
#include "AppLE-C.h"
#include "Key_Gen.h"
//Initial MD value
void Key_Gen(BYTE* key, BYTE* seckey, int strlength)
{
BYTE kbuf[128]={0, };
BYTE k_strlen[8]={0, };
BYTE hashtobyte[8][8]={0, };
BYTE hashbuf[64]={0,};
int blocksize=0;
int count=0;
int i, j, block=0;
WORDS final_hash[8]={0, };
WORDS* exword;
WORDS str;
exword=(WORDS *)malloc(sizeof(WORDS)*80);
//printf("kbuf : ");
for(i=0;i<(strlen(key));i++){
kbuf[i]=key[i];
//printf("%x ", kbuf[i]);
}
//printf("\n ");
str=i*8;
//padding
kbuf[i]=0x80;
//----strlenth
WordtoByte(str, k_strlen);
for(i=0;i<8;i++)
kbuf[120+i]=k_strlen[i];
WordEx(kbuf, exword);
Round(exword, block, final_hash);
for(i=0;i<8;i++){
WordtoByte(final_hash[i], hashtobyte[i]);
//printf("final_hash : %llx\n", final_hash[i]);
}
//printf("hashbuf : ");
for(i=0;i<8;i++){
for(j=0;j<8;j++){
hashbuf[i*8+j]=hashtobyte[i][j];
//printf("%x ", hashbuf[i*8+j]);}
}
//printf("\n");
}
KeyArr(hashbuf, seckey, strlength);
}
// Word Extention
void WordEx(BYTE* bufs, WORDS* Word){
int r;
int i;
BYTE temp[8]={0, };
WORDS temp1=0;
WORDS wtemp=0, wtemp1=0,wtemp2=0,wtemp3=0;
//printf("WordEx : %s", buf);
for(r=0;r<80;r++){
if(r<16){
for(i=0;i<8;i++)
temp[i]=bufs[r*BLOCKSIZE+i];
BytetoWord(temp, &temp1);
Word[r]=temp1;
memset(temp, 0, sizeof(temp));
temp1=0;
}
else if(r>15){
wtemp=Word[r-16];
//Rotate shift l-m-n
wtemp1=RotShift1(Word[r-15]);
wtemp2=Word[r-7];
wtemp3=RotShift2(Word[r-2]);
Word[r]=wtemp^wtemp1^wtemp2^wtemp3;
}
//printf("round %d : %llx\n", r, Word[r]);
}
}
//--buf의 byte 8을 Word(64bits) 로 변환
void BytetoWord(BYTE* p, WORDS* word){
int i;
for(i=0; i<8; i++){
*word |= (WORDS)p[i] << (56-(i*BLOCKSIZE));
}
//getAbit2(" byte to word :", *word, 64);
}
void getAbit2(char* a, WORDS x, int bit) { //
int n=0;
WORDS mask= 0x8000000000000000;
printf("%s\n", a);
for(n=0; n
if((x & (mask >> n))>0)
printf("1");
else
printf("0");
}
printf(" ");
printf("\n");
}
void Round(WORDS* word, int block, WORDS* hash){
WORDS at=0, bt=0, ct=0, dt=0, et=0, ft=0, gt=0, ht=0;
WORDS a1=0, b1=0, c1=0, d1=0, e1=0, f1=0, g1=0, h1=0;
WORDS xo, yo, maj, rot,con;
int round;
for(round=0;round<80;round++){
if(round==0){
at=0x6a09e667f3bcc908;
bt=0xbb67ae8584caa73b;
ct=0x3c6ef372fe94f82b;
dt=0xa54ff53a5f1d36f1;
et=0x510e527fade682d1;
ft=0x9b05688c2b3e6c1f;
gt=0x1f83d9abfb41bd6b;
ht=0x5be0cd19137e2179;
}
b1=at;
c1=bt;
d1=ct;
f1=et;
g1=ft;
h1=gt;
maj=(WORDS)Majority(at, bt, ct);
//printf("maj : %llx", maj);
rot=Rotate(at, 28, 34, 39);
//printf("rot : %llx", rot);
a1=(maj+rot);
rot=0;
con=(WORDS)Conditional(et, ft, gt);
rot=Rotate(et, 14, 18, 41);
e1=(ht+con+rot+word[round]+K[round]);
//x
xo=(a1+e1);
//y
yo=(e1+dt);
at=xo;
bt=b1;
ct=c1;
dt=d1;
et=yo;
ft=f1;
gt=g1;
ht=h1;
}
hash[0]=(at+a);
hash[1]=(bt+b);
hash[2]=(ct+c);
hash[3]=(dt+d);
hash[4]=(et+e);
hash[5]=(ft+f);
hash[6]=(gt+g);
hash[7]=(ht+h);
}
int qwer(int a){
int count=0;
while(1){
a/=2;
if(a<2){
break;
}
count++;
}
return count;
}
void WordtoByte(WORDS str, BYTE* out){
int i;
WORDS mask = 0xFF00000000000000;
//printf("word to str : %llx\n", str);
for(i=0; i<8; i++){
out[i] = (str & (mask >> (i*BLOCKSIZE))) >> (56-(i*BLOCKSIZE));
//printf("word to byte : %x\n", out[i]);
//printf("final_hash : %llx\n", out[i]);
}
}
void KeyArr(BYTE* hash , BYTE* seckey, int strlength){
int i;
BYTE* temp;
temp=(BYTE*)malloc(sizeof(BYTE)*(strlength+1));
for(i=0;i
//printf("hash : %x\n", hash[i%64]);
hash[i%64]=RotShift1(hash[i%64]);
temp[i]=hash[i%64];
//printf("%d, temp : %x, hash : %x ", i, temp[i], hash[i%64]);
seckey[i]=Makekey(temp[i], i%7);
//printf("%x \n", seckey[i]);
}
//printf("Sec key Length : %d\n",strlength);
}
main.h
======================================================
#ifndef __HEADER__
#define __HEADER__
#include
#include
#include
#include
#include
typedef unsigned char BYTE;
typedef unsigned long long WORDS;
void Calculation(BYTE* input, BYTE* output, BYTE* key);
void Key_Gen(BYTE* key, BYTE* seckey, int strlength);
#endif
Key_Gen.h
=========================================================
#ifndef __KEYGEN__
#define __KEYGEN__
//Conditional
#define Conditional(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
//Majority
#define Majority(x,y,z) (((x) & (y) ) ^ ((x) & (z)) ^ ((y) & (z)))
//Rotate
#define RotR(in, value) ((in<<(64-value)) | (in>>value))
#define Rotate(in, i, j, k) (RotR(in, i) ^ RotR(in, j)^ RotR(in, k))
//shift Left
//*****
#define ShiftL(in, value) (in<
//ROtate Word
#define RotShift1(in) (RotR(in, 1) ^ RotR(in, 8) ^ ShiftL(in, 7))//
#define RotShift2(in) (RotR(in, 19) ^ RotR(in, 61) ^ ShiftL(in, 6))
#define BUFSIZE 128
#define BLOCKSIZE 8
#define Makekey(in, value) ((in<<(8-value))|(in>>value))
WORDS a=0x6a09e667f3bcc908;
WORDS b=0xbb67ae8584caa73b;
WORDS c=0x3c6ef372fe94f82b;
WORDS d=0xa54ff53a5f1d36f1;
WORDS e=0x510e527fade682d1;
WORDS f=0x9b05688c2b3e6c1f;
WORDS g=0x1f83d9abfb41bd6b;
WORDS h=0x5be0cd19137e2179;
//Key
/* initial key value */
WORDS K[80] = {
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
};
void BytetoWord(BYTE* p, WORDS* Word);
void WordEx(BYTE* bufs, WORDS* Word);
void getAbit2(char* a, WORDS x, int bit);
void Round(WORDS* Word, int block, WORDS* hash);
int qwer(int a);
void WordtoByte(WORDS str, BYTE* out);
void KeyArr(BYTE* hash , BYTE* seckey, int strlength);
#endif