我一直在努力学习OpenSSL C API。我找到了有趣的密码挑战页面,并想尝试一些简单的挑战。我参加了挑战7,AES ECB:https://cryptopals.com/sets/1/challenges/7
在阅读了OpenSSL文档之后,我想做一些编码。我从OpenSSL wiki页面复制粘贴了下面的大部分代码。但不知何故我做不到这件事。我试着用谷歌搜索这个错误,发现人们在Python中很容易解决这个问题,但是没有发现C。
输入文件是用Base64编码的,带有换行符。所以首先我做的是删除新行。我是用tr工具手动完成的,而不是编程的。
tr -d '\n' < cipertext > ciphertext.no_newlines接下来,我对它进行了解码,也是手动的:
base64 -d ciphertext.no_newlines > ciphertext.no_newlines_decoded接下来,我主要是从这个OpenSSL网页解密复制所有内容。
加密和解密函数是文档的精确副本。我只修改了主要功能。我还从某个地方复制了readFile函数(测试了它是否正常工作)。
我是这样编译的:
gcc -I /opt/openssl/include challenge7.c /opt/openssl/lib/libcrypto.a -lpthread -ldl -o challenge7 但我收到了这个错误:
140095710082880:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:crypto/evp/evp_enc.c:570:
[1] 24550 abort (core dumped) ./challenge7我在googled上搜索了这个错误,发现这可能与OpenSSL版本之间的不兼容有关,即用于加密文件的版本和我计算机上的版本。这真的是原因吗?如果是这样的话,那么这是史上最糟糕的库,我不能用相同的算法用不同版本的库解密文件。我真不敢相信。我听说OpenSSL很糟糕,但不知怎么的,我不相信这是这个错误的原因。
有人能帮我找出我一直在做的错事吗?整个守则如下:
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
char* readFile(char* filename, int* size)
{
char* source = NULL;
FILE *fp = fopen(filename, "r");
if (fp != NULL) {
if (fseek(fp, 0L, SEEK_END) == 0) {
long bufsize = ftell(fp);
if (bufsize == -1) {
fputs("ftell error", stderr);
} else {
source = malloc(sizeof(char) * (bufsize + 1));
if (fseek(fp, 0L, SEEK_SET) != 0) {
fputs("fseek error", stderr);
}
size_t nl = fread(source, sizeof(char), bufsize, fp);
if (ferror(fp) != 0) {
fputs("Error reading file", stderr);
} else {
source[nl] = '\0';
*size = nl;
}
}
}
fclose(fp);
}
return source;
}
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();
/*
* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
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 *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();
/*
* Initialise the decryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
* IV size for *most* modes is the same as the block size. For AES this
* is 128 bits
*/
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
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)
{
unsigned char *key = (unsigned char *)"59454C4C4F57205355424D4152494E45";
unsigned char *iv = NULL;
int decryptedtext_len;
int ciphertext_len;
char* ciphertext = readFile("ciphertext.no_newlines_decoded", &ciphertext_len);
unsigned char decryptedtext[10000];
decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
decryptedtext[decryptedtext_len] = '\0';
printf("Decrypted text is:\n");
printf("%s\n", decryptedtext);
return 0;
}发布于 2019-11-18 17:12:43
可能的原因是您使用的是AES-CBC密码,而不是AES-ECB建议(EVP_DecryptInit_ex(ctx, **EVP_aes_256_cbc()**, NULL, key, iv)).
而实际的错误可能是由于将空指针作为这个CBC密码的IV传递(我不知道这个API在这种情况下是如何起作用的,但是IV对于CBC加密模式是必需的)。
因此,尝试使用适当的EVP_aes_128_ecb()加密模式。
更新
导致EVP_DecryptFinal_ex错误的另一件事是输入数据字节长度不是16的倍数(AES块长度)。检查您的ciphertext.no_newlines_decoded大小是否为16的倍数。
下一点是,OpenSSL API操作的是字节,而不是以零结尾的字符串。因此,您应该将"rb“而不是"r”传递给您的fopen调用,并读取字节而不附加任何零终止符。
我还认为您使用了一个键错误,我认为您应该定义和传递给它(unsigned char *key = (unsigned char *)"YELLOW SUBMARINE")。虽然以您的方式使用密钥不会导致您面临的错误,但它将导致不正确的解密文本。
https://stackoverflow.com/questions/58919056
复制相似问题