OpenSSL AES_CFB128_ENCRYPT公共/私钥C
OpenSSL AES_cfb128_encrypt public/private key C++
我有使用常数密钥的非常基本的加密/解密应用程序。如何使此应用程序使用公共/私钥?用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为您的问题提供了两个(也许是三个)高水平的原语。这是它们的文档:
- EVP对称加密和解密
- EVP非对称加密和解密
"也许三"是:
- 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_KEY
,AES_set_encrypt_key
和朋友提供的代码更难使用。如果您使用no-asm
配置,则可以将其获得专用的AES实现。它也有一些地雷,例如在某些情况下不可携带。例如,我似乎回想起在一些大型平台上必须采取特殊护理,因为您需要字节汇总钥匙。
yu可能还想看看IPSEC和TLS如何做事。它们将工作流分为两个部分:(1)密钥交换(或密钥运输)和(2)批量加密。使用公共/私钥密码学发生关键交换部分。并建立一个共同的秘密。一旦建立了共享的秘密,对称密码将被键入并进行大量加密。
IPSEC具有更具防御性的安全姿势。TLS的运行速度有些快,可以使我的口味变得宽松。我仅在要求告诉我这样做时使用TLS(例如与现有系统互动)。否则,我更喜欢应用内VPN或类似IPSEC的方案。
- 如何在openssl-ecc中获取十六进制格式的私钥
- 如何在C++中使用X509证书模在令牌中查找私钥
- 私钥解密代码使用公共接口说明符到 BSAFE 库 v6.0?
- 从字符串 Crypto++ 导入 RSA 公钥/私钥
- 将私钥和证书存储在C++/OpenSSL中
- 区块链超级账本私钥错误
- 博坦加载现有的 RSA 私钥
- C 从XML导入CAPI/CNG中的RSA私钥
- 使用密码将 RSA 私钥写入 PEM 文件
- 一代 RSA-2048 密钥(公钥和私钥)
- 如何获取PCCERT_CONTEXT私钥的NCRYPT_KEY_HANDLE?
- 将ECDSA Keypair的公共和私钥分开
- RSA 私钥加密 QT C++
- 在 SSH 上的公钥-私钥身份验证上的 libcurl 实现
- 使用 openssl libcrypto 解密具有 RSA 私钥的数据时RSA_NO_PADDING的使用
- 使用 WinInet 的客户端身份验证(证书 + 私钥)
- OpenSSL AES_CFB128_ENCRYPT公共/私钥C
- 如何通过PKCS#11 API从电子令牌获取私钥
- 使用 OpenSSL 生成私钥
- 在 GnuPG 中从 C/C++ 和参数生成公钥/私钥