我有非常基本的加密/解密应用程序,它使用常量键。如何使此应用程序使用公钥/私钥?是否足以使用openssl生成密钥并在我的代码变量ckey
中使用它们?
我可以以某种方式用我的库生成密钥吗?
#include "stdafx.h"
#include <openssl/aes.h>
#include <algorithm>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE + 1];
unsigned char outdata[AES_BLOCK_SIZE + 1];
std::fill(indata, indata + AES_BLOCK_SIZE, 0);
std::fill(outdata, outdata + AES_BLOCK_SIZE, 0);
/* ckey and ivec are the two 128-bits keys necesary to
en- and recrypt your data. Note that ckey can be
192 or 256 bits as well */
unsigned char ckey[] = "thiskeyisverybad";
const char ivecstr[AES_BLOCK_SIZE] = "goodbyworldkey\0";
unsigned char ivec[] = "dontusethisinput";
/* data structure that contains the key itself */
AES_KEY key;
/* set the encryption key */
AES_set_encrypt_key(ckey, 128, &key);
/* set where on the 128 bit encrypted block to begin encryption*/
int num = 0;
FILE* ifp;
FILE* oefp;
FILE* odfp;
ifp = fopen("infile.txt", "r");
if (ifp == NULL) perror("Error opening file");
oefp = fopen("outEncryptfile.txt", "w");
if (oefp == NULL) perror("Error opening file");
odfp = fopen("outDecryptfile.txt", "w");
if (odfp == NULL) perror("Error opening file");
int b = 0;
int w = 0;
memcpy(ivec, ivecstr, AES_BLOCK_SIZE);
while (1)
{
std::fill(indata, indata + AES_BLOCK_SIZE, 0);
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, ifp);
b = b + bytes_read;
indata[AES_BLOCK_SIZE] = 0;
//std::cout << "original data:\t" << indata << std::endl;
std::cout << indata;
AES_cfb128_encrypt(indata, outdata, bytes_read, &key, ivec, &num,AES_ENCRYPT);
bytes_written = fwrite(outdata, 1, bytes_read, oefp);
w = w + bytes_written;
if (bytes_read < AES_BLOCK_SIZE)
break;
}
fclose(oefp);
oefp = fopen("outEncryptfile.txt", "r");
if (oefp == NULL) perror("Error opening file");
b = 0;
memcpy(ivec, ivecstr, AES_BLOCK_SIZE);
while (1)
{
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, oefp);
b = b + bytes_read;
indata[AES_BLOCK_SIZE] = 0;
std::cout << "original data:\t" << indata << std::endl;
AES_cfb128_encrypt(indata, outdata, bytes_read, &key, ivec, &num, AES_DECRYPT);
std::cout << "decrypted data:\t" << outdata << std::endl;
bytes_written = fwrite(outdata, 1, bytes_read, odfp);
if (bytes_read < AES_BLOCK_SIZE)
break;
}
fclose(odfp);
return 0;
}
答案 0 :(得分:1)
我有非常基本的加密/解密应用程序,它使用常量键。如何使此应用程序使用公钥/私钥?是否足以使用openssl生成密钥并在我的代码变量ckey中使用它们?
你不能。共享密钥和私钥加密是完全不同的,以确保您不能这样做。或者更准确地说,如果没有重大的重新设计和重写,你就无法做到。
OpenSSL为您的问题提供了两个(可能是三个)感兴趣的高级原语。这是关于它们的文档:
&#34;也许三个&#34;是:
以下是使用(1)EVP对称加密和(2)上述EVP非对称加密时的常见API调用:
EVP对称加密
EVP_CIPHER_CTX_new
EVP_EncryptInit_ex
EVP_EncryptUpdate
EVP_EncryptFinal_ex
EVP非对称加密
EVP_CIPHER_CTX_new
EVP_SealInit
EVP_SealUpdate
EVP_SealFinal
EVP_CIPHER_CTX_free
尽管如此,它并不是OpenSSL中的设计问题。 API已针对手头操作进行了明确定义 - 对称加密,经过身份验证的加密,非对称加密,签名,验证,散列,mac等等。很难将所有内容整合到一组API调用中。
您使用AES_KEY
,AES_set_encrypt_key
和朋友提供的代码更难以使用。它是一个专门的纯软件AES实现,如果您使用no-asm
进行配置,则可以获得该实现。它也有一些地雷,在某些情况下是不便携的。例如,我似乎记得在一些大端平台上需要特别小心,因为你需要对密钥进行字节交换。
Yu可能还想看看IPsec和TLS是如何做的。它们将工作流分为两部分:(1)密钥交换(或密钥传输)和(2)批量加密。密钥交换部分使用公钥/私钥密码术进行。并建立共享秘密。一旦建立了共享密钥,对称密码就会被锁定并进行批量加密。
IPsec具有更具防御性的安全态势。根据我的口味,TLS运行速度有点快。我只在需求告诉我这样做时使用TLS(比如与现有系统互操作)。否则,我更喜欢应用内VPN或类似IPsec的方案。