OpenSSL AES_CFB128_ENCRYPT公共/私钥C

OpenSSL AES_cfb128_encrypt public/private key C++

本文关键字:私钥 公共 ENCRYPT AES CFB128 OpenSSL      更新时间:2023-10-16

我有使用常数密钥的非常基本的加密/解密应用程序。如何使此应用程序使用公共/私钥?用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";
    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;
}

我有使用常数密钥的非常基本的加密/解密应用程序。如何使此应用程序使用公共/私钥?使用OpenSL生成键并在我的代码变量CKEY中使用它们吗?

你不能。共享的密钥和私有密钥密码学足够不同,以确保您无法做到。或更正确的是,如果没有重大的重新设计和重写,就无法做到。

openssl为您的问题提供了两个(也许是三个)高水平的原语。这是它们的文档:

  1. EVP对称加密和解密
  2. EVP非对称加密和解密

"也许三"是:

  1. EVP身份验证的加密和解密

使用(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

ashe as as as openssl中并不是真正的设计问题。API的定义很好地用于手工操作 - 对称加密,身份验证的加密,不对称的加密,签名,验证,验证,哈希,Mac'ing等。将所有内容都插入一组API调用。

使用AES_KEYAES_set_encrypt_key和朋友提供的代码更难使用。如果您使用no-asm配置,则可以将其获得专用的AES实现。它也有一些地雷,例如在某些情况下不可携带。例如,我似乎回想起在一些大型平台上必须采取特殊护理,因为您需要字节汇总钥匙。


yu可能还想看看IPSEC和TLS如何做事。它们将工作流分为两个部分:(1)密钥交换(或密钥运输)和(2)批量加密。使用公共/私钥密码学发生关键交换部分。并建立一个共同的秘密。一旦建立了共享的秘密,对称密码将被键入并进行大量加密。

IPSEC具有更具防御性的安全姿势。TLS的运行速度有些快,可以使我的口味变得宽松。我仅在要求告诉我这样做时使用TLS(例如与现有系统互动)。否则,我更喜欢应用内VPN或类似IPSEC的方案。