⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hsj.c

📁 这是一个des的实现
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdio.h>/* DES VERSION 0.4 CREATED BY SIMPLE SOURCE AT 2007.3 *//* COMPETENCE : * P4 3.0G 512M * 3000000 TIMES ENCRYPTION WITH CREATED SUBKEYS * 26.784 SECONDS (ABOUT 0.85M/S)*/static __int64 g_arrayMask[64] = {    0x0000000000000001, 0x0000000000000002, 0x0000000000000004, 0x0000000000000008,    0x0000000000000010, 0x0000000000000020, 0x0000000000000040, 0x0000000000000080,    0x0000000000000100, 0x0000000000000200, 0x0000000000000400, 0x0000000000000800,    0x0000000000001000, 0x0000000000002000, 0x0000000000004000, 0x0000000000008000,    0x0000000000010000, 0x0000000000020000, 0x0000000000040000, 0x0000000000080000,    0x0000000000100000, 0x0000000000200000, 0x0000000000400000, 0x0000000000800000,    0x0000000001000000, 0x0000000002000000, 0x0000000004000000, 0x0000000008000000,    0x0000000010000000, 0x0000000020000000, 0x0000000040000000, 0x0000000080000000,    0x0000000100000000, 0x0000000200000000, 0x0000000400000000, 0x0000000800000000,    0x0000001000000000, 0x0000002000000000, 0x0000004000000000, 0x0000008000000000,    0x0000010000000000, 0x0000020000000000, 0x0000040000000000, 0x0000080000000000,    0x0000100000000000, 0x0000200000000000, 0x0000400000000000, 0x0000800000000000,    0x0001000000000000, 0x0002000000000000, 0x0004000000000000, 0x0008000000000000,    0x0010000000000000, 0x0020000000000000, 0x0040000000000000, 0x0080000000000000,    0x0100000000000000, 0x0200000000000000, 0x0400000000000000, 0x0800000000000000,    0x1000000000000000, 0x2000000000000000, 0x4000000000000000, 0x8000000000000000};static int g_arrayIP[64] = {    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,     56, 48, 40, 32, 24, 16,  8,  0,     58, 50, 42, 34, 26, 18, 10,  2,     60, 52, 44, 36, 28, 20, 12,  4,     62, 54, 46, 38, 30, 22, 14,  6 };static int g_arrayE[64] = {    31,  0,  1,  2,  3,  4, -1, -1,      3,  4,  5,  6,  7,  8, -1, -1,      7,  8,  9, 10, 11, 12, -1, -1,     11, 12, 13, 14, 15, 16, -1, -1,     15, 16, 17, 18, 19, 20, -1, -1,     19, 20, 21, 22, 23, 24, -1, -1,     23, 24, 25, 26, 27, 28, -1, -1,     27, 28, 29, 30, 31, 30, -1, -1};static char g_matrixNSBox[8][64] = {    {        14,  4,  3, 15,  2, 13,  5,  3,        13, 14,  6,  9, 11,  2,  0,  5,         4,  1, 10, 12, 15,  6,  9, 10,         1,  8, 12,  7,  8, 11,  7,  0,         0, 15, 10,  5, 14,  4,  9, 10,         7,  8, 12,  3, 13,  1,  3,  6,        15, 12,  6, 11,  2,  9,  5,  0,         4,  2, 11, 14,  1,  7,  8, 13    },    {        15,  0,  9,  5,  6, 10, 12,  9,         8,  7,  2, 12,  3, 13,  5,  2,         1, 14,  7,  8, 11,  4,  0,  3,        14, 11, 13,  6,  4,  1, 10, 15,         3, 13, 12, 11, 15,  3,  6,  0,         4, 10,  1,  7,  8,  4, 11, 14,        13,  8,  0,  6,  2, 15,  9,  5,         7,  1, 10, 12, 14,  2,  5,  9    },    {        10, 13,  1, 11,  6,  8, 11,  5,         9,  4, 12,  2, 15,  3,  2, 14,         0,  6, 13,  1,  3, 15,  4, 10,        14,  9,  7, 12,  5,  0,  8,  7,        13,  1,  2,  4,  3,  6, 12, 11,         0, 13,  5, 14,  6,  8, 15,  2,         7, 10,  8, 15,  4,  9, 11,  5,         9,  0, 14,  3, 10,  7,  1, 12    },    {         7, 10,  1, 15,  0, 12, 11,  5,        14,  9,  8,  3,  9,  7,  4,  8,        13,  6,  2,  1,  6, 11, 12,  2,         3,  0,  5, 14, 10, 13, 15,  4,        13,  3,  4,  9,  6, 10,  1, 12,        11,  0,  2,  5,  0, 13, 14,  2,         8, 15,  7,  4, 15,  1, 10,  7,         5,  6, 12, 11,  3,  8,  9, 14    },    {         2,  4,  8, 15,  7, 10, 13,  6,         4,  1,  3, 12, 11,  7, 14,  0,        12,  2,  5,  9, 10, 13,  0,  3,         1, 11, 15,  5,  6,  8,  9, 14,        14, 11,  5,  6,  4,  1,  3, 10,         2, 12, 15,  0, 13,  2,  8,  5,        11,  8,  0, 15,  7, 14,  9,  4,        12,  7, 10,  9,  1, 13,  6,  3    },    {        12,  9,  0,  7,  9,  2, 14,  1,        10, 15,  3,  4,  6, 12,  5, 11,         1, 14, 13,  0,  2,  8,  7, 13,        15,  5,  4, 10,  8,  3, 11,  6,        10,  4,  6, 11,  7,  9,  0,  6,         4,  2, 13,  1,  9, 15,  3,  8,        15,  3,  1, 14, 12,  5, 11,  0,         2, 12, 14,  7,  5, 10,  8, 13    },    {         4,  1,  3, 10, 15, 12,  5,  0,         2, 11,  9,  6,  8,  7,  6,  9,        11,  4, 12, 15,  0,  3, 10,  5,        14, 13,  7,  8, 13, 14,  1,  2,        13,  6, 14,  9,  4,  1,  2, 14,        11, 13,  5,  0,  1, 10,  8,  3,         0, 11,  3,  5,  9,  4, 15,  2,         7,  8, 12, 15, 10,  7,  6, 12    },    {        13,  7, 10,  0,  6,  9,  5, 15,         8,  4,  3, 10, 11, 14, 12,  5,         2, 11,  9,  6, 15, 12,  0,  3,         4,  1, 14, 13,  1,  2,  7,  8,         1,  2, 12, 15, 10,  4,  0,  3,        13, 14,  6,  9,  7,  8,  9,  6,        15,  1,  5, 12,  3, 10, 14,  5,         8,  7, 11,  0,  4, 13,  2, 11    },};static int g_arrayP[32] = {    15,  6, 19, 20, 28, 11, 27, 16,      0, 14, 22, 25,  4, 17, 30,  9,      1,  7, 23, 13, 31, 26,  2,  8,     18, 12, 29,  5, 21, 10,  3, 24};static int g_arrayIP_1[64] = {    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,     32,  0, 40,  8, 48, 16, 56, 24};static int g_arrayPC_1[56] = {    56, 48, 40, 32, 24, 16,  8,     0, 57, 49, 41, 33, 25, 17,     9,  1, 58, 50, 42, 34, 26,    18, 10,  2, 59, 51, 43, 35,    62, 54, 46, 38, 30, 22, 14,     6, 61, 53, 45, 37, 29, 21,    13,  5, 60, 52, 44, 36, 28,    20, 12,  4, 27, 19, 11,  3};static int g_arrayPC_2[64] = {    13, 16, 10, 23,  0,  4, -1, -1,     2, 27, 14,  5, 20,  9, -1, -1,    22, 18, 11,  3, 25,  7, -1, -1,    15,  6, 26, 19, 12,  1, -1, -1,    40, 51, 30, 36, 46, 54, -1, -1,    29, 39, 50, 44, 32, 47, -1, -1,    43, 48, 38, 55, 33, 52, -1, -1,    45, 41, 49, 35, 28, 31, -1, -1};static int g_arrayLs[16] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};static __int64 g_arrayLsMask[3] = {    0x0000000000000000,    0x0000000000100001,    0x0000000000300003};#define BitTransform(array, len, source, dest) \{\    __int64 bts = source;\    int bti;\    dest = 0;\    for(bti = 0; bti < len; bti++)\    {\        if(array[bti] >= 0 && (bts & g_arrayMask[array[bti]]))\        {\            dest |= g_arrayMask[bti];\        }\    }\}#define DES_MODE_ENCRYPT        0#define DES_MODE_DECRYPT        1void DESSubKeys(__int64 key, __int64 K[16], int mode);__int64 DES64(__int64 subkeys[16], __int64 data); void DESSubKeys(__int64 key, __int64 K[16], int mode){    __int64 temp;    int j;    /* PC-1变换 */    BitTransform(g_arrayPC_1, 56, key, temp);    for(j = 0; j < 16; j++)    {        /* 循环左移 */        {            __int64 source = temp;            temp = ((source & g_arrayLsMask[g_arrayLs[j]]) << (28 - g_arrayLs[j])) | ((source & ~g_arrayLsMask[g_arrayLs[j]]) >> g_arrayLs[j]);        }        /* PC-2变换 */        BitTransform(g_arrayPC_2, 64, temp, K[j]);    }    if(mode == DES_MODE_DECRYPT) /* 如果解密则反转子密钥顺序 */    {        __int64 t;        for(j = 0; j < 8; j++)        {            t = K[j];            K[j] = K[15 - j];            K[15 - j] = t;        }    }}__int64 DES64(__int64 subkeys[16], __int64 data){    static __int64 out;    static __int64 source;    static __int64 L, R;    static __int32 * pSource;    static char * pR;    static int i;    static __int32 SOut;    static __int32 t;    static int sbi;    pSource = (__int32 *)&out;    pR = (char *)&R;    /* IP变换 */    {        BitTransform(g_arrayIP, 64, data, out);    }    /* 主迭代 */    {        //source = out;        for(i = 0; i < 16; i++)        {            R = pSource[1];            /* F变换开始 */            {                /* E变换 */                {                    BitTransform(g_arrayE, 64, R, R);                }                /* 与子密钥异或 */                R ^= subkeys[i];                /* S盒变换 */                {                    SOut = 0;                    for(sbi = 7; sbi >= 0; sbi--)                    {                        SOut <<= 4;                        SOut |= g_matrixNSBox[sbi][pR[sbi]];                    }                    R = SOut;                }                /* P变换 */                {                    BitTransform(g_arrayP, 32, R, R);                }            }            /* f变换完成 */            L = pSource[0];            pSource[0] = pSource[1];            pSource[1] = (__int32)(L ^ R);        }        /* 交换高低32位 */        {            t = pSource[0];            pSource[0] = pSource[1];            pSource[1] = t;        }    }    /* IP-1变换 */    {        BitTransform(g_arrayIP_1, 64, out, out);    }    return out;}老早就写的了.只是现在看着BLOG上的东西这么少,心里实在是不怎么爽.也就随便复制+粘贴一下了.呵呵.这次贴个实用点的,DES密码算法.该密码算法应该说还是比较安全的.作为一般的个人使用相信没有什么大的问题. 只是由于对C语言以及算法的理解有限,该DES算法程序实现的加密解密效率较低,加密文本文件尚感觉不到,如果用来加密多媒体文件等较大的文件,就会觉得像老牛拉车了. DES密码算法是一种块加密算法,也就是一次加密一定大小的块.标准DES一次加密64个bit,也就是8个字节.我实现的时候是使用unsigned char [8]这样的数组作为缓冲区,也即数组中的每一个bit都用到了,如果用unsigned char [64]这样的数组作为缓冲取,也就是数组中的每一个元素只代表一个 bit.这样会造成内存的浪费. 在做的时候还遇到了另外一个问题,因为DES是块加密算法,所以如果一个文件的大小不能整除8字节的时候,就会无法加密解密剩下的几个不足8字节的文件,当时考虑的方法是:计算文件大小能否被8byte整除,无论能与不能,都为其添加数个字节使其能被8整除,然后多加一个byte的数据用来保存刚才添加字节的数量.比如:文件为9byte,程序就为其添加7byte,然后再添加1byte,最后这1byte的值为7. 运行参数为:程序名 希望加密的文件路径 保存加密后的文件路径 密钥 E/D(E为加密,D为解密) #include <stdio.h> #include <string.h> //此DES算法为CBC--密码分组链接模式 //此DES算法的加解密效率约为56K每秒。 int Crypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv); int Decrypt(char *sourcepath,char *destpath,char *inputkey,char *inputiv); int is_InputKeyRight(char *inputkey); int Check(int argc,char **argv); int is_Sourcepathright(char *source_path); int is_Destpathright(char *dest_path); void IP(unsigned char *output,unsigned char *input); void IP1(unsigned char *output,unsigned char *input); unsigned char getbit(int n,unsigned char ch); int EP(unsigned char *EPplaintext,unsigned char *plaintext); int F(unsigned char *r,unsigned char *k,unsigned char *output); void P(unsigned char *output,unsigned char *input); void FK(unsigned char *l,unsigned char *r,unsigned char *sk,unsigned char *output); int PC1(unsigned char *cd,unsigned char *input); int PC2(unsigned char *output,unsigned char *input); int LS1(unsigned char *input); void GenerateSubkey(char sk[16][6],char *inputkey); void des(unsigned char *output,unsigned char *plaintext,unsigned char sk[16][6],int flag); int main(int argc,char **argv) { if (Check(argc,argv)==0){ return -1; } if (argv[5][0]=='e'||argv[5][0]=='E'){ if(Crypt(argv[1],argv[2],argv[3],argv[4])) return 1; else return -1; } if (argv[5][0]=='d'||argv[5][0]=='D'){ if(Decrypt(argv[1],argv[2],argv[3],argv[4])) return 1; else return -1; } return -1; } unsigned char getbit(int n,unsigned char ch)//0-8均可作为n的传递值 { if(n==0) n=8; if(((ch>>(8-n))&1)==1) return 1; else return 0; } void IP(unsigned char *output,unsigned char *input)//output[8] { unsigned char IPmap[]={ 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 }; int i=0; int j=0; int k=7; for(i=0;i<8;i++){ for(j=0;j<8;j++){ output[i]=output[i]+getbit(IPmap[j+i*8]%8,input[(IPmap[j+i*8]-1)/8]); if(k>0) output[i]=output[i]<<1; k--; } k=7; } } void IP1(unsigned char *output,unsigned char *input) { unsigned char IP1map[]={ 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 }; int i; int j; int k=7; for(i=0;i<8;i++){ for(j=0;j<8;j++){ output[i]=output[i]+getbit(IP1map[j+i*8]%8,input[(IP1map[j+i*8]-1)/8]); if(k>0) output[i]=output[i]<<1; k--; } k=7; } } int EP(unsigned char *EPplaintext,unsigned char *plaintext) { unsigned char EPmap[]={ 32,1,2,3,4,5, 4,5,6,7,8,9, 8,9,10,11,12,13, 12,13,14,15,16,17, 16,17,18,19,20,21, 20,21,22,23,24,25, 24,25,26,27,28,29, 28,29,30,31,32,1 }; int i; int j; int k=7; for(i=0;i<6;i++){ for(j=0;j<8;j++){ EPplaintext[i]=EPplaintext[i]+getbit(EPmap[j+i*8]%8,plaintext[(EPmap[j+i*8]-1)/8]); if(k>0) EPplaintext[i]=EPplaintext[i]<<1; k--; } k=7; } return 1; } int F(unsigned char *r,unsigned char *k,unsigned char *output) { unsigned char s1[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} }; unsigned char s2[4][16]={ {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} }; unsigned char s3[4][16]={ {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} }; unsigned char s4[4][16]={ {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} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -