回 帖 发 新 帖 刷新版面

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

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

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

//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个回复)

21 楼

//DESCipherSystem.h

#ifndef DES_CIPHER_SYSTEM_H
#define DES_CIPHER_SYSTEM_H

#include "MyCipherSystem.h"
#include "GlobalConstArraysForDes.h"
#include "GlobalFunctions.h"
#include <deque>
#include <string>

using std::deque;
using std::string;

typedef deque<bool> binary_stream;

class DESCipherSystem : public EncryptSystem
{
public:
    void GetData(const string& input);
    void GetKey(const string& key);
    void Encrypt();
    void Decrypt();

private:
    binary_stream subKeyStream48bit[16];
    binary_stream keyWord;
private:

    //初始变换
    //return 64 bit binary stream after IP; ok
    binary_stream IP(const binary_stream& BinaryStream64bit);  
    //逆初始变换 ok
    binary_stream AthwartIP(const binary_stream& BinaryStream64bit);

    //乘积变换
    //return 64 bit binary stream
    binary_stream ProductSwitch(const binary_stream& BinaryStream64bit, 
                         const binary_stream& keyStream64bit, int mark = 1); 

    //SUB 乘积变换, f函数
    // input 32 bit binary stream, and the subKeyStream48bit[16] which is the member of the class
    //         and the subKeyStream48bit displace which named i;
    // return 32 bit binary stream
    binary_stream Ffunction(const binary_stream& BinaryStream32bit, int i);

    //SUB of Ffunction, 扩展置换
    //input 32 bit binary stream
    //return 48 bit binary stream
    binary_stream Expand(const binary_stream& BinaryStream32bit);

    //SUB of Ffunction, S盒变换
    //input 48 bit binary stream, and a global S table;
    //return 32 bit binary stream
    binary_stream SboxPermutation(const binary_stream& BinaryStream48bit);

    //SUB of Ffunction, P盒变换
    //input 32 bit binary stream, and a global p table;
    //return 32 bit binary stream
    binary_stream PboxPermutation(const binary_stream& BinaryStream32bit);

    //SUB 乘积变换, 子密钥的生成
    //input: 64 bit binary stream, attention:the 64 bit binary stream should be change into 56 bit in the function
    //output: no ouput, but set the value of subKeyStream48bit[16]
    void SubKeyCreation(const binary_stream& keyStream64bit);

    //SUB 子密钥的生成, 置换选择1
    //input: 56 bit binary stream(key), 28 bit binary stream C, 28 bit binary stream D, C and D should be empty
    //       and a PC-1 table;
    //output: return void, but change the value of C and D above
    void PC_1(const binary_stream& keyStream56bit, binary_stream& keyStreamC28bit, binary_stream& keyStreamD28bit);

    //SUB 子密钥的生成, 循环左移计算
    //input: 28 bit key binary stream C and D, 
    //output: return void, the C and D's value will be changed
    void CycleLeftShift(binary_stream& keyStreamC28bit, binary_stream& keyStreamD28bit, int round);

    //SUB 子密钥的生成, 置换选择2
    //input: 28 bit key binary stream C and D, and a PC-2 table
    //output:return void , but set the value of the subKeyStream48bit[16];
    void PC_2(const binary_stream& keyStreamC28bit, const binary_stream& keyStreamD28bit, int displace);
};


#endif //DES_CIPHER_SYSTEM_H

22 楼

//DESCipherSystem.cpp

#include "DESCipherSystem.h"

void DESCipherSystem::PC_2(const binary_stream& keyStreamC28bit, const binary_stream& keyStreamD28bit, int displace)
{
    int i;
    binary_stream temp56bit = keyStreamC28bit;
    for (i=0; i<(int)keyStreamD28bit.size(); i++) temp56bit.push_back(keyStreamD28bit[i]);

    subKeyStream48bit[displace].clear();
    for (i=0; i<48; i++) 
    {
        subKeyStream48bit[displace].push_back((temp56bit[PC_2_BOX[i] - 1]));
    }
}

void DESCipherSystem::CycleLeftShift(binary_stream& keyStreamC28bit, binary_stream& keyStreamD28bit, int round)
{
    int cycleNum;
    switch (round)
    {
    case 1:
    case 2:
    case 9:
    case 16: cycleNum = 1; break;
    default: cycleNum = 2;
    }
    int i;
    for (i=0; i<cycleNum; i++)
    {
        bool temp = keyStreamC28bit.front();
        keyStreamC28bit.push_back(temp);
        keyStreamC28bit.pop_front();

        temp = keyStreamD28bit.front();
        keyStreamD28bit.push_back(temp);
        keyStreamD28bit.pop_front();
    }
}

void DESCipherSystem::PC_1(const binary_stream& keyStream56bit, binary_stream& keyStreamC28bit, binary_stream& keyStreamD28bit)
{
    keyStreamC28bit.clear();
    keyStreamD28bit.clear();

    int i;
    for (i=0; i<28; i++) keyStreamC28bit.push_back(keyStream56bit[i]);
    for (; i<56; i++) keyStreamD28bit.push_back(keyStream56bit[i]);
}

void DESCipherSystem::SubKeyCreation(const binary_stream& keyStream64bit)
{
    binary_stream temp56bit;
    int i;
    for (i=0; i<56; i++) temp56bit.push_back(keyStream64bit[PC_1_BOX[i] - 1]);
    
    binary_stream keyStreamC28bit, keyStreamD28bit;
    PC_1(temp56bit, keyStreamC28bit, keyStreamD28bit);
    for (i=1; i<=16; i++)
    {
        CycleLeftShift(keyStreamC28bit, keyStreamD28bit, i);
        PC_2(keyStreamC28bit, keyStreamD28bit, i - 1);
    }

}

binary_stream DESCipherSystem::PboxPermutation(const binary_stream& BinaryStream32bit)
{
    binary_stream temp32bit;
    int i;
    for (i=0; i<32; i++) temp32bit.push_back(BinaryStream32bit[P_BOX[i] - 1]);
    return temp32bit;
}

binary_stream DESCipherSystem::SboxPermutation(const binary_stream& BinaryStream48bit)
{
    binary_stream result;
    int sValue;
    int sBoxID = 0;
    int i = 0;
    for (; sBoxID<8; sBoxID++)
    {
        binary_stream temp6bit;
        int j;
        for (j=0; j<6; j++) temp6bit.push_back(BinaryStream48bit[i++]);

        int sBoxRow = int(temp6bit[0]) * 2 + int(temp6bit[5]);
        int sBoxCol = int(temp6bit[1] * 8) + int(temp6bit[2]) * 4 
            + int(temp6bit[3]) * 2 + int(temp6bit[4]);
        sValue = char(S_BOX[sBoxID][sBoxRow][sBoxCol]);

        for (j=0; j<4; j++)
        {
            if (sValue & (0x8 >> j)) result.push_back(true);
            else result.push_back(false);
        }
    }
    return result;
}

binary_stream DESCipherSystem::Expand(const binary_stream& BinaryStream32bit)
{
    binary_stream temp48bit;
    temp48bit.push_back(BinaryStream32bit[31]);
    int i;
    for (i=0; i<32; i++)
    {
        temp48bit.push_back(BinaryStream32bit[i]);
        if ((i % 4 == 0) && i != 0)
        {
            temp48bit.push_back(BinaryStream32bit[i - 1]);
            temp48bit.push_back(BinaryStream32bit[i]);
        }
    }
    temp48bit.push_back(BinaryStream32bit[0]);
    return temp48bit;
}

binary_stream DESCipherSystem::Ffunction(const binary_stream& BinaryStream32bit , int displace)
{
    binary_stream temp48bit = Expand(BinaryStream32bit);
    int i;
    for (i=0; i<48; i++) temp48bit[i] ^= this->subKeyStream48bit[displace][i];
    return PboxPermutation(SboxPermutation(temp48bit));
}

23 楼

//DESCipherSystem.cpp 续

binary_stream DESCipherSystem::ProductSwitch(const binary_stream& BinaryStream64bit,
                                        const binary_stream& keyStream64bit, int mark)
{
    binary_stream leftStream32bit,
           rightStream32bit,
           leftStream32bitLater,
           rightStream32bitLater;

    int i;
    for (i=0; i<32; i++) leftStream32bit.push_back(BinaryStream64bit[i]);
    for (; i<64; i++) rightStream32bit.push_back(BinaryStream64bit[i]);

    SubKeyCreation(keyStream64bit);
    for (i=0; i<16; i++)
    {
        leftStream32bitLater = rightStream32bit;
        if (mark == 1) rightStream32bitLater = Ffunction(rightStream32bit, i);   // Encrypt
        else rightStream32bitLater = Ffunction(rightStream32bit, 15 - i);         // Decrypt
        int j;
        for (j=0; j<32; j++) rightStream32bitLater[j] ^= leftStream32bit[j];
        leftStream32bit = leftStream32bitLater;
        rightStream32bit = rightStream32bitLater;
    }
    for (i=0; i<32; i++) rightStream32bit.push_back(leftStream32bit[i]);
    return rightStream32bit;
}

void DESCipherSystem::GetData(const string& input)
{
    text = input;
    int left = (int)text.length() % 8;             //确保输入是8的倍数
    int i;
    if (left != 0) left = 8 - left;
    for (i=0; i<left; i++) text += ' ';
}

binary_stream DESCipherSystem::IP(const binary_stream& BinaryStream64bit)
{
    binary_stream temp;
    int i;
    for (i=0; i<64; i++) temp.push_back(BinaryStream64bit[IP_ARRAY[i] - 1]);
    return temp;
}

binary_stream DESCipherSystem::AthwartIP(const binary_stream& BinaryStream64bit)
{
    binary_stream temp;
    int i;
    for (i=0; i<64; i++) temp.push_back(BinaryStream64bit[ATHWART_IP_ARRAY[i] - 1]);
    return temp;
}
void DESCipherSystem::GetKey(const string& key)
{
    CharToBinary(key, keyWord); 
}

24 楼

//DESCipherSystem.cpp 续

void DESCipherSystem::Encrypt()
{
    binary_stream reserveText;
    binary_stream cipherBinaryStream;
    CharToBinary(text, reserveText);

    binary_stream plainText64bit;
    binary_stream keyWord64bit;

    text.erase();
    int textDisplace = 0;
    int keyWordDisplace = 0;
    while (textDisplace < (int)reserveText.size())
    {
        plainText64bit.clear();
        keyWord64bit.clear();

        int i;
        for (i=0; i<64; i++) plainText64bit.push_back(reserveText[textDisplace++]);
        for (i=0; i<64; i++) keyWord64bit.push_back(keyWord[keyWordDisplace++ % (int)keyWord.size()]);
        plainText64bit = (AthwartIP(ProductSwitch(IP(plainText64bit), keyWord64bit, 1)));
        for (i=0; i<(int)plainText64bit.size(); i++) cipherBinaryStream.push_back(plainText64bit[i]);
    }
    text.reserve(int(cipherBinaryStream.size()));
    int i;
    for (i=0; i<(int)cipherBinaryStream.size(); i++) text += char(int(cipherBinaryStream[i]) + 48);
}

void DESCipherSystem::Decrypt()
{
    binary_stream reserveText;
    binary_stream cipherBinaryStream;
    int i;
    for (i=0; i<(int)text.length(); i++)
    {
        if (text[i] == '0') cipherBinaryStream.push_back(false);
        else cipherBinaryStream.push_back(true);
    }
    binary_stream cipherText64bit;
    binary_stream keyWord64bit;
    int textDisplace = 0;
    int keyWordDisplace = 0;
    while (textDisplace < (int)cipherBinaryStream.size())
    {
        cipherText64bit.clear();
        keyWord64bit.clear();

        for (i=0; i<64; i++) cipherText64bit.push_back(cipherBinaryStream[textDisplace++]);
        for (i=0; i<64; i++) keyWord64bit.push_back(keyWord[keyWordDisplace++ % (int)keyWord.size()]);
        cipherText64bit = AthwartIP(ProductSwitch(IP(cipherText64bit), keyWord64bit, 0));
        for (i=0; i<(int)cipherText64bit.size(); i++) reserveText.push_back(cipherText64bit[i]);
    }
    BinaryToChar(reserveText, text);
}

25 楼

//IDEA

//IDEACipherSystem.h

#ifndef IDEA_CIPHER_SYSTEM_H
#define IDEA_CIPHER_SYSTEM_H

#include "MyCipherSystem.h"
#include "GlobalFunctions.h"
#include <deque>
#include <string>

using std::deque;
using std::string;

typedef deque<bool> binary_stream;

class IDEACipherSystem : public EncryptSystem
{
public:
    //ensure the input stream is the multiple of eight
    void GetData(const string& input);
    void GetKey(const string& key);
    void Encrypt();
    void Decrypt();

private:
    //influence the  subKeyStored
    void SubKeyCreation(const binary_stream& keyBinaryStream128bit);
    //influence the subKeyStored which has been influenced by the SubKeyCreation
    void SubKeyModifyForDecrypt();
    //influence the subDataBlock
    void SetSubDataBlock(const binary_stream& textBinaryStream64bit);

    //input: subKeyStored & subDataBlock
    //output: no return but change the value of the subDataBlock16bit array;
    void Iterative();

    int BinaryStreamToInt(const binary_stream& binaryStream16bit);
    void IntToBinaryStream(int result, binary_stream& binaryStream16bit);

    //mod 65537 mul
    int MulMod65537(int muled, int mul);
    int AddMod65536(int added, int add);
    int AddMod2(int added, int add);

    int AthwartMul(int x);
    int AthwartAdd(int x);
private:
    static const int MOD_ADD_2_16 = 65536;
    static const int MOD_MUL_2_16_1 = 65537;
    static const int MOD_ADD_2 = 2;

    int subKeyStored16bit[52];
    int subDataBlock16bit[4];
    binary_stream keyBinaryStream;
};
#endif //IDEA_CIPHER_SYSTEM_H

26 楼

//IDEACipherSystem.cpp

#include "IDEACipherSystem.h"

//////////////////////////////////////////////////////////////////////////////////////
////////////////        Implement class of IDEAEncryptSystem        //////////////////
//////////////////////////////////////////////////////////////////////////////////////

int IDEACipherSystem::AthwartMul(int x)
{
    if (x == 0 || x == 65536) return 65536;
    unsigned int i;
    for (i=1; ; i += 65537)
    {
        if (i % x == 0) return i / x;
    }
}

int IDEACipherSystem::AthwartAdd(int x)
{
    int i;
    for (i=0; ; i += MOD_ADD_2_16)
    {
        int temp = i - x;
        if (temp >= 0) return temp;
    }
    return 0;
}

void IDEACipherSystem::SubKeyModifyForDecrypt()
{
    int tempArray[52];
    int displace = 0;
    int i;
    for (i=0; i<4; i++) tempArray[displace++] = subKeyStored16bit[48 + i];
    for (i=42; i>=0; i-=6)
    {
        tempArray[displace++] = subKeyStored16bit[i + 4];
        tempArray[displace++] = subKeyStored16bit[i + 5];
        int j;
        for (j=0; j<4; j++) tempArray[displace++] = subKeyStored16bit[i + j];
    }
    for (i=0; i<52; i++)
    {
        int select = i % 6;
        switch (select)
        {
        case 0: ;
        case 3: subKeyStored16bit[i] = AthwartMul(tempArray[i]);
            break;
        case 1: ;
        case 2: subKeyStored16bit[i] = AthwartAdd(tempArray[i]);
            break;
        default: subKeyStored16bit[i] = tempArray[i];
        }
    }
    for (i=6; i<=42; i+=6)
    {
        int temp = subKeyStored16bit[i + 1];
        subKeyStored16bit[i + 1] = subKeyStored16bit[i + 2];
        subKeyStored16bit[i + 2] = temp;
    }
}

void IDEACipherSystem::Decrypt()
{
    binary_stream plainBinaryStream;
    binary_stream cipherBinaryStream;
    int i;
    for (i=0; i<(int)text.length(); i++)
    {
        if (text[i] == '0') cipherBinaryStream.push_back(false);
        else cipherBinaryStream.push_back(true);
    }

    binary_stream cipherText64bit;
    binary_stream keyStream128bit;
    int textDisplace = 0;
    int keyWordDisplace = 0;

    while (textDisplace < (int)cipherBinaryStream.size())
    {
        cipherText64bit.clear();
        keyStream128bit.clear();

        for (i=0; i<64; i++) cipherText64bit.push_back(cipherBinaryStream[textDisplace++]);
        for (i=0; i<128; i++) 
            keyStream128bit.push_back(keyBinaryStream[keyWordDisplace++ % (int)keyBinaryStream.size()]);
        SubKeyCreation(keyStream128bit);
        SubKeyModifyForDecrypt();
        SetSubDataBlock(cipherText64bit);
        Iterative();

        for (i=0; i<4; i++)
        {
            IntToBinaryStream(subDataBlock16bit[i], cipherText64bit);
            int j;
            for (j=0; j<(int)cipherText64bit.size(); j++) plainBinaryStream.push_back(cipherText64bit[j]);
        }
    }
    BinaryToChar(plainBinaryStream, text);
}

void IDEACipherSystem::Encrypt()
{
    binary_stream textReserve;
    CharToBinary(text, textReserve);

    binary_stream plainText64bit;
    binary_stream cipherBinaryStream;
    binary_stream keyStream128bit;
    int textDisplace = 0;
    int keyDisplace = 0;

    while (textDisplace < (int)textReserve.size())
    {
        plainText64bit.clear();
        keyStream128bit.clear();

        int i;
        for (i=0; i<64; i++) plainText64bit.push_back(textReserve[textDisplace++]);
        for (i=0; i<128; i++) 
            keyStream128bit.push_back(keyBinaryStream[keyDisplace++ % (int)keyBinaryStream.size()]);

        SubKeyCreation(keyStream128bit);
        SetSubDataBlock(plainText64bit);
        Iterative();

        for (i=0; i<4; i++)
        {
            IntToBinaryStream(subDataBlock16bit[i], plainText64bit);
            int j;
            for (j=0; j<(int)plainText64bit.size(); j++) 
            {
                cipherBinaryStream.push_back(plainText64bit[j]);
            }
        }
    }
    text.erase();
    text.reserve(int(cipherBinaryStream.size()));
    int i;
    for (i=0; i<(int)cipherBinaryStream.size(); i++) text += char(int(cipherBinaryStream[i]) + 48);
}


void IDEACipherSystem::GetKey(const string& str)
{
    CharToBinary(str, keyBinaryStream);
}

void IDEACipherSystem::GetData(const string& input)
{
    text = input;
    int left = (int)text.length() % 8;             //确保输入是8的倍数
    int i;
    if (left != 0) left = 8 - left;
    for (i=0; i<left; i++) text += ' ';
}

27 楼

//IDEACipherSystem.cpp 续

void IDEACipherSystem::Iterative()
{
    int displace = 0;
    int i;
    for (i=0; i<8; i++)
    {
        int step1 = MulMod65537(subDataBlock16bit[0], subKeyStored16bit[displace++]);
        int step2 = AddMod65536(subDataBlock16bit[1], subKeyStored16bit[displace++]);
        int step3 = AddMod65536(subDataBlock16bit[2], subKeyStored16bit[displace++]);
        int step4 = MulMod65537(subDataBlock16bit[3], subKeyStored16bit[displace++]);
        int step5 = AddMod2(step1, step3);
        int step6 = AddMod2(step2, step4);
        int step7 = MulMod65537(step5, subKeyStored16bit[displace++]);
        int step8 = AddMod65536(step6, step7);
        int step9 = MulMod65537(step8, subKeyStored16bit[displace++]);
        int step10 = AddMod65536(step7, step9);
        int step11 = AddMod2(step1, step9);
        int step12 = AddMod2(step3, step9);
        int step13 = AddMod2(step2, step10);
        int step14 = AddMod2(step4, step10);

        subDataBlock16bit[0] = step11;
        subDataBlock16bit[1] = step12;
        subDataBlock16bit[2] = step13;
        subDataBlock16bit[3] = step14;
    }

    int temp = subDataBlock16bit[1];
    subDataBlock16bit[1] = subDataBlock16bit[2];
    subDataBlock16bit[2] = temp;

    subDataBlock16bit[0] = MulMod65537(subDataBlock16bit[0], subKeyStored16bit[displace++]);
    subDataBlock16bit[1] = AddMod65536(subDataBlock16bit[1], subKeyStored16bit[displace++]);
    subDataBlock16bit[2] = AddMod65536(subDataBlock16bit[2], subKeyStored16bit[displace++]);
    subDataBlock16bit[3] = MulMod65537(subDataBlock16bit[3], subKeyStored16bit[displace]);
}    

int IDEACipherSystem::AddMod65536(int added, int add)
{
    return (added + add) % MOD_ADD_2_16;
}
int IDEACipherSystem::AddMod2(int added, int add)
{
    return added ^ add;
}

int IDEACipherSystem::MulMod65537(int muled, int mul)
{
    if (muled == 65536 && mul == 65536) return 1;
    unsigned int temp = (unsigned int(muled) * unsigned int(mul));
    return  temp % MOD_MUL_2_16_1;
}


void IDEACipherSystem::SetSubDataBlock(const binary_stream& textBinaryStream64bit)
{
    binary_stream tempBinaryStream16bit;
    int displace = 0;
    int i;
    for (i=0; i<4; i++)
    {
        tempBinaryStream16bit.clear();
        int j;
        for (j=0; j<16; j++) tempBinaryStream16bit.push_back(textBinaryStream64bit[displace++]);
        subDataBlock16bit[i] = BinaryStreamToInt(tempBinaryStream16bit);
    }
    return;
}

void IDEACipherSystem::SubKeyCreation(const binary_stream& keyBinaryStream128bit)
{
    binary_stream tempBinaryStream128bit = keyBinaryStream128bit;
    binary_stream tempKeyStream16bit;
    int i;
    int displace = 0;
    for (i=0; i<52; i++)
    {
        tempKeyStream16bit.clear();
        if (i % 8 == 0 && i != 0)
        {
            int j;
            for (j=0; j<25; j++)
            {    
                tempBinaryStream128bit.push_back(tempBinaryStream128bit.front());
                tempBinaryStream128bit.pop_front();
            }
            displace = 0;
        }
        int j;
        for (j=0; j<16; j++) tempKeyStream16bit.push_back(tempBinaryStream128bit[displace++]);

        subKeyStored16bit[i] = BinaryStreamToInt(tempKeyStream16bit);
    }
    return;
}

void IDEACipherSystem::IntToBinaryStream(int result, binary_stream& binaryStream16bit)
{
    binaryStream16bit.clear();

    int i;
    for (i=0; i<16; i++)
    {
        if (result & (0x8000 >> i)) binaryStream16bit.push_back(true);
        else binaryStream16bit.push_back(false);
    }
    return;
}

int IDEACipherSystem::BinaryStreamToInt(const binary_stream& binaryStream16bit)
{
    int result = 0;
    int power = 1;
    int i;
    for (i=15; i>=0; i--)
    {
        result += (int)binaryStream16bit[i] * power;
        power *= 2;
    }
    return result;
}

28 楼

先到这里吧
剩下的是RSA 了

正在教^_^

29 楼

强烈感谢楼主大无畏的分享精神!!


(同志不若,把文件打包传上来吧!)

30 楼

可以啊,不过是vc.net的

有界面

怎么传?

我来回复

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