C mcrypt不使用rijndael 128(aes)工作/解密

时间:2015-02-12 12:50:11

标签: c encryption aes mcrypt rijndael

我正在尝试使用一个小函数将“秘密字”编码为二进制加密的“字符串”,然后将其转换为base64,通过互联网发送并在另一端解密。

这是我的代码:

编码功能:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"


#include <math.h>

int encrypt(
    void* buffer,
    int buffer_len, /* Because the plaintext could include null bytes*/
    char* IV,
    char* key,
    int key_len
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;}

  mcrypt_generic_init(td, key, key_len, IV);
  mcrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);

  return 0;
}

void display(char* ciphertext, int len){
  int v;
  for (v=0; v<len; v++){
    printf("%d", ciphertext[v]);
  }
  printf("\n");
}

int main()
{
  MCRYPT td, td2;
  char * plaintext = "CA7D22F5D7BBB3291487D3EFD935E";
  char* IV = "AAAAAAAAAAAAABBB";
  char *key = "0123456789abcdef";
  int keysize = 32; /* 256 bits */
  char* buffer;
  int buffer_len = 32;
  int* ret;

  buffer = calloc(1, buffer_len);
  strncpy(buffer, plaintext, buffer_len);

  printf("plain:   %s\n", plaintext);

  encrypt(buffer, buffer_len, IV, key, keysize);
  char * mybase64 = base64(buffer, buffer_len, ret);
  printf("b64   : %s\n", mybase64);
  printf("cipher: "); display(buffer , buffer_len);
  printf("buffer: %s\n", buffer);

  return 0;
}

解码功能:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"

#include <mcrypt.h>

#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int decrypt(
    void* buffer,
    int buffer_len,
    char* IV,
    char* key,
    int keysize
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;}

  mcrypt_generic_init(td, key, keysize, IV);
  mdecrypt_generic(td, buffer, buffer_len);
  mcrypt_generic_deinit (td);
  mcrypt_module_close(td);

  return 0;
}

void display(char* ciphertext, int len){
  int v;
  for (v=0; v<len; v++){
    printf("%d", ciphertext[v]);
  }
  printf("\n");
}

int main()
{
//  MCRYPT td;
  char* IV = "AAAAAAAAAAAAABBB";
  char *key = "0123456789abcdef";
  int keysize = 32; /* 256 bits */
  int buffer_len = 32;
  int* ret;


  char * mybase64 = "0DMkJ3UozhLfYjNav5Ih6rLd+aJua5KNTPHs/m2flLo=";

  char * buffer = unbase64(mybase64, (int)strlen(mybase64)+1, ret);


  printf("cipher: "); display(buffer , buffer_len);
  printf("buffer: %s\n", buffer);
  decrypt(buffer, buffer_len, IV, key, keysize);

  printf("plain: %s\n", buffer);

  return 0;
}

我用过的base64函数: https://github.com/superwills/NibbleAndAHalf/blob/master/NibbleAndAHalf/base64.h

奇怪的是,编码的二进制数据在控制台上“看起来”相同,也是十六进制,但它没有被正确解码,只是奇怪的字符。

有人有个主意吗?

编辑:这是运行程序测试时的结果:。

编码:

user@0F5NV:~/aes$ ./aes 
plain:   CA7D22F5D7BBB3291487D3EFD935E
b64   : EWQ2NxzOGCB1XGARxdfC7kLMGzAGJZ8/sEQf6dThxfk=
cipher: 17100545528-502432117929617-59-41-62-1866-522748637-9763-806831-23-44-31-59-7
buffer: d67? u\`????B?%???D?????

解码:

user@0F5NV:~/aes$ ./unaes
b64   : EWQ2NxzOGCB1XGARxdfC7kLMGzAGJZ8/sEQf6dThxfk=
cipher: 17100545528-502432117929617-59-41-62-1866-522748637-9763-806831-23-44-31-59-7
buffer: d67? u\`????B?%???D?????
plain: ?Q?X?P??Qa?t?@???wi ?qa?Z

如您所见,密码和解码在编码和解码中是相同的但不是明文......为什么?!

0 个答案:

没有答案