码迷,mamicode.com
首页 > 其他好文 > 详细

AES源码

时间:2016-05-04 01:07:26      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

#define MESSAGESHOW(X) MessageBox(NULL,L##X,L"",0)
#define SECTION_SIZE 16
#define GET_BYTE_HIGH(x)  ((x & 0xf0)>>4)
#define GET_BYTE_LOW(x)( x & 0x0f)

#define xtime(x)   ((x<<1) ^ (((x>>7) & 1) * 0x1b))
#define mul(x,y) (((y & 1) * x) ^ ((y>>1 & 1) * xtime(x)) ^ ((y>>2 & 1) * xtime(xtime(x))) ^ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x))))))


BYTE sBox[16][16] =
{ /*  0    1    2    3    4    5    6    7    8    9    a    b    c    d    e    f */
    0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, /*0*/
    0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, /*1*/
    0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, /*2*/
    0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, /*3*/
    0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, /*4*/
    0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, /*5*/
    0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, /*6*/
    0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, /*7*/
    0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, /*8*/
    0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, /*9*/
    0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, /*a*/
    0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, /*b*/
    0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, /*c*/
    0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, /*d*/
    0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, /*e*/
    0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16  /*f*/
};
BYTE invsBox[16][16] =
{ /*  0    1    2    3    4    5    6    7    8    9    a    b    c    d    e    f  */
    0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, /*0*/
    0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, /*1*/
    0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, /*2*/
    0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, /*3*/
    0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, /*4*/
    0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, /*5*/
    0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, /*6*/
    0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, /*7*/
    0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, /*8*/
    0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, /*9*/
    0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, /*a*/
    0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, /*b*/
    0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, /*c*/
    0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, /*d*/
    0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, /*e*/
    0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d  /*f*/
};
BYTE mix[4][4] = {  2, 3, 1, 1,
                    1, 2, 3, 1,
                    1, 1, 2, 3,
                    3, 1, 1, 2
};
BYTE rmix[4][4] = { 0x0e, 0x0b, 0x0d, 0x09,
                    0x09, 0x0e, 0x0b, 0x0d,
                    0x0d, 0x09, 0x0e, 0x0b,
                    0x0b, 0x0d, 0x09, 0x0e,
};
BYTE RC[] = { 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 };
PVOID GetFileMapping(PHANDLE hFile, PHANDLE hMap, LPWSTR FileName);
// AES.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"
#include "Header.h"


class  AES
{
private:
    BYTE SubKey[11][16];
    BYTE Key[4][4];
    BYTE PlainTextChanged[4][4];
    void BoxChange(PBYTE pByte, BYTE box[16][16])
    {
        *pByte = box[GET_BYTE_HIGH(*pByte)][GET_BYTE_LOW(*pByte)];
    }
    void KeyExtension()
    {
        memcpy(SubKey[0], Key, 16);
        for (int i = 1; i < 11; i++)
        {
            PBYTE temp1 = SubKey[i - 1] + 12;
            g_function(temp1, (i - 1) * 4 - 1);
            for (int j = 0; j < 16; j += 4)
            {
                BYTE temp2[4];
                memcpy(temp2, SubKey[i - 1] + j, 4);
                if (j == 0)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        temp2[k] = temp2[k] ^ temp1[k];
                    }
                }
                else
                {
                    for (int k = 0; k < 4; k++)
                    {
                        temp2[k] = temp2[k] ^ SubKey[i][j - 4 + k];
                    }
                }
                memcpy(SubKey[i] + j, temp2, 4);
            }
        }
    }
    void g_function(PBYTE pINByte, int jValue)
    {
        BYTE result[4];
        result[0] = pINByte[1];
        result[1] = pINByte[2];
        result[2] = pINByte[3];
        result[3] = pINByte[0];
        for (int i = 0; i < 3; i++)
            BoxChange(result + i, sBox);
        BYTE RCJ[4] = { RC[jValue / 4], 0, 0, 0 };
        for (int i = 0; i < 3; i++)
            result[i] = result[i] ^ RCJ[i];
        memcpy(pINByte, result, 4);
        return;
    }
    BYTE FFmul(BYTE a,BYTE b)
    {
        unsigned char bw[4];
        unsigned char res = 0;
        int i;
        bw[0] = b;
        for (i = 1; i<4; i++)
        {
            bw[i] = bw[i - 1] << 1;
            if (bw[i - 1] & 0x80)
            {
                bw[i] ^= 0x1b;
            }
        }
        for (i = 0; i<4; i++)
        {
            if ((a >> i) & 0x01)
            {
                res ^= bw[i];
            }
        }
        return res;
    }
    //----------------------------------------------------------------
    void xorText(BYTE text1[4][4], BYTE text2[16])
    {
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                text1[j][i] = text1[j][i] ^ *(text2 + i + j);
            }
        }
    }
    void shiftRow(BYTE Text[4][4])
    {
        BYTE temp[4][4];
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                temp[i][j] = Text[i][(i + j) % 4];
            }
        }
        memcpy(Text, temp, 16);
    }
    void RshiftRow(BYTE Text[4][4])
    {
        BYTE temp[4][4];
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                temp[i][j] = Text[i][(4 + j - i) % 4];
            }
        }
        memcpy(Text, temp, 16);
    }
    void mixCol(BYTE st[4][4])
    {
        for (int j =  0; j < 4; j++)
        {
            BYTE temp[4] = { 0 };
            
            temp[0] = FFmul(0x2, st[0][j]) ^ FFmul(0x3, st[1][j]) ^ FFmul(0x1, st[2][j]) ^ FFmul(0x1, st[3][j]);
            temp[1] = FFmul(0x1, st[0][j]) ^ FFmul(0x2, st[1][j]) ^ FFmul(0x3, st[2][j]) ^ FFmul(0x1, st[3][j]);
            temp[2] = FFmul(0x1, st[0][j]) ^ FFmul(0x1, st[1][j]) ^ FFmul(0x2, st[2][j]) ^ FFmul(0x3, st[3][j]);
            temp[3] = FFmul(0x3, st[0][j]) ^ FFmul(0x1, st[1][j]) ^ FFmul(0x1, st[2][j]) ^ FFmul(0x2, st[3][j]);
            st[0][j] = temp[0];
            st[1][j] = temp[1];
            st[2][j] = temp[2];
            st[3][j] = temp[3];
        }
    }
        void RmixCol(BYTE st[4][4])
        {
            for (int j = 0; j < 4; j++)
            {
                BYTE tmp[4] = { 0 };
                tmp[0] = FFmul(0x0e, st[0][j]) ^ FFmul(0x0b, st[1][j]) ^ FFmul(0x0d, st[2][j]) ^ FFmul(0x09, st[3][j]);
                tmp[1] = FFmul(0x09, st[0][j]) ^ FFmul(0x0e, st[1][j]) ^ FFmul(0x0b, st[2][j]) ^ FFmul(0x0d, st[3][j]);
                tmp[2] = FFmul(0x0d, st[0][j]) ^ FFmul(0x09, st[1][j]) ^ FFmul(0x0e, st[2][j]) ^ FFmul(0x0b, st[3][j]);
                tmp[3] = FFmul(0x0b, st[0][j]) ^ FFmul(0x0d, st[1][j]) ^ FFmul(0x09, st[2][j]) ^ FFmul(0x0e, st[3][j]);
                st[0][j] = tmp[0];
                st[1][j] = tmp[1];
                st[2][j] = tmp[2];
                st[3][j] = tmp[3];
            }
        }
        void ChangePlainText(PBYTE pPlainText)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    PlainTextChanged[j][i] = *(pPlainText + i*4 + j);
                }
            }
        }
        void ReChangePlainText(PBYTE pPlainText)
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    *(pPlainText + i*4 + j) = PlainTextChanged[j][i];
                }
            }
        }
    public:
        AES(PBYTE pKey)
        {
            memcpy(this->Key, pKey, 16);
            KeyExtension();
        }
        void DeCrypt(PBYTE PlainText)
        {
            ChangePlainText(PlainText);
            for (int i = 10; i >=0; i--)
            {
                if (i == 10)
                {
                    xorText(PlainTextChanged, SubKey[i]);
                    RshiftRow(PlainTextChanged);
                    for (int j = 0; j < 16; j++)
                    {
                        BoxChange(PlainTextChanged[0], invsBox);
                    }
                }
                
                else if (i > 0 && i < 10)
                {
                    
                    xorText(PlainTextChanged, SubKey[i]);
                    RmixCol(PlainTextChanged);
                    RshiftRow(PlainTextChanged);
                    for (int j = 0; j < 16; j++)
                    {
                        BoxChange(PlainTextChanged[0], invsBox);
                    }
                    
                }
                
                else
                {
                    xorText(PlainTextChanged, SubKey[i]);
                }
                
            }
            ReChangePlainText(PlainText);
        }
        void EnCrypt(PBYTE PlainText)
        {
            ChangePlainText(PlainText);
            for (int i = 0; i < 11; i++)
            {
                
                if (i == 0)
                {
                    xorText(PlainTextChanged, SubKey[i]);
                }
                
                else if (i >= 1 && i <= 9)
                {
                    
                    for (int j = 0; j < 16; j++)
                    {
                        BoxChange(PlainTextChanged[0], sBox);
                    }
                    shiftRow(PlainTextChanged);
                    mixCol(PlainTextChanged);
                    xorText(PlainTextChanged, SubKey[i]);
                }
                
                else
                {
                    for (int j = 0; j < 16; j++)
                    {
                        BoxChange(PlainTextChanged[0], sBox);
                    }
                    shiftRow(PlainTextChanged);
                    xorText(PlainTextChanged, SubKey[i]);
                }

            }
            ReChangePlainText(PlainText);
        }
        ~AES()
        {
        }

    };


    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {

        TCHAR szBuffer[MAX_PATH] = { 0 };
        OPENFILENAME ofn = { 0 };
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = NULL;
        ofn.lpstrFilter = _T("TXT文件(*.txt)\0*.txt\0所有文件(*.*)\0*.*\0");//要选择的文件后缀   
        ofn.lpstrInitialDir = _T("D:\\");//默认的文件路径   
        ofn.lpstrFile = szBuffer;//存放文件的缓冲区   
        ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
        ofn.nFilterIndex = 0;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT  
        BOOL bSel = GetOpenFileName(&ofn);
        //MESSAGESHOW("AAA");
        if (!bSel)
        {
            MESSAGESHOW("open file?");
            return 0;
        }
        //---------------------------------
        BYTE Key[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
        HANDLE hFile, hMap;
        PVOID fileAddress = GetFileMapping(&hFile, &hMap, szBuffer);
        AES Aes(Key);
        int re = MessageBoxA(NULL, "encrypt or decrypt(false for decrypt)", "", MB_YESNOCANCEL);
        if (re ==IDYES)
        {
            for (DWORD i = 0; i < GetFileSize(hFile, 0)/SECTION_SIZE; i ++)
            {
                Aes.EnCrypt(((PBYTE)fileAddress) + i*SECTION_SIZE);
            }
            MESSAGESHOW("OK!the text has been encrypt");
        }
        else if (re == IDNO)
        {
            for (DWORD i = 0; i < GetFileSize(hFile, 0) / SECTION_SIZE; i ++)
            {
                Aes.DeCrypt(((PBYTE)fileAddress) + i*SECTION_SIZE);
            }
            MESSAGESHOW("OK!the text has been decrypt");
        }
        else
        {
            MESSAGESHOW("nothing done");
        }
        UnmapViewOfFile(fileAddress);
        CloseHandle(hMap);
        CloseHandle(hFile);
        return 0;
    }
    PVOID GetFileMapping(PHANDLE hFile, PHANDLE hMap, LPWSTR FileName)
    {
        *hFile = CreateFile(FileName, GENERIC_ALL, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
        if (*hFile == INVALID_HANDLE_VALUE)
        {
            MESSAGESHOW("can not create file");
            exit(0);
        }
        *hMap = CreateFileMapping(*hFile, 0, PAGE_READWRITE, 0, 0, 0);
        if (*hMap == INVALID_HANDLE_VALUE)
        {
            MESSAGESHOW("can not create filemapping");
            exit(0);
        }
        PVOID fileOffset = MapViewOfFile(*hMap, FILE_MAP_ALL_ACCESS, 0, 0, GetFileSize(*hFile, 0));
        return fileOffset;
    }

当然了代码看多了,会想来点福利什么的~~那么接下来

 

技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享技术分享

 

我觉得她的眼睛里,有整个世界。

 

AES源码

标签:

原文地址:http://www.cnblogs.com/distanceblog/p/5456922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!