📄 desmode.c
字号:
#include "des.h"
/******************************************************************************/
/* des_cbc_encrypt */
/* This is the Data Encryption Standard (DES) routine which encrypts */
/* a data array of arbitrary length in the Cipher Block Chaining */
/* (CBC) mode. It assumes that the input array has a length */
/* divisible by 8 bytes. If this data array does not have a length */
/* divisible by 8 bytes, it should be zero padded to prevent */
/* overflow. The output array should be similarly padded. */
/* */
/* The initialization vector, "ivec" is overwritten with the last */
/* 8 bytes of encrypted output. This new "ivec" may be used as the */
/* initialization vector for the next block of data. */
/******************************************************************************/
void des_cbc_encrypt(
des_cblock (*input),
des_cblock (*ivec),
des_key_schedule ks,
des_cblock (*output),
int length)
{
register DES_LONG tout0,tout1;
register DES_LONG *inint, *outint;
DES_LONG *iv;
register int l=length;
inint = (DES_LONG *)input;
outint = (DES_LONG *)output;
iv=(DES_LONG *)ivec;
tout0 = *iv++;
tout1 = *iv++;
for (; l>0; l-=8){
*(outint ) = (*inint++) ^ tout0;
*(outint +1) = (*inint++) ^ tout1;
des_encrypt_block((DES_LONG *)outint,ks);
tout0=*outint++;
tout1=*outint++;
}
iv=(DES_LONG *)ivec;
*iv++ = tout0;
*iv++ = tout1;
}
/******************************************************************************/
/* des_cbc_decrypt */
/* This is the Data Encryption Standard (DES) routine which decrypts */
/* a data array of arbitrary length in the Cipher Block Chaining mode*/
/* (CBC). It assumes that the input array has a length divisible by */
/* 8 bytes. If this data array does not have a length divisible by */
/* 8 bytes, it should be zero padded to prevent overflow. The */
/* output array should be similarly padded. The variable "length" */
/* does not need to be divisible by 8 as long as the input and */
/* output arrays are properly padded. */
/* */
/* The initialization vector, "ivec" is overwritten with the last */
/* 8 bytes of de-encrypted output. This new "ivec" may be used as */
/* the initialization vector for the next block of data. */
/* */
/* The key schedule provided to this function should be the same key */
/* schedule that is provided to des_cbc_encrypt. The function */
/* reorganizes the key schedule to decrypt using the block */
/* encryption function. (It is a property of the DES algorithm that */
/* the core encryption routine is also used to decrypt, but with a */
/* modification to the key schedule.) */
/******************************************************************************/
void des_cbc_decrypt(
des_cblock (*input),
des_cblock (*ivec),
des_key_schedule ks,
des_cblock (*output),
int length)
{
register DES_LONG xor0,xor1;
register DES_LONG t0,t1;
register DES_LONG *inint, *outint;
DES_LONG *iv;
register int l=length,i;
des_key_schedule ks_decrypt;
/* Generate key schedule to decrypt using DES_encrypt */
for(i=0;i<32;i+=2){
*(((DES_LONG *) ks_decrypt)+i ) = *(((DES_LONG *) ks)+30-i);
*(((DES_LONG *) ks_decrypt)+i+1) = *(((DES_LONG *) ks)+31-i);
}
inint=(DES_LONG *)input;
outint=(DES_LONG *)output;
iv=(DES_LONG *)ivec;
xor0 = *iv++;
xor1 = *iv++;
for (; l>0; l-=8){
*(outint ) = t0 = (*inint++);
*(outint +1) = t1 = (*inint++);
des_encrypt_block((DES_LONG *)outint,ks_decrypt);
*outint++ ^= xor0;
*outint++ ^= xor1;
xor0 = t0;
xor1 = t1;
}
iv=(DES_LONG *)ivec;
*iv++ = t0;
*iv++ = t1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -