【发布时间】:2018-08-16 10:13:44
【问题描述】:
所以我正在研究 AES 加密,但这个错误一直困扰着我。
错误:
lib(6) func(101) reason(100) evp_enc.c
我正在用一个程序加密我的文件并用另一个程序解密它。 使用此代码成功加密。
#include "stdafx.h"
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <string>
#include <iostream>
#include <fstream>
#include <openssl/applink.c>
using namespace std;
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_cbc(), 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);
// cout << ciphertext_len << "\n";
return ciphertext_len;
}
using namespace std;
int main(void)
{
/* A 256 bit key */
unsigned char key[2000];
memset(key, 0, sizeof(key));
char s; int initializer = 0;
/* Key reading */
string path = "C:/openssl/mykey.pem";
ifstream myfile(path);
while (!myfile.eof())
{
myfile >> s;
key[initializer] = s;
initializer++;
/* Key is read in such a way that each character is stored into the array */
}
cout << key;
myfile.close();
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0123456789012345";
/* Message to be encrypted */
//unsigned char text[]="weufhskgwesjfho";
char text[2000];
memset(text, 0, sizeof(text));
// Taking input of the text for encryption
char f; int init = 0;
/* Key reading */
string pathToFile = "C:/Users/Zeephremia/Desktop/a.txt";
ifstream tfs(pathToFile);
while (!tfs.eof())
{
tfs >> f;
text[init] = f;
init++;
/* Key is read in such a way that each character is stored into the array */
}
tfs.close();
//cout << text << endl;
// Message is type casted
unsigned char *plaintext = (unsigned char *)text;
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
int ciphertext_len;
/* Encryption of the plaintext */
ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
ciphertext[ciphertext_len] = '\0';
cout << "\n\nCipher text is \n \n";
cout << ciphertext;
ofstream e;
e.open("c:/users/zeephremia/desktop/b.txt");
e << ciphertext;
BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
return 0;
}
但是,当我尝试使用此代码对其进行解密时,
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <string>
#include <iostream>
#include <fstream>
#include <openssl/applink.c>
using namespace std;
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
using namespace std;
int main(void)
{
/* A 256 bit key */
unsigned char key[2000];
memset(key, 0, sizeof(key));
char s; int initializer = 0;
/* Key reading */
string path = "C:/openssl/mykey.pem";
ifstream myfile(path);
while (!myfile.eof())
{
myfile >> s;
key[initializer] = s;
initializer++;
/* Key is read in such a way that each character is stored into the array */
}
// cout << key;
myfile.close();
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0123456789012345";
/* Message to be encrypted */
//unsigned char text[]="weufhskgwesjfho";
char text[2000];
memset(text, 0, sizeof(text));
// Taking input of the text for encryption
char f; int init = 0;
/* Key reading */
string pathToFile = "C:/Users/Zeephremia/Desktop/b.txt";
ifstream tfs(pathToFile);
while (!tfs.eof())
{
tfs >> f;
text[init] = f;
init++;
/* Key is read in such a way that each character is stored into the array */
}
tfs.close();
//cout << text << endl;
// Message is type casted
unsigned char *plaintext = (unsigned char *)text;
unsigned char decryptedtext[2000];
memset(decryptedtext, 0, sizeof(decryptedtext));
int len = init-1;
/* Buffer for the decrypted text */
int decryptedtext_len;
cout << "The encryption is: " << plaintext << endl;
/*decrytption of the plaintext */
decryptedtext_len = decrypt(plaintext, len, key, iv, decryptedtext);
cout << decryptedtext;
system("pause");
return 0;
}
它给了我这个错误
2332:Erorr:0605506D:lib(6) func(101) reason(100) evp_enc.c
还有这个
调试错误! Abort() 已被取消。
从我的小实验中,我发现错误就在这一行。
decryptedtext_len = 解密(明文,len,密钥,iv,解密文本);
任何形式的帮助将不胜感激,非常感谢。 :)
【问题讨论】:
标签: c++ visual-studio encryption openssl aes