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



我试图用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 textt : %s n",clear); 
      memcpy(encrypted, Encrypt(key, clear, sizeof(clear)), sizeof(clear));
      printf("Encrypted textt : %s n",encrypted);
      return 0;
}
  1. DES有一个8字节的56位密钥(LSB不是密钥的一部分,它用于奇偶校验),所以不能使用16字节的密钥(奇偶校验通常被忽略)。

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

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

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

最新更新