使用 16 字节密钥的 C++ Des 加密

C++ Des encryption with 16 bytes key

我正在尝试使用 16 字节密钥在 DES 中加密具有动态长度的文本,但密钥和文本的块大小存在问题,我正在使用 openssl 库进行 DES 加密。如何使用长度为 16 字节的密钥。

这是我的例子:

char * Encrypt( char Key, char *Msg, int size) { 
      static char*    Res;
      DES_cblock      Key2;
      DES_key_schedule schedule;

      Res = ( char * ) malloc( size );

      memcpy(Key2, Key, 8);
      DES_set_odd_parity( &Key2 );
      DES_set_key_checked( &Key2, &schedule );

      unsigned char buf[9];    
      buf[8] = 0;

      DES_ecb_encrypt(( DES_cblock  ) &Msg, ( DES_cblock  ) &buf, &schedule, DES_ENCRYPT );    
      memcpy(Res, buf, sizeof(buf));    
      return (Res);
}

int main(int argc, char const *argv[]) {
      char key[] = "password";
      char clear[] = "This is a secret message";
      char *encrypted;

      encrypted = (char *) malloc(sizeof(clear));

      printf("Clear text\t : %s \n",clear); 

      memcpy(encrypted, Encrypt(key, clear, sizeof(clear)), sizeof(clear));

      printf("Encrypted text\t : %s \n",encrypted);
      return 0;
}
  1. DES 有一个 8 字节的 56 位密钥(LSB 不用作密钥的一部分,它用于奇偶校验)所以你不能使用 16 字节的密钥(奇偶校验)一般被忽略)。

  2. 不要使用 DES,它不安全,已被 AES 取代。

  3. 不要使用 ECB 模式,它不安全,请参阅 ECB mode,向下滚动到企鹅。

AES 允许 128、192 和 256 位密钥。