AES_ctr128_encrypt字符串,反之亦然ANSI C [英] AES_ctr128_encrypt string and vice versa ANSI C
本文介绍了AES_ctr128_encrypt字符串,反之亦然ANSI C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要插入一个128位十六进制的密钥来加密(16)一个字符串内容,然后我想将该字符串(crypted)解密为纯文本。
有时我有相同的输出,但并不总是正确的!我正在做这件事吗?
我现在的代码:
#include< openssl / aes.h>
#include< openssl / rand.h>
#include< openssl / hmac.h>
#include< openssl / buffer.h>
#include< stdio.h>
#include< string.h>
#include< stdlib.h>
#include< math.h>
//代码示例使用partail代码:http://stackoverflow.com/questions/3141860/aes-ctr-256-encryption-mode-of-operation-on-openssl
//大部分在ctr_状态和init_ctr函数中。
struct ctr_state
{
unsigned char ivec [AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount [AES_BLOCK_SIZE];
};
AES_KEY键;
int bytes_read,bytes_written;
unsigned char indata [AES_BLOCK_SIZE];
unsigned char outdata [AES_BLOCK_SIZE];
unsigned char iv [AES_BLOCK_SIZE]; // 16?
struct ctr_state state;
int init_ctr(struct ctr_state * state,const unsigned char iv [16])
{
/ * O aes_ctr128_encrypt exige um'num'e um'ecount 'definidos一个零na齐拉chamada。 * /
state-> num = 0;
memset(state-> ecount,0,AES_BLOCK_SIZE); // 16?
/ *Inicilaizaçãodo contador no'ivec'a 0 * /
memset(state-> ecount,0,16); // 16?
/ * Copia o IV para o'ivec'* /
memcpy(state-> ivec,iv,16); // 16?
}
char * TextEncrypt(const unsigned char * enc_key,char * text)
{
// Cria vector com valoresaleatórios
if(! RAND_bytes(iv,AES_BLOCK_SIZE))
{
printf(Erro:Nãofoi possivel criar bytes aleatorios.\\\
);
exit(1);
}
// Inicializa a chave deencriptação
if(AES_set_encrypt_key(enc_key,128,& key)< 0)
{
fprintf (stderr,Nao foipossíveldefinir chave de encrefacao);
exit(1);
}
init_ctr(& state,iv); // Chamada do contador
bytes_read = strlen(text);
AES_set_encrypt_key(enc_key,128,& key);
// Encripta em blocos de 16 bytes e guarda o texto cifrado numa string - > outdata
AES_ctr128_encrypt(text,outdata,bytes_read,& key,state.ivec,state.ecount,& state.num);
fflush(stdin);
返回outdata;
}
char * TextDecrypt(const unsigned char * enc_key,unsigned char * cypherText)
{
//Inicializaçãoda Chave deencriptação
if(AES_set_encrypt_key(enc_key,128,& key)< 0)
{
fprintf(stderr,Nao foi possivel definir chave de decodificacao);
exit(1);
}
init_ctr(& state,iv); // Chamada do contador
// Encripta em blocos de 16 bytes e escreve o ficheiro output.txt cifrado
bytes_read = strlen(cypherText);
AES_set_encrypt_key(enc_key,128,& key);
AES_ctr128_encrypt(cypherText,outdata,bytes_read,& key,state.ivec,state.ecount,& state.num);
fflush(stdin);
返回outdata;
}
int main(int argc,char * argv [])
{
char text [] =我的程序的文本测试;
//接收十六进制128位密钥
unsigned const char * key =1234567812345678;
// unsigned const char * key =9EF4BCDE;
char * cipher,*解密;
printf(清洁文本:%s\\\
,文本);
cipher = TextEncrypt(key,text);
printf(Chiper文本:%s\\\
,cipher);
decryptpted = TextDecrypt(key,cipher);
printf(解密文本:%s\\\
,解密);
getc(stdin);
return 0;
}
有人可以帮忙吗?谢谢。
解决方案
您正在CipherText上使用strlen(),它可能包含空字节。这将导致bytes_read小于要解密的数据的实际长度。
I need to insert a 128 hex key to cipher by blocks (16) a string content, and then I want to decrypyt that string(crypted) to a plain text.
Sometimes I have the same output, but not totaly correct! I am doing this right?!
My code now:
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <openssl/buffer.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
// Code example uses partail code from: http://stackoverflow.com/questions/3141860/aes-ctr-256-encryption-mode-of-operation-on-openssl
// Mostly in the ctr_ state, and init_ctr functions.
struct ctr_state
{
unsigned char ivec[AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount[AES_BLOCK_SIZE];
};
AES_KEY key;
int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE]; //16?
struct ctr_state state;
int init_ctr(struct ctr_state *state, const unsigned char iv[16])
{
/*O aes_ctr128_encrypt exige um 'num' e um 'ecount' definidos a zero na primeira chamada. */
state->num = 0;
memset(state->ecount, 0, AES_BLOCK_SIZE); //16?
/* Inicilaização do contador no 'ivec' a 0 */
memset(state->ecount, 0, 16); //16?
/* Copia o IV para o 'ivec' */
memcpy(state->ivec, iv, 16); //16?
}
char * TextEncrypt(const unsigned char* enc_key, char * text)
{
//Cria vector com valores aleatórios
if(!RAND_bytes(iv, AES_BLOCK_SIZE))
{
printf("Erro: Não foi possivel criar bytes aleatorios.\n");
exit(1);
}
//Inicializa a chave de encriptação
if (AES_set_encrypt_key(enc_key, 128, &key) < 0)
{
fprintf(stderr, "Nao foi possível definir chave de encriptacao.");
exit(1);
}
init_ctr(&state, iv); //Chamada do contador
bytes_read = strlen(text);
AES_set_encrypt_key(enc_key, 128, &key);
//Encripta em blocos de 16 bytes e guarda o texto cifrado numa string -> outdata
AES_ctr128_encrypt(text, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);
fflush(stdin);
return outdata;
}
char * TextDecrypt(const unsigned char* enc_key, unsigned char* cypherText)
{
//Inicialização da Chave de encriptação
if (AES_set_encrypt_key(enc_key, 128, &key) < 0)
{
fprintf(stderr, "Nao foi possível definir chave de decodificacao.");
exit(1);
}
init_ctr(&state, iv);//Chamada do contador
//Encripta em blocos de 16 bytes e escreve o ficheiro output.txt cifrado
bytes_read = strlen(cypherText);
AES_set_encrypt_key(enc_key, 128, &key);
AES_ctr128_encrypt(cypherText, outdata, bytes_read, &key, state.ivec, state.ecount, &state.num);
fflush(stdin);
return outdata;
}
int main(int argc, char *argv[])
{
char text [] = "Text test to my program";
//Receive hexadecimal 128 bits key
unsigned const char * key = "1234567812345678";
//unsigned const char * key = "9EF4BCDE";
char * cipher, * decrypted;
printf("Clean text: %s\n", text);
cipher = TextEncrypt(key, text);
printf("Chiper text: %s\n", cipher);
decrypted = TextDecrypt(key, cipher);
printf("Decrypted text: %s\n", decrypted);
getc(stdin);
return 0;
}
Can anybody help, please?! Thanks.
解决方案
You are using strlen() on your CipherText, which may contain null bytes. This will cause bytes_read to be less than the actual length of the data you want to decrypt.
这篇关于AES_ctr128_encrypt字符串,反之亦然ANSI C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文