下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
#include
#include
#include
#define ENCRYPT 1
#define DECRYPT 0
static void printHex ( char *cmd,int len );
static void printArray ( const char *In,int len );
static void F_func ( bool In[32],const bool Ki[48] ); // f函数
static void S_func ( bool Out[32],const bool In[48] ); // S盒代替
static void Transform ( bool *Out,bool *In,const char *Table,int len ); // 变换
static void Xor ( bool *InA,const bool *InB,int len ); // 异或
static void RotateL ( bool *In,int len,int loop ); // 循环左移
static void ByteToBit ( bool *Out,const char *In,int bits ); // 字节组转换成位组
static void BitToByte ( char *Out,const bool *In,int bits ); // 位组转换成字节组
// 16位子密钥
static bool SubKey[16][48];
// 64位经过PC1转换为56位 (PC-1)
const static char PC1_Table[56] =
{
57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,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 static char LOOP_Table[16] =
{
1,1
};
// 排列选择 2 (PC-2)
const static char PC2_Table[48] =
{
14,24,4,8,16,40,48,56,32
};
// Ri_1(32位)经过变换E后膨胀为48位 (E) void F_func
static const char E_Table[48] =
{
32,32,1
};
// 8个4比特合并为32比特的排列 P
const static char P_Table[32] =
{
16,};
// 经过S盒 S-Boxes
const static char S_Box[8][