OpenSSL:RSA加密/解密,密钥生成&关键持久性 [英] OpenSSL: RSA Encryption/Decryption, key generation & key persistance

查看:168
本文介绍了OpenSSL:RSA加密/解密,密钥生成&关键持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RSA在OpenSSL中构建需要以下内容的p2p应用程序:

I am trying to build a p2p application that requires the following, using RSA in OpenSSL:

-Encryption
-Decryption
-Generating Keys (done)
-Saving and loading keys (done)
-Saving the PUBLIC key as bytes so it can be sent over the sockets
-Loading keys from the above format

我选择使用EVP函数,无论什么意思。然而,我有最高的困难找到哪些功能我需要用来做这些事情,以什么顺序。 OpenSSL的官方文档似乎不存在。

I have chosen to use the EVP functions, whatever that means. However I am having supreme difficulty finding which functions I need to use to do these things, and in what order. Official documentation of OpenSSL seems to be non-existant.

有没有人知道我需要什么样的功能,以什么顺序和他们的原型?

Does anyone know what functions I need to use in what order and their prototypes? Any example code lying around would also be nice.

非常感谢,

twitchliquid64。

twitchliquid64.

PS:这是我到目前为止

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/engine.h>
#include <openssl/rand.h>

RSA* Generate_KeyPair(void)
{
  char rand_buff[16];
  EVP_PKEY *pkey = NULL;
  RSA* r;
  char* pass = "passgdfgf";//for now

  int bits = 512;       //      512, 1024, 2048, 4096
  unsigned long exp = RSA_F4;     //      RSA_3
  OpenSSL_add_all_algorithms();

  RAND_seed(rand_buff, 16); //On linux: RAND_load_file("/dev/urandom", 1024);
  r = RSA_generate_key(bits,exp,NULL,NULL);

  if (RSA_check_key(r)!=1);;; //Check key - error out

  //Create EVP to save to file.
  pkey = EVP_PKEY_new();
  EVP_PKEY_assign_RSA(pkey, r);

  //Save private key
  FILE* fp = fopen("private.key", "w");
  PEM_write_PrivateKey(fp,pkey,EVP_aes_256_cbc(),NULL,0,NULL,pass)
  fclose(fp);

  //Save public key
  fp = fopen("public.key", "w");
  PEM_write_PUBKEY(fp, pkey);
  fclose(fp);

  return r;
}

EVP_PKEY* ReadPrivKey_FromFile(char* filename, char* pass)
{
  FILE* fp = fopen(filename, "r");
  EVP_PKEY* key = NULL;
  PEM_read_PrivateKey(fp, &key, NULL, pass);
  fclose(fp);

  return key;
}

EVP_PKEY* ReadPubKey_FromFile(char* filename)
{
  FILE* fp = fopen(filename, "r");
  EVP_PKEY* key = NULL;
  PEM_read_PUBKEY(fp, &key, NULL, NULL);
  fclose(fp);

  return key;
}


推荐答案

我的问题:

您会发现OpenSSL附带的示例代码比文档更有用。例如,在apps / rsa.c中显示了使用RSA加密的文档。它可能有助于工作出OpenSSL命令行来执行每个功能,你想用命令行工具,然后弄清楚代码实际上是做什么(通过检查它),所以你可以让你的代码做同样的事情。
- David Schwartz

You'll find the example code that comes with OpenSSL more useful than the documentation. For example, documentation of encryption with RSA is shows in apps/rsa.c. It may help to work out the OpenSSL command lines to perform each function you want to do with the command line tool and then figure out what the code actually does (by inspecting it) so you can make your code do the same thing. – David Schwartz

这个例子代码正是我需要的,我建议任何有类似问题的人去查阅rsa代码和头文件,也是他们在文档中使用的小例子。

This example code was exactly what I needed, I advise anyone with a similar problem to consult the rsa code and header file, and also their are small use examples in the documentation as well.

这篇关于OpenSSL:RSA加密/解密,密钥生成&amp;关键持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆