2017-02-20 185 views
1

我有非常基本的使用常量密钥的加密/解密应用程序。如何使这个应用程序使用公钥/私钥?使用openssl生成密钥并将其用于我的代码变量ckey足够了吗?OpenSSL AES_cfb128_encrypt公钥/私钥C++

我可以以某种方式生成密钥与我的图书馆?

#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

“公钥/私钥”是非对称加密(RSA/EC),与AES等对称密钥加密完全不同。进一步的非对称加密不是为数据加密而设计的,主要用于加密对称密钥,要加密的数据必须短于加密密钥。对称密钥加密被定义为加密数据。 – zaph

+1

你应该*不*使用'AES_encrypt'和朋友。这是一个纯软件实现,所以你不会喜欢硬件支持,比如AES-NI。您应该使用'EVP_ *'功能。请参阅OpenSSL wiki上的[EVP Symmetric Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption)。事实上,您应该使用经过身份验证的加密,因为它提供了*机密性和真实性。请参阅OpenSSL wiki上的[EVP Authenticated Encryption and Decryption](http://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption)。 – jww

回答

1

我有非常基本的加密/解密应用程序,使用恒定的关键。如何使这个应用程序使用公钥/私钥?使用openssl生成密钥并在我的代码变量ckey中使用它们就足够了吗?

你不行。共享密钥和私钥密码学有很大的不同,以确保您无法做到这一点。或者更准确地说,没有重新设计和重写就无法做到。

OpenSSL为您的问题提供了两个(也许是三个)高级关注基元。下面是对他们的文档:

  1. EVP Symmetric Encryption and Decryption
  2. EVP Asymmetric Encryption and Decryption

的 “也许三” 是:

  • EVP Authenticated Encryption and Decryption
  • 以下是使用(1)EVP对称加密和(2) EVP非对称加密从上方:

    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'ing等等。很难将所有东西都整合到一组API调用中。

    您提供的代码使用AES_KEYAES_set_encrypt_key和朋友更难以使用。它是一个专门的纯软件AES实现,如果您使用no-asm进行配置,则会获得该实现。它也有一些地雷,就像在某些情况下不能携带一样。例如,我似乎记得在一些大端平台上需要特别小心,因为您需要字节交换密钥。


    余可能也想看看IPsec和TLS如何做。他们将工作流分成两部分:(1)密钥交换(或密钥传输)和(2)批量加密。密钥交换部分使用公钥/私钥密码术进行。并建立一个共享的秘密。一旦建立共享密钥,对称密码就会被锁定,并进行批量加密。

    IPsec有更安全的防御姿势。我的口味TLS运行速度有点快。当需求告诉我这样做时(例如与现有系统进行互操作),我只使用TLS。否则,我更喜欢应用内VPN或类似IPsec的方案。