본문 바로가기

Secure Note

Chaotic stream cipher via logistic map

무단 도용금지.

(실험적인 코드라 사실 가져다 써도 효율이 좋지못할 수 있음)

XOR 마스킹을 기본암호화방식으로 사용하고, 그 키값의 생성에 카오스함수인 로지스틱 맵을 사용하였다.



//ChaosStreamCipher.h

//code by alleysark

//2012.6.20

#ifndef CHAOS_STREAM_CIPHER_H

#define CHAOS_STREAM_CIPHER_H


#include<string.h>


namespace Sark{

class ChaosStreamCipher{

public:

ChaosStreamCipher(){

init_x = 0;

key=NULL;

}

~ChaosStreamCipher(){

if(key != NULL)

delete [] key;

}


bool SetSeed(double InitX){

if(InitX<0 || 1 < InitX)

return false;

init_x = InitX;

return true;

}

//설정한 init_x에 대해서 Length의 key stream을 생성시킨다.

//생성과 동시에 본 클래스의 키로 설정됨

const char* GenKey(int Length){

if(key != NULL)

delete [] key;


double xn = init_x;

for(int i=0; i<DiffusionN; i++)

xn = Alpha * xn * ( 1 - xn );

//DiffusionN 번 까진 기본 확산 시킴


char* _key = new char[Length];

memset(_key, 0, Length);

int j=-1;

for(int i=0; i<Length*8; i++){

if(i%8 == 0)

j++;

_key[j] <<= 1;

_key[j] |= (xn>=Critic?1:0);

xn = Alpha*xn*(1-xn);

}

RegisterKey(_key, Length);

return _key;

}

void RegisterKey(const char* Key, int KeyLength){

if(key != NULL)

delete [] key;

key = Key;

key_len = KeyLength;

}


//입력된 PlainText로 부터 암호화된 문자열스트림을 반환한다.

const char* Encryption(const char* PlainText){

if(PlainText == NULL || key == NULL)

return NULL;


char* cipher_t = new char[key_len+1];

memset(cipher_t, 0, key_len+1);

//XOR-ing

for(int i=0; i<key_len; i++)

cipher_t[i] = PlainText[i]^key[i];

return cipher_t;

}

const char* Decryption(const char* CipherText){

if(CipherText == NULL || key == NULL)

return NULL;


char* plain_t = new char[key_len+1];

memset(plain_t, 0, key_len+1);

for(int i=0; i<key_len; i++)

plain_t[i] = CipherText[i]^key[i];

return plain_t;

}

private:

static const int DiffusionN;

static const int Alpha;

static const double Critic;

double init_x;

const char* key;

int key_len;

};

const int ChaosStreamCipher::DiffusionN = 10000;

const int ChaosStreamCipher::Alpha = 4;

const double ChaosStreamCipher::Critic = 0.5;

}


#endif

'Secure Note' 카테고리의 다른 글

forensics ref site  (0) 2012.06.25
paros scan  (0) 2012.04.16
xss 우회  (0) 2012.03.09
free web proxy site  (0) 2012.03.07
md5  (0) 2011.11.05