AES_ctr128_encrypt()“分段错误” (OpenSSL)
我需要使用 openssl 在 C 中解密一些密码文本(aes 128 ctr)编程,因为我使用的库版本不支持 aes ctr 的 EVP 我尝试使用 AES_ctr128_encrypt(),但我遇到分段错误,这里是我正在使用的代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
int chartoint(char car);
char * extochar(char * in, int inLen);
struct ctr_state {
unsigned char ivec[16];
unsigned int num;
unsigned char ecount[16];
};
void init_ctr(struct ctr_state *state, const unsigned char iv[16]){
state->num = 0;
memset(state->ecount, 0, 16);
memcpy(state->ivec, iv, 16);
}
void main(){
unsigned char * cypher = extochar("874d6191b620e3261bef6864990db6ce",32);
unsigned char * key = extochar("2b7e151628aed2a6abf7158809cf4f3c",32);
unsigned char * iv = extochar("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",32);
unsigned char out[256]; //more than needed
AES_KEY aes_key;
int msg_len = 16;
struct ctr_state status;
if (!AES_set_encrypt_key(key, 16, &aes_key)){
printf("key error");
exit(-1);
}
init_ctr(&status, iv);
AES_ctr128_encrypt(cypher, out, msg_len, &aes_key, status.ivec, status.ecount, &status.num);
//expected plaintext: "6bc1bee22e409f96e93d7e117393172a"
}
分段错误位于指令 AES_ctr128_encrypt() 上,最后一个,而我正在期待“6bc1bee22e409f96e93d7e117393172a”作为明文(使用 printf("%02x", var[i]) 来打印它)
i need to decrypt some cypherd text (aes 128 ctr) programming in C using openssl, since libraries version i'm using don't support EVP for aes ctr i'm tring to use AES_ctr128_encrypt(), but i get segmentation fault, here's the code i'm using:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/aes.h>
int chartoint(char car);
char * extochar(char * in, int inLen);
struct ctr_state {
unsigned char ivec[16];
unsigned int num;
unsigned char ecount[16];
};
void init_ctr(struct ctr_state *state, const unsigned char iv[16]){
state->num = 0;
memset(state->ecount, 0, 16);
memcpy(state->ivec, iv, 16);
}
void main(){
unsigned char * cypher = extochar("874d6191b620e3261bef6864990db6ce",32);
unsigned char * key = extochar("2b7e151628aed2a6abf7158809cf4f3c",32);
unsigned char * iv = extochar("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",32);
unsigned char out[256]; //more than needed
AES_KEY aes_key;
int msg_len = 16;
struct ctr_state status;
if (!AES_set_encrypt_key(key, 16, &aes_key)){
printf("key error");
exit(-1);
}
init_ctr(&status, iv);
AES_ctr128_encrypt(cypher, out, msg_len, &aes_key, status.ivec, status.ecount, &status.num);
//expected plaintext: "6bc1bee22e409f96e93d7e117393172a"
}
segmentation fault is on istruction AES_ctr128_encrypt(), the last one, while i'm expecting "6bc1bee22e409f96e93d7e117393172a" as plaintext (using printf("%02x", var[i]) to printf it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 AES 密钥长度为 256 位。您告诉 AES_set_encrypt_key 它是 16 位。然后将密钥传递给 AES_ctr 128 _encrypt。如果这三个数字都相同,您的代码会工作得更好。
Your AES key is 256 bits long. You tell AES_set_encrypt_key that it's 16 bits. And then you pass the key to AES_ctr 128 _encrypt. Your code would work a lot better if all three of those numbers were the same.