• 大小: 41KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: C/C++
  • 标签: SMS4  

资源简介

SMS4算法原理与C语言实现 运行环境:WINDOWS下VC6.0及以上编程工具 运行方式:(1)WINDOWS下VC6.0及以上编程工具编译链接运行 (2)工程文件夹下Debug下的*.exe

资源截图

代码片段和文件信息

#include 
#include 
//#include 
//#define LITTLE_ENDIAN     //定义小端字节序
//#define BIG_ENDIAN     //定义大端字节序
#define ENCRYPT  0     //定义加密标志
#define DECRYPT  1     //定义解密标志

typedef unsigned char muint8;
typedef unsigned int muint32;
const muint8 Sbox[256] = {
0xd60x900xe90xfe0xcc0xe10x3d0xb70x160xb60x140xc20x280xfb0x2c0x05
0x2b0x670x9a0x760x2a0xbe0x040xc30xaa0x440x130x260x490x860x060x99
0x9c0x420x500xf40x910xef0x980x7a0x330x540x0b0x430xed0xcf0xac0x62
0xe40xb30x1c0xa90xc90x080xe80x950x800xdf0x940xfa0x750x8f0x3f0xa6
0x470x070xa70xfc0xf30x730x170xba0x830x590x3c0x190xe60x850x4f0xa8
0x680x6b0x810xb20x710x640xda0x8b0xf80xeb0x0f0x4b0x700x560x9d0x35
0x1e0x240x0e0x5e0x630x580xd10xa20x250x220x7c0x3b0x010x210x780x87
0xd40x000x460x570x9f0xd30x270x520x4c0x360x020xe70xa00xc40xc80x9e
0xea0xbf0x8a0xd20x400xc70x380xb50xa30xf70xf20xce0xf90x610x150xa1
0xe00xae0x5d0xa40x9b0x340x1a0x550xad0x930x320x300xf50x8c0xb10xe3
0x1d0xf60xe20x2e0x820x660xca0x600xc00x290x230xab0x0d0x530x4e0x6f
0xd50xdb0x370x450xde0xfd0x8e0x2f0x030xff0x6a0x720x6d0x6c0x5b0x51
0x8d0x1b0xaf0x920xbb0xdd0xbc0x7f0x110xd90x5c0x410x1f0x100x5a0xd8
0x0a0xc10x310x880xa50xcd0x7b0xbd0x2d0x740xd00x120xb80xe50xb40xb0
0x890x690x970x4a0x0c0x960x770x7e0x650xb90xf10x090xc50x6e0xc60x84
0x180xf00x7d0xec0x3a0xdc0x4d0x200x790xee0x5f0x3e0xd70xcb0x390x48
};

const muint32 CK[32] = {
0x00070e15 0x1c232a31 0x383f464d 0x545b6269
0x70777e85 0x8c939aa1 0xa8afb6bd 0xc4cbd2d9
0xe0e7eef5 0xfc030a11 0x181f262d 0x343b4249
0x50575e65 0x6c737a81 0x888f969d 0xa4abb2b9
0xc0c7ced5 0xdce3eaf1 0xf8ff060d 0x141b2229
0x30373e45 0x4c535a61 0x686f767d 0x848b9299
0xa0a7aeb5 0xbcc3cad1 0xd8dfe6ed 0xf4fb0209
0x10171e25 0x2c333a41 0x484f565d 0x646b7279 };

#define Rotl(_x _y) (((_x) << (_y)) | ((_x) >> (32 - (_y))))

#define ByteSub(_A) (Sbox[(_A) >> 24 & 0xFF] << 24 ^ \
                     Sbox[(_A) >> 16 & 0xFF] << 16 ^ \
                     Sbox[(_A) >>  8 & 0xFF] <<  8 ^ \
                     Sbox[(_A) & 0xFF])

#define L1(_B) ((_B) ^ Rotl(_B 2) ^ Rotl(_B 10) ^ Rotl(_B 18) ^ Rotl(_B 24))
#define L2(_B) ((_B) ^ Rotl(_B 13) ^ Rotl(_B 23))

// SMS4的加解密函数
// 参数说明:Input为输入信息分组,Output为输出分组,rk为轮密钥
void SMS4Crypt(muint8 *Input muint8 *Output muint32 *rk)
{
 muint32 r mid x0 x1 x2 x3 *p;
 p = (muint32 *)Input;
 x0 = p[0];
 x1 = p[1];
 x2 = p[2];
 x3 = p[3];
#ifdef LITTLE_ENDIAN
 x0 = Rotl(x0 16); x0 = ((x0 & 0x00FF00FF) << 8) ^ ((x0 & 0xFF00FF00) >> 8);
 x1 = Rotl(x1 16); x1 = ((x1 & 0x00FF00FF) << 8) ^ ((x1 & 0xFF00FF00) >> 8);
 x2 = Rotl(x2 16); x2 = ((x2 & 0x00FF00FF) << 8) ^ ((x2 & 0xFF00FF00) >> 8);
 x3 = Rotl(x3 16); x3 = ((x3 & 0x00FF00F

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1272  2013-10-09 17:08  第四章  SMS4算法原理与实现\README.txt

     文件      49152  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\Release\sms4.exe

     文件       5399  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\Release\sms4.obj

     文件     203724  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\Release\sms4.pch

     文件      33792  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\Release\vc60.idb

     文件       6117  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\sms4.cpp

     文件       4260  2013-10-07 18:07  第四章  SMS4算法原理与实现\SMS4\sms4.dsp

     文件        531  2013-10-07 18:07  第四章  SMS4算法原理与实现\SMS4\sms4.dsw

     文件      50176  2013-10-09 16:17  第四章  SMS4算法原理与实现\SMS4\sms4.ncb

     文件      53760  2013-10-09 16:17  第四章  SMS4算法原理与实现\SMS4\sms4.opt

     文件        861  2013-10-09 16:16  第四章  SMS4算法原理与实现\SMS4\sms4.plg

     目录          0  2014-04-10 11:00  第四章  SMS4算法原理与实现\SMS4\Debug

     目录          0  2014-04-10 11:00  第四章  SMS4算法原理与实现\SMS4\Release

     目录          0  2014-04-10 11:00  第四章  SMS4算法原理与实现\SMS4

     目录          0  2014-04-10 11:00  第四章  SMS4算法原理与实现

----------- ---------  ---------- -----  ----

               409044                    15


评论

共有 条评论

相关资源