openssl关于des算法

des是对称算法,加密的密钥和解码的密钥是相同的,对于des有两种方案,ecb和cbc。值得注意ecb每次只能对一组数据进行加密,即8位的数据进行加密。cbc则对指定的长度的数据进行加密。

借鉴了该博文http://blog.csdn.net/p0x1307/article/details/40115567,本文测算下ecb和cbc的执行时间对比.


cbc:


#include <openssl/des.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>

long get_mesl() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*1000 + tv.tv_usec/1000;
}

long get_trick() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*1000000 + tv.tv_usec;
}

void get_key(DES_cblock *key, unsigned char *keystring) {
   if (keystring) {
      DES_string_to_key(keystring, key);
   } else {
      DES_random_key(key);
   }
}

int main(int argc, char** argv)
{
      unsigned char *keystring = "secrct key text word";
      DES_cblock key;
      DES_key_schedule key_schedule;
      get_key(&key, keystring);

      if (DES_set_key_checked(&key, &key_schedule) != 0) {
          printf("convert to key_schedule failed.\n");
          return -1;
      }

      
      int klen = strlen(key);
      for (int i = 0; i < klen; ++i)
         printf("%02X ", key[i]);
      printf("\n");
      printf("klen:%d\n", klen);


      //需要加密的字符串
      unsigned char input[] = "this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl.this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl. this is a text being encrypted by openssl.";
      printf("slen:%d\n", strlen(input));
      size_t len = (sizeof(input)+7)/8 * 8;  
      unsigned char *output = malloc(len+1);
      //IV
      DES_cblock ivec;

      //IV设置为0x0000000000000000
      memset((char*)&ivec, 0, sizeof(ivec));

      //加密
      long start = get_trick();
      DES_ncbc_encrypt(input, output, sizeof(input), &key_schedule, &ivec, DES_ENCRYPT);
      long spend = get_trick() - start;
      printf("encrypt spend:%ld\n", spend);
      printf("dlen:%d\n", strlen(output));

      //输出加密以后的内容
      for (int i = 0; i < len; ++i)
         printf("%02X", output[i]);
      printf("\n");

      memset((char*)&ivec, 0, sizeof(ivec));

      //解密
      start = get_trick();
      DES_ncbc_encrypt(output, input, len, &key_schedule, &ivec, 0);
      spend = get_trick() - start;
      printf("decrypt spend:%ld\n", spend);

      
      printf("%s\n", input);

      free(output);
      return EXIT_SUCCESS;
}



ecb:


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <openssl/des.h>

long get_mesl() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*1000 + tv.tv_usec/1000;
}

long get_trick() {
    struct timeval tv;
    gettimeofday(&tv,NULL);
    return tv.tv_sec*1000000 + tv.tv_usec;
}

int main(int argc,char **argv)
{
    DES_cblock key;
    //随机密钥
    DES_random_key(&key);

    DES_key_schedule schedule;
    //转换成schedule
    DES_set_key_checked(&key, &schedule); 

    const_DES_cblock input = "hehehe";
    DES_cblock output;

    printf("cleartext: %s\n", input);

    //加密
    long start = get_trick();
    DES_ecb_encrypt(&input, &output, &schedule, DES_ENCRYPT);
    long spend = get_trick() - start;
    printf("encrypt spend:%ld\n", spend);
    //printf("Encrypted!\n");

    printf("ciphertext: ");
    int i;
    for (i = 0; i < sizeof(input); i++)
         printf("%02x", output[i]);
    printf("\n");

    //解密
    start = get_trick();
    DES_ecb_encrypt(&output, &input, &schedule, DES_DECRYPT);
    spend = get_trick() - start;
    printf("decrypt spend:%ld\n", spend);

    printf("cleartext:%s\n", input);

    return 0;
} 

注意在gcc编译时要加上-lcrypto,引入openssl的动态库。


猜你喜欢

转载自blog.csdn.net/adofsauron/article/details/56017366