回 帖 发 新 帖 刷新版面

主题:[原创]加密算法系列

好久没来了,最近的安全课,编了不少加密算法,放上来与大家共同切磋^_^

之下去的一系列加密算法都继承与一下基类

//Encrypt system 
//author: smallrain
//2006.3.22 in fzu

//MyCipherSystem.h


#ifndef ENCRYPT_SYSTEM
#define ENCRYPT_SYSTEM

#include <string>
using std::string;

typedef char Byte;




#endif //ENCRYPT_SYSTEM

回复列表 (共43个回复)

11 楼

//VigenereCipherSystem.cpp
#include "VigenereCipherSystem.h"


//////////////////////////////////////////////////////////////////////////////////////
////////////////        Implement class of VigenereEncryptSystem    //////////////////
//////////////////////////////////////////////////////////////////////////////////////

bool VigenereEncryptSystem::GetKey(const string& str)
{
    keyWord = "";
    int i;
    for (i=0; i<(int)str.length(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z') keyWord += str[i] + 32;
        else if (str[i] >= 'a' && str[i] <= 'z') keyWord += str[i];
    }

    if (keyWord.length() == 0) return false;
    else return true;
}

void VigenereEncryptSystem::Encrypt()
{
    int len = (int)keyWord.length();

    int i;
    for (i=0; i<(int)text.length(); i++)
    {
        char col = keyWord[i % len];
        text[i] = (text[i] + col - 2 * 97) % 26 + 97;
    }
    return;
}

void VigenereEncryptSystem::Decrypt()
{
    int len = (int)keyWord.length();

    int i;
    for (i=0; i<(int)text.length(); i++)
    {
        char row = keyWord[i % len];
        text[i] = (text[i] + 26 - row) % 26 + 97;
    }
    return;
}

//////////////////////////////////////////////////////////////////////////////////////

12 楼

以上是古典密码部分
接下去开始现代密码
这个论坛好像代码太多会出错
所以我一个实现文件会分成几部分贴

13 楼

//线性移位反馈寄存器 LFSR
//LFSRCipherSystem.h

#ifndef LFSR_CIPHER_SYSTEM_H
#define LFSR_CIPHER_SYSTEM_H

#include "MyCipherSystem.h"
#include "GlobalFunctions.h"

class LFSRCipherSystem : public EncryptSystem
{
public:

    void Encrypt();
    void Decrypt();

    void GetKey(const string& key, int feedback[], int feedbackLen);
    void GetData(const string& input);

    friend void BinaryToChar(const string& binaryStr, string& charStr);
    friend void CharToBinary(const string& charStr, string& binaryStr);

    
private:
    string shiftRegister;
    string keyStream;
};


#endif//LFSR_CIPHER_SYSTEM_H

14 楼

//LFSRCipherSystem.cpp

#include "LFSRCipherSystem.h"


//////////////////////////////////////////////////////////////////////////////////////
////////////////        Implement class of LFSRCipherSystem   ////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

        
void LFSRCipherSystem::GetData(const string& input)
{
    text = input;
}

void LFSRCipherSystem::GetKey(const string& key, int feedback[], int feedbackLen)
{
    keyStream.erase();

    CharToBinary(key, shiftRegister);

    int len = (int)text.length() * 8;
    int i;
    for (i=0; i<len; i++)
    {
        char in = char(0);
        int j;
        for (j=0; j<feedbackLen; j++) in ^= shiftRegister[feedback[j]];
        
        keyStream += shiftRegister[shiftRegister.length() - 1];
        shiftRegister.erase(shiftRegister.length() - 1);
        shiftRegister.insert(0, in);
    }
}

void LFSRCipherSystem::Encrypt()
{
    string temp = text;
    CharToBinary(temp, text);
    int i;
    for (i=0; i<(int)text.length(); i++) (text[i] ^= keyStream[i]) += 0x30;
}

void LFSRCipherSystem::Decrypt()
{
    int i;
    for (i=0; i<(int)text.length(); i++) (text[i] -= 0x30) ^= keyStream[i];

    string temp = text;
    BinaryToChar(temp, text);
    return;
}

//////////////////////////////////////////////////////////////////////////////////////

15 楼

忘记了,现代密码开始包含了头文件GlobalFunctions.h

下面是它的声明和定义

//GlobalFunctions.h

#ifndef GLOBAL_FUNCTIONS_H
#define GLOBAL_FUNCTIONS_H

#include <string>
#include <deque>
using std::deque;
using std::string;
typedef deque<bool> binary_stream;

void BinaryToChar(const string& binaryStr, string& charStr);
void CharToBinary(const string& charStr, string& binaryStr);

void StringToCString(const string& str, CString& cstr);
void CStringToString(const CString& cstr, string& str);

void CharToBinary(const string& str, binary_stream& binaryStream);
void BinaryToChar(const binary_stream& binaryStream, string& str);

#endif//GLOBAL_FUNCTIONS_H

16 楼

//GlobalFunctions.cpp


#include "GlobalFunctions.h"

void CharToBinary(const string& charStr, string& binaryStr)
{
    binaryStr.erase();
    int i;
    for (i=0; i<(int)charStr.length(); i++)
    {
        char temp = charStr[i];

        int j;
        for (j=0; j<8; j++)
        {
            if (temp & (0x80 >> j))
            {
                int test = 1;
                binaryStr += test;
            }
            else 
            {
                int test = 0;
                binaryStr += test;
            }
        }
    }
    return;
}

void BinaryToChar(const string& binaryStr, string& charStr)
{
    charStr.erase();
    int mulArray[] = {128, 64, 32, 16, 8 ,4, 2, 1};
    int mulArrayDisplace = 0;
    int temp = 0;
    int i;
    for (i=0; i<(int)binaryStr.length(); i++)
    {
        temp += mulArray[mulArrayDisplace++] * (int)binaryStr[i];
        if (mulArrayDisplace == 8)
        {
            charStr += temp;
            temp = 0;
            mulArrayDisplace = 0;
        }
    }
}

void CharToBinary(const string& str, binary_stream& binaryStream)
{
    binaryStream.clear();
    int i;
    for (i=0; i<(int)str.length(); i++)
    {
        int j;
        for (j=0; j<8; j++)
        {
            if (str[i] & (0x80 >> j)) binaryStream.push_back(true);
            else binaryStream.push_back(false);
        }
    }
    return;
}

void BinaryToChar(const binary_stream& binaryStream, string& str)
{
    str.erase();
    int mulArray[] = {128, 64, 32, 16, 8 ,4, 2, 1};
    int mulArrayDisplace = 0;
    int temp = 0;
    int i;
    for (i=0; i<(int)binaryStream.size(); i++)
    {
        temp += mulArray[mulArrayDisplace++] * (int)binaryStream[i];
        if (mulArrayDisplace == 8)
        {
            str += char(temp);
            temp = 0;
            mulArrayDisplace = 0;
        }
    }    
}
void StringToCString(const string& str, CString& cstr)
{
    cstr.Empty();
    int i;
    for (i=0; i<(int)str.length(); i++) cstr += str[i];
}

void CStringToString(const CString& cstr, string& str)
{
    str.erase();
    int i;
    for (i=0; i<(int)cstr.GetLength(); i++) str += cstr[i];
}

17 楼

//RC4
//RC4CipherSystem.h

#ifndef RC4_CIPHER_SYSTEM_H
#define RC4_CIPHER_SYSTEM_H

#include "MyCipherSystem.h"
#include "GlobalFunctions.h"

class RC4CipherSystem : public EncryptSystem
{
public:

    RC4CipherSystem();
    void GetData(const string& input);
    void GetKey(int valueOfKarray[], int arrayLen);

    void Encrypt();
    void Decrypt();

    friend void BinaryToChar(const string& binaryStr, string& charStr);
    friend void CharToBinary(const string& charStr, string& binaryStr);

private:

    void SetKarray(int array[], int arrayLen);
    void SetSarray();
    void KSA();
    Byte PSGA();

private:
    int iS;
    int jS;

    string keyStream;

    Byte K[256];
    Byte S[256];
};


#endif//RC4_CIPHER_SYSTEM_H

18 楼

//RC4CipherSystem.cpp

#include "RC4CipherSystem.h"

//////////////////////////////////////////////////////////////////////////////////////
////////////////        Implement class of RC4CipherSystem    ////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
RC4CipherSystem::RC4CipherSystem() : iS(0), jS(0) {}

void RC4CipherSystem::GetKey(int valueOfKarray[], int arrayLen)
{
    SetKarray(valueOfKarray, arrayLen);
    SetSarray();
    KSA();

    keyStream = "";
    int i;
    for (i=0; i<(int)text.length(); i++) keyStream += PSGA();
    
    return;
}

void RC4CipherSystem::GetData(const string& input)
{
    text = input;
}

inline void RC4CipherSystem::SetSarray()
{
    int i;
    for (i=0; i<256; i++) S[i] = Byte(i);
}

void RC4CipherSystem::SetKarray(int array[], int arrayLen)
{
    int arrayDisplace = 0;
    int i = 0;
    while (i < 256) K[i++] = (Byte)array[arrayDisplace++ % arrayLen];

    return;
}

void RC4CipherSystem::KSA()
{
    int i, j=0;
    for(i=0; i<256; i++)
    {
        j = (j + S[i] + K[i]) % 256;

        Byte t = S[i];
        S[i] = S[j];
        S[j] = t;
    }
}

Byte RC4CipherSystem::PSGA()
{
    iS = (iS + 1) % 256;
    jS = (jS + S[iS]) % 256;
    
    Byte t = S[iS];
    S[iS] = S[jS];
    S[jS] = t;

    t = (S[iS] + S[jS]) % 256;

    return S[t];
}

void RC4CipherSystem::Encrypt()
{
    int i;
    for (i=0; i<(int)text.length(); i++) text[i] ^= keyStream[i];
    string temp = text;
    CharToBinary(temp, text);
    for (i=0; i<(int)text.length(); i++) text[i] += 0x30;
}

void RC4CipherSystem::Decrypt()
{
    int i;
    for (i=0; i<(int)text.length(); i++) text[i] -= 0x30;
    string temp = text;
    BinaryToChar(temp, text);
    for (i=0; i<(int)text.length(); i++) text[i] ^= keyStream[i];
}

19 楼

接下来是分组密码,先是很有名的DES
还有IDEA

20 楼

//des中要用到的矩阵
//GlobalConstArrayForDES.h

#ifndef GLOBAL_CONST_ARRAYS_FOR_DES_H
#define GLOBAL_CONST_ARRAYS_FOR_DES_H


const int IP_ARRAY[64] = 
{
    58, 50, 42, 34, 26, 18, 10, 2,
    60, 52, 44, 36, 28, 20, 12, 4,
    62, 54, 46, 38, 30, 22, 14, 6,
    64, 56, 48, 40, 32, 24, 16, 8,
    57, 49, 41, 33, 25, 17,  9, 1,
    59, 51, 43, 35, 27, 19, 11, 3,
    61, 53, 45, 37, 29, 21, 13, 5,
    63, 55, 47, 39, 31, 23, 15, 7
};

const int ATHWART_IP_ARRAY[64] = 
{
    40, 8, 48, 16, 56, 24, 64, 32,
    39, 7, 47, 15, 55, 23, 63, 31,
    38, 6, 46, 14, 54, 22, 62, 30,
    37, 5, 45, 13, 53, 21, 61, 29,
    36, 4, 44, 12, 52, 20, 60, 28,
    35, 3, 43, 11, 51, 19, 59, 27,
    34, 2, 42, 10, 50, 18, 58, 26,
    33, 1, 41,  9, 49, 17, 57, 25
};

const int S_BOX[8][4][16] = 
{
    {
        {14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
        {0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
        {4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
        {15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}
    },
    {
        {15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
        {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
        {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
        {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}
    },
    {
        {10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
        {13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
        {13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
        {1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}
    },
    {
        {7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
        {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
        {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
        {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
    },
    {
        {2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
        {14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
        {4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
        {11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}
    },
    {
        {12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
        {10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
        {9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
        {4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}
    },
    {
        {4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
        {13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
        {1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
        {6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}
    },
    {
        {13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
        {1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
        {7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
        {2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
    },
};

const int P_BOX[32] =
{
    16,  7, 20, 21,
    29, 12, 28, 17,
     1, 15, 23, 26,
     5, 18, 31, 10,
     2,  8, 24, 14,
    32, 27,  3,  9,
    19, 13, 30,  6,
    22, 11,  4, 25
};

const int PC_1_BOX[56]=
{
    57, 49, 41, 33, 25, 17,  9,
     1, 58, 50, 42, 34, 26, 18,
    10,  2, 59, 51, 43, 33, 27,
    19, 11,  3, 60, 52, 44, 36,
    63, 55, 47, 39, 31, 23, 15,
     7, 62, 54, 46, 38, 30, 22,
    14,  6, 61, 53, 45, 37, 29,
    21, 13,  5, 28, 20, 12,  4
};    

const int PC_2_BOX[48]=
{
    14, 17, 11, 24,  1,  5,
     3, 28, 15,  6, 21, 10,
    23, 19, 12,  4, 26,  8,
    16,  7, 27, 20, 13,  2,
    41, 52, 31, 37, 47, 55,
    30, 40, 51, 45, 33, 48,
    44, 49, 39, 56, 34, 53,
    46, 42, 50, 36, 29, 32
};

#endif //GLOBAL_CONST_ARRAYS_FOR_DES_H

我来回复

您尚未登录,请登录后再回复。点此登录或注册