微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 基于单片机的DES加密解密算法C源码

基于单片机的DES加密解密算法C源码

时间:11-29 来源:互联网 点击:

void rotate_l(uint8_t *key)
{
uint8_t str_x[8];
uint8_t i;

for (i=0; i < 8; ++i) str_x[i] = key[i];
for (i=0; i < 7; ++i)
{
key[i] = (key[i] < 1);
if ((i < 6) && ((str_x[i+1] & 128) == 128))
key[i] |= 1;
}
if (str_x[0] & 0x80 )
key[3] |= 0x10;
else
key[3] &= ~0x10;
if (str_x[3] & 0x08 )
key[6] |= 0x01;
else
key[6] &= ~0x01;
}

/************************************************************************/
/* */
/* Module title: compute_subkeys */
/* Module type: des subrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: Computes the 16 sub keys for use in the */
/* DES algorithm */
/* */
/* input parameter 1: pointer to first byte in key string */
/* output : fills the array sub_keys[16][8] with */
/* sub keys and stores the input key in */
/* main_key[8] */
/************************************************************************/
void compute_subkeys(uint8_t *key)
{
uint8_t i, j, ikey[8], okey[8];

for (i=0; i < 8; ++i)
{
main_key[i] = key[i];
}

transpose(key, ikey, key_tr1, 56);

for (i=0; i < 16; ++i)
{
for (j=0; j < rots[i]; ++j) {rotate_l(ikey);}
transpose(ikey, okey, key_tr2, 64);
for (j=0; j < 8; ++j)
{sub_keys[i][j] = okey[j];}
}

}

/************************************************************************/
/* */
/* Module title: f */
/* Module type: des subrutine */
/* */
/* Author: YXH */
/* Date: 2012-07-13 */
/* */
/* Last changed by: YXH */
/* Date: 2012-07-13 */
/* */
/* Functional Description: The chipher function */
/* */
/* input parameter 1: pointer to first byte in key string */
/* 2: pointer to a 32 bit input string */
/* 3: pointer to a 32 bit output string */
/************************************************************************/
void f(uint8_t *skey, uint8_t *a_str, uint8_t *x_str)
{
uint8_t e_str[8], y_str[8], z_str[8];
uint8_t k;

transpose(a_str, e_str, etr, 64);
for (k=0; k < 8; ++k)
{
y_str[k] = (e_str[k] ^ skey[k]) & 63;
z_str[k] = s[k] [y_str[k]];
}
transpose(z_str, x_str, ptr, 32);
}

//源码完毕

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top