2017-02-20 182 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的方案。