在3DES解密后将字符解码为C中的UTF8

时间:2015-10-01 12:11:02

标签: c encryption padding 3des

我需要一些帮助将C中的char解码为UTF8解释。

到目前为止,我的代码以这种方式工作:

  • 初始化char时使用十六进制表示“密码”70617373776F7264
  • 然后使用3DES DD201F609E49C0609FABA4C8AAFBB1E5
  • 对其进行加密
  • 然后使用3DES 70617373776F72640808080808080808
  • 成功解密

printf("decrypted: %s",dec)语句中,所有内容看起来都很正常,并显示为decrypted: password

然而,在进行字符串比较时,它不匹配。仔细查看char,我可以看到它出现为\001password\010\010\010\010\010\010\010\010(这是由于填充)

有没有办法解开或解码到 UTF8 something similar to this

使用代码编辑:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/des.h>
#include <openssl/rand.h>
#include <ldap.h>
#include "k.h"
#include "hex.h"


#define ULONG unsigned long
#define INT unsigned int


char *encrypt(char *data)
{

  int i = 0;
  int len = 0;
  int nlen = 0;


  char *key1 = "1313232323231313";
  char *key2 = "6789678967896789";
  char *key3 = "1313232323231313";

  /* Padding */
  char ch = '\0';
  unsigned char out[64] = {0};
  unsigned char src[64] = {0};


  unsigned char *ptr  = NULL;
  unsigned char block[8] = {0};
  DES_key_schedule ks1, ks2, ks3;

  /* set password table */
  ptr = hex2bin(key1, strlen(key1), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks1);

  ptr = hex2bin(key2, strlen(key2), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks2);

  ptr = hex2bin(key3, strlen(key3), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks3);

  ptr = hex2bin(data, strlen(data), &nlen);
  memcpy(src, ptr, nlen);
  free(ptr);

  len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;

  ch = 8 - nlen % 8;
  memset(src + nlen, ch, (8 - nlen % 8) % 8);

  printf("Raw data: ");
  for (i = 0; i < len; i++) {
      printf("%02X", *(src + i));
  }
  printf("\n");

  for (i = 0; i < len; i += 8) {
      DES_ecb3_encrypt((C_Block *)(src + i), (C_Block *)(out + i), &ks1, &ks2, &ks3, DES_ENCRYPT);
  }

  printf("Encrypted: ");
  for (i = 0; i < len; i++) {
      printf("%02X" , *(out + i));
  }
  printf("\n");


  return out;

}


char *decrypt(char *data)
{

  int i = 0;
  int len = 0;
  int nlen = 0;


  char *key1 = "1313232323231313";
  char *key2 = "6789678967896789";
  char *key3 = "1313232323231313";

  /* Padding */
  int ch = 0;
  unsigned char out[64] = {0};
  unsigned char src[64] = {0};


  unsigned char *ptr  = NULL;
  unsigned char block[8] = {0};
  DES_key_schedule ks1, ks2, ks3;

  /* set password table */
  ptr = hex2bin(key1, strlen(key1), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks1);

  ptr = hex2bin(key2, strlen(key2), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks2);

  ptr = hex2bin(key3, strlen(key3), &nlen);
  memcpy(block, ptr, sizeof(block));
  free(ptr);
  DES_set_key_unchecked((C_Block *)block, &ks3);

  ptr = hex2bin(data, strlen(data), &nlen);
  memcpy(src, ptr, nlen);
  free(ptr);

  len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;

  ch = 8 - nlen % 8;
  memset(src + nlen, ch, (8 - nlen % 8) % 8);

  printf("Raw data: ");
  for (i = 0; i < len; i++) {
      printf("%02X", *(src + i));
  }
  printf("\n");

  for (i = 0; i < len; i += 8) {
      DES_ecb3_encrypt((C_Block *)(src + i), (C_Block *)(out + i), &ks1, &ks2, &ks3, DES_DECRYPT);
  }

  printf("Decrypted: ");
  for (i = 0; i < len; i++) {
      printf("%02X", *(out + i));
  }

  printf("\n");
  return out;

}


K DES_ecb3_do(K user, K pass,K fl )
{
  int res = 0;
  int flag = fl->i;
  char *usn = user->s;

  char *enc = pass->s;
  char *decr = "";
  char dec[32];

  if(flag==1)
  {
    decr = encrypt(enc);
    strcat(dec,decr);
  }
  else if(flag==0)
  {
    decr = decrypt(enc);
    strcat(dec,decr);
  }
  int ret;
  ret = strcmp(dec, "password");
  if(ret==0)
  {
    printf("they match");
  }
  else
  {
    printf("they don't match\n");
    return (K) 0;
  }  
 printf("decrypted pass is:%s\n",dec);

   return ks(dec);
}

2 个答案:

答案 0 :(得分:2)

decrypt函数中,您从局部变量返回解密结果,该变量超出了调用者的范围。

out[64]更改为static或全局或malloc

最后,您的代码会调用UB,因为您正在返回堆栈区域的地址。

OP评论请求后编辑

您的代码是

char *decrypt(char *data)
{

  int i = 0;
  int len = 0;
  int nlen = 0;


  char *key1 = "1313232323231313";
  char *key2 = "6789678967896789";
  char *key3 = "1313232323231313";

  /* Padding */
  int ch = 0;
  unsigned char out[64] = {0};
  unsigned char src[64] = {0};

  // stuff....
 return out;
}

此代码将数组声明为本地,否则堆栈已分配。该变量仅在本地范围内可用:在decrypt函数内。

这意味着当函数返回调用函数DES_ecb3_do时,可能会损坏内存地址。你不应该访问该地址。

要解决此问题,您必须使用out函数范围中的decrypt变量,例如,使用以下某个选项:

1)您将out定义为global

unsigned char out[64] = {0};

char *decrypt(char *data)
{
   // STUFF
}

2)您在函数中将out定义为static

char *decrypt(char *data)
{
   static unsigned char out[64] = {0};
   // STUFF
}

3)您将out定义为poiter,将malloc定义为:

char *decrypt(char *data)
{
   unsigned char *out] = malloc(64);
   // STUFF
}

希望它足够清楚。

答案 1 :(得分:0)

是因为你的解密字符串不是NULL终止的吗? 如果您的&#34;字符串比较&#34;是     if(dec ==&#34;密码&#34;) 那不行,试试吧     if(strcmp(dec,&#34; password&#34;)== 0)