AES-256 使用 ECB 操作模式的 OpenSSL 库进行加密
AES-256 Encryption with OpenSSL library using ECB mode of operation
我正在尝试使用ECB模式使用OpenSSL库创建AES加密的示例。 很难找到任何文档,尤其是关于欧洲央行的文档,所以我举了一个使用 CBC 模式的代码示例,并尝试为 ECB 修改它。 我摆脱了 ECB 中未包含的内容,例如初始化向量,并尝试尽我所能修改代码。 完成后,我在编译后遇到了一些问题:
AES-256-ECB-Encryption.cpp: In function ‘int encrypt(unsigned char*, int, unsigned char*, unsigned char*)’:
AES-256-ECB-Encryption.cpp:27:63: error: too few arguments to function ‘int EVP_EncryptInit_ex(EVP_CIPHER_CTX*, const EVP_CIPHER*, ENGINE*, const unsigned char*, const unsigned char*)’
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
该错误说我的参数太少,无法在 int 加密函数中运行。 我也有 int 解密函数的错误。 我想知道这里是否有人可以帮助我澄清我的问题。 我知道欧洲央行模式存在漏洞,但我仍然想熟悉它。 另外,我知道密钥不应该硬编码,但我只是想运行一个示例以确保我有正确的想法。 我正在使用OpenSSL中libcrypto库中的EVP对称加密和解密。 如果这很重要,我在 Ubuntu 16.0.4 上。 如果有人能阐明我的问题或提供更多关于欧洲央行的文件,我们将不胜感激。
谢谢
以下是代码的其余部分:
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* In this example we are using 256 bit AES (i.e. a 256 bit key).
*/
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
/* Initialise the decryption operation. IMPORTANT - ensure you use a key
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
*/
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key))
handleErrors();
/* Provide the message to be decrypted, and obtain the plaintext output.
* EVP_DecryptUpdate can be called multiple times if necessary
*/
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
/* Finalise the decryption. Further plaintext bytes may be written at
* this stage.
*/
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
int main (void)
{
/* A 256 bit key */
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
/* Message to be encrypted */
unsigned char *plaintext =
(unsigned char *)"This is a test.";
/* Buffer for ciphertext. Ensure the buffer is long enough for the
* ciphertext which may be longer than the plaintext, dependant on the
* algorithm and mode
*/
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
unsigned char decryptedtext[128];
int decryptedtext_len, ciphertext_len;
/* Initialise the library */
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
/* Encrypt the plaintext */
ciphertext_len = encrypt (plaintext, strlen ((char *)plaintext), key, ciphertext);
/* Do something useful with the ciphertext here */
printf("Ciphertext is:n");
BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
/* Decrypt the ciphertext */
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key,
decryptedtext);
/* Add a NULL terminator. Expecting printable text */
decryptedtext[decryptedtext_len] = ' ';
/* Show the decrypted text */
printf("Decrypted text is:n");
printf("%sn", decryptedtext);
/* Clean up */
EVP_cleanup();
ERR_free_strings();
return 0;
}
该函数接受 5 个参数,为 iv
参数传递NULL
。
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, NULL))
从文档中:
int EVP_EncryptInit_ex( EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *type,
ENGINE *impl,
unsigned char *key,
unsigned char *iv);
正如老绝地大师@zaph平静地指示@akfe79"相信错误消息"。
相关文章:
- 通过 Openssl 命令行加密,通过 c++ 解密
- OpenSSL API,使用GCM(伽罗瓦计数器模式)进行AES加密
- 使用 char 数组公钥的 OpenSSL 进行 RSA 加密
- OpenSSL AES加密似乎无法正常工作
- 我是否需要在 OpenSSL 1.1.0+ 中使用加密锁定函数来实现线程安全?
- 从文件进行 Openssl EVP 加密和解密
- 加密任务使用QT崩溃,Openssl该怎么办
- 使用C 中的OpenSSL使用AES128加密字符串
- 如何通过编程方式进行差异openssl签名和加密
- AES 256-cbc 加密C++使用 OpenSSL
- openssl rc4 命令行加密和 cpp 文件实现 rc4 之间的区别
- OpenSSL和JSON文件加密和解密
- OPENSSL Blowfish CBC加密与PHP不同,C++
- C++Openssl加密例程在每次运行时生成不同的输出
- 在NodeJS和OpenSSL C++中加密时获得不同的值
- 如何在没有明文大小的情况下解密数据?-openssl加密c++ubuntu环境
- 使用OpenSSL/C++和PHP/Mcrypt进行AES-128-CBC加密:仅对第一个块进行解密
- OpenSSL:我使用存储外部文件的RSA密钥加密和解密文件,但有时我的程序会崩溃
- 使用 OpenSSL 兼容格式在 C# 中加密,在 Poco 中解密
- 基于密码的加密OpenSSL C++