English 中文(简体)
Opensl base64 解码字符串并不总是解密
原标题:Openssl base64 decoded string does not always decrypt

我试图用RSA_ public_ encrypt () 用公用密钥加密一些普通文本, 这样数据就会被发送到远程服务器进行验证。 我相信, 加密/ 解密工作正常, 因为 RSA_ public_ encrypt 的输出可以传送到 RSA_ public_ decrypt 并且有效 。 我现在的问题是, 我需要将数据编码64 为基础, 才能发送到 HTTP 上 。

作为测试( 在将它发送到服务器之前) 我将 RSA_ public_ encrypt () 的输出编码为64, 然后解码并将其传送回 RSA_ public_ decrypt () 。 这似乎有一段时间有效, 并且以这样的错误失败 :

error:0407A079:rsa routines:RSA_padding_check_PKCS1_OAEP:oaep decoding error

当我使用Memcmp来比较原始数据( base64 前) 和我的 base64 解码函数的输出时, 尽管内容似乎匹配( 在视觉工作室, 查看内存内容为十六进制), 我仍然收到一个 - 1 。 我还用各种在线工具检查了基数64 编码版本, 它们似乎解码为预期值 。

我重复检查过基数64/unbase64 函数的输入/产出无效,这似乎没有什么区别。

这个问题我一直在兜圈子呆了好几天,但我相信这一定与64基数编码/编码过程有关,因为当它不涉及时,一切都会起作用。如果有人对如何发生这种情况有任何建议的话,那将不胜感激。

<强度 > Opensl 版本: 1. 0.1c

<强度>平台: Windows/MSVC

<强度 > Base64 代码:

char *base64(const unsigned char *input, int length)
{
  BIO *bmem, *b64;
  BUF_MEM *bptr;
  char *buff = NULL;

  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bmem = BIO_new(BIO_s_mem());
  b64 = BIO_push(b64, bmem);
  BIO_write(b64, input, length);
  BIO_flush(b64);
  BIO_get_mem_ptr(b64, &bptr);

  buff = (char *)malloc(bptr->length+1);
  memcpy(buff, bptr->data, bptr->length);
  buff[bptr->length] =   ;

  BIO_free_all(b64);

  return buff;
}

<强> Unbase64 代码:

char *unbase64(unsigned char *input, int length)
{
  BIO *b64, *bmem;

  char *buffer = (char *)malloc(length+1);
  memset(buffer, 0, length+1);

  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  bmem = BIO_new_mem_buf(input, length);
  bmem = BIO_push(b64, bmem);

  BIO_read(bmem, buffer, length);
  buffer[length] =   ;

  BIO_free_all(bmem);

  return buffer;
}

< 强度 > 加密“ hello world” 示例:

š:Œ¼JŒ"ÌïëŸÔè#¢Oo‚À–    œêçrú¿±a/8ƒòÌ¢QT¹]nío

<强度>Base64 版本(使用上述代码):

G5qdOgWMvEqMIswZ7+uf1OgPI6JPb4LAlgmc6lzncvq/sWEvOIPyzByiUVwMjYFUuV0Vbu1v

谢谢你的帮助!

最佳回答

您正在通过 unbase64 函数中的正确大小 。 它应该是返回的 base64 缓冲的大小, 而不是目的地缓冲的大小, 即使用示例主函数 :

int main(void)
{
  unsigned char bufron[2000];
  int i;
  char *chab;
  unsigned char *chac;

  for (i = 0; i < 2000; i++) {
      bufron[i] = i % 255;
  }

  chab = base64(bufron, 2000);
  printf("%s
", chab);

  chac = unbase64(chab, strlen(chab));

  for (i = 0; i < 2000; i++) {
      if (bufron[i] != chac[i]) {
          printf("Failed at %d
", i);
          return (1);
      }
  }
}
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签