用C语言实现DES算法

[11-20 16:14:32]   来源:http://www.88dzw.com  FPGA   阅读:8324

文章摘要:用C语言实现DES算法本DES算法,使用了效率很高的C完成。目前,国内知名企业的POS终端中,单DES算法,均是采用这个函数完成。函数经本站验证过,可以放心使用。#include "stdafx.h"int des(unsigned char *source,unsigned char * dest,unsigned char * inkey, int flg);int _tmain(int argc, _TCHAR* argv[]){unsigned char s[65]="DES算法是这样工作的:如Mode为加密,则用Key 去把数据Data进行加a

用C语言实现DES算法,标签:fpga是什么,fpga教程,http://www.88dzw.com

用C语言实现DES算法

本DES算法,使用了效率很高的C完成。目前,国内知名企业的POS终端中,单DES算法,均是采用这个函数完成。函数经本站验证过,可以放心使用。
#include "stdafx.h"
int des(unsigned char *source,unsigned char * dest,unsigned char * inkey, int flg);
int _tmain(int argc, _TCHAR* argv[])
{

unsigned char s[65]="DES算法是这样工作的:如Mode为加密,则用Key 去把数据Data进行加abc";
unsigned char d[65]={0};
unsigned char k[11]="adsss";
unsigned char m[65]={0};
for(int i =0;i<8;i++)
{
    des(&s[8*i],&d[8*i],k,0);
}
for(int i =0;i<8;i++)
{
    des(&d[8*i],&m[8*i],k,1);
}
//des(s,d,k,0);
//des(d,m,k2,1);
//printf(d);

char i;
scanf("%c",&i);
return 0;
}

/* ============================================================
des()
Description: DES algorithm,do encript or descript.
============================================================ */
int des(unsigned char *source,unsigned char * dest,unsigned char * inkey, int flg)
{
unsigned char bufout[64],
kwork[56], worka[48], kn[48], buffer[64], key[64],
nbrofshift, temp1, temp2;
int valindex;
register i, j, k, iter;
/* INITIALIZE THE TABLES */
/* Table - s1 */
static 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 };
/* Table - s2 */
static 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 };
/* Table - s3 */
static 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 };
/* Table - s4 */
static 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 };
/* Table - s5 */
static unsigned char s5[4][16] = {
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 };
/* Table - s6 */
static unsigned char s6[4][16] = {
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 };
/* Table - s7 */
static unsigned char s7[4][16] = {
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 };

[1] [2] [3] [4] [5]  下一页


Tag:FPGAfpga是什么,fpga教程FPGA

《用C语言实现DES算法》相关文章

分类导航
最新更新
热门排行