C++ 从文件进行Openssl EVP加密和解密
下面是使用openssl EVP进行加密和解密的示例代码。当我同时执行加密和解密时,它似乎工作得很好。当我在文件中写入加密字符串并从文件中删除时,我得到了一个错误 加密.cC++ 从文件进行Openssl EVP加密和解密,c++,encryption,openssl,aes,C++,Encryption,Openssl,Aes,下面是使用openssl EVP进行加密和解密的示例代码。当我同时执行加密和解密时,它似乎工作得很好。当我在文件中写入加密字符串并从文件中删除时,我得到了一个错误 加密.c #include <openssl/conf.h> #include <openssl/evp.h> #include <openssl/err.h> #include <openssl/bio.h> #include <string.h> #include <
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <assert.h>
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_ecb(), NULL, key, iv))
handleErrors();
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int main (int argc, char *argv[])
{
if ( argc != 2 )
{
printf("Usage: <process> <file>\n");
exit(0);
}
/* A 256 bit key */
unsigned char *key = (unsigned char *) "key";
/* A 128 bit IV Should be hardcoded in both encrypt and decrypt. */
unsigned char *iv = (unsigned char *)"iv";
/* Message to be encrypted */
unsigned char *plaintext =
(unsigned char *)"Password";
unsigned char ciphertext[128],base64[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, iv,
ciphertext);
/* Do something useful with the ciphertext here */
printf("Ciphertext is:\n");
BIO_dump_fp (stdout, (const char *)ciphertext, ciphertext_len);
printf("%d %s\n", ciphertext_len, ciphertext);
int encode_str_size = EVP_EncodeBlock(base64, ciphertext, ciphertext_len);
printf("%d %s\n", encode_str_size, base64);
std::ofstream outFile (argv[1]);
outFile << base64;
outFile.close();
/* Clean up */
EVP_cleanup();
ERR_free_strings();
return 0;
}
核心
当我写入文件并从文件中读取内容时,我无法解密 传递给
EVP_EncryptInit_ex
和EVP_DecryptInit_ex
的密钥和IV不是字符串,而是根据密码大小固定的字符数组
在AES 256的情况下,密钥的长度为32字节(256位),IV的长度为16字节(128位)。传递的字符串常量不够长,无法满足这些约束。结果,上述函数读取的字符串超过了这些字符串的末尾,调用
至于最有可能发生的情况,当您在同一个程序中进行加密和解密时,它起作用的原因是,您可能正在将包含密钥和IV的相同缓冲区传递给这两个函数,因此这两个函数都在每个字符串结束后读取相同的未知字节集。当您在两个不同的程序中执行相同的操作时,这些未知数据是不同的,因此实际上有两组不同的key和IV
将密钥和IV声明为固定大小,并初始化两者。任何未显式初始化的字节都将设置为0,因此您将拥有已知的数量
unsigned char key[32] = (unsigned char *) "key";
unsigned char iv[16] = (unsigned char *)"iv";
dbush在密钥和IV问题上做对了,但后来他继续让它运行,而不是让它正确 如果您想使用密码,那么应该使用PBKDF:一个基于密码的密钥派生函数。如果要使用密钥,则应使用16、24或32字节的密钥,具体取决于所需的128、192或256位密钥大小 OpenSSL提供了自己的名为
EVP_BytesToKey
的算法和PBKDF2。(我没有录音)如何使用后者,更现代的算法
最后,一个AES密钥应该由128、192或256位组成,攻击者认为这些位是随机的。如果您没有为AES提供这样的密钥,那么库应该返回一个错误,而不是继续。故意给它输入太少或太多字节会导致灾难。填充零字节不是扩展密钥的方法。这显然不是C,而是C++!正确设置文本和文件扩展名!不要垃圾标签@Olaf用于加密的所有openssl/evp.h都是c。使用c库不符合c标记的条件!感谢您,我将尝试阅读更多关于这些openssl/evp的内容。通过nw解决。感谢@dbush感谢“填充零字节不是扩展密钥的方法”。
./encrypt file
Ciphertext is:
0000 - 52 6f a3 c6 6b ea 4a aa-a5 e8 9d 26 47 dc e9 b7 Ro..k.J....&G...
16 Ro��k�J����&G�鷈H�t�
24 Um+jxmvqSqql6J0mR9zptw==
./decrypt file
24 Um+jxmvqSqql6J0mR9zptw==
16 Ro��k�J����&G���
0000 - 52 6f a3 c6 6b ea 4a aa-a5 e8 9d 26 47 dc e9 b7 Ro..k.J....&G...
140405999580856:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:
Aborted (core dumped)
Core was generated by `./decrypt file'.
Program terminated with signal SIGABRT, Aborted.
#0 0x00007efe46356428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
54 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 0x00007efe46356428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007efe4635802a in __GI_abort () at abort.c:89
#2 0x000000000040110e in handleErrors() ()
#3 0x00000000004011eb in decrypt(unsigned char*, int, unsigned char*, unsigned char*, unsigned char*) ()
#4 0x0000000000401485 in main ()
unsigned char key[32] = (unsigned char *) "key";
unsigned char iv[16] = (unsigned char *)"iv";