1
저는 openssl의 전문가가 아닙니다. 다음 코드를 조합하여 AES-CTR을 사용하여 메시지를 암호화하고 해독합니다. 출력은 내가 기대하는 것이 아닙니다.AES CTR 대칭 암호화 및 해독
#include "stdafx.h"
#include <openssl/aes.h>
#include <openssl/evp.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <psapi.h>
#include <openssl/rand.h> //for RAND_bytes function
struct ctr_state {
unsigned char ivec[16]; /* ivec[0..7] is the IV, ivec[8..15] is the big-endian counter */
unsigned int num;
unsigned char ecount[16];
};
int init_ctr(struct ctr_state *state, const unsigned char iv[8])
{
/* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
* first call. */
state->num = 0;
memset(state->ecount, 0, 16);
/* Initialise counter in 'ivec' to 0 */
memset(state->ivec + 8, 0, 8);
/* Copy IV into 'ivec' */
memcpy(state->ivec, iv, 8);
return(0);
}
int main(int argc, char **argv)
{
unsigned char key[] = "thiskeyisverybad"; // It is 128bits though..
unsigned char iv[8];
struct ctr_state state;
if (!RAND_bytes(iv, 8))
printf("\nError in RAND_Bytes...\n");
init_ctr(&state, iv);
AES_KEY aes_key;
AES_set_encrypt_key(key, 128, &aes_key);
char msg[] = "hey";
unsigned char cipher[AES_BLOCK_SIZE];
char plain[AES_BLOCK_SIZE];
AES_ctr128_encrypt((unsigned char *) msg, cipher, AES_BLOCK_SIZE, &aes_key, state.ivec, state.ecount, &state.num);
AES_ctr128_encrypt(cipher, (unsigned char *) plain, AES_BLOCK_SIZE, &aes_key, state.ivec, state.ecount, &state.num);
printf("\nPLAIN:%s\n", plain);
return 0;
}
내가 얻고 그 결과이 같은 것입니다 : "PLAIN : ¢ u∩U└ ■ 내 & nu9 ♫ _╠╠╠╠╠╠╠╠"Åä▬♂☻e0T ç§▓ → ♀ v╠╠╠╠╠╠╠╠hey "
나는 무엇을 할까? CTR을 사용하여 메시지를 암호화하고 해독하는 AES를 사용하고 싶습니다. 평문과 동일한 암호화 된 길이를 얻고 싶습니다. (또는 +1 바이트). DES를 사용했지만 DES는 안전하지 않습니다. 그런 다음 AES-CTR을 사용하여 네트워크 트래픽 (스트림)을 암호화하고 해독합니다.