
sodium_crypto_aead_chacha20poly1305_ietf_encrypt 함수는 AEAD(Authenticated Encryption with Associated Data) 암호화 함수로, nonce의 길이는 24바이트로 고정되어 있습니다.
이 함수는 nonce을 24바이트로 설정해야 하는 이유는 nonce이 중복되면 암호화된 데이터가 동일해지기 때문입니다. nonce이 중복되면 암호화된 데이터가 동일해지기 때문에, nonce의 길이를 24바이트로 설정하는 것이 안전합니다.
하지만, nonce을 12바이트로 생성하여 함수에 전달하는 방법은 없습니다. nonce의 길이는 24바이트로 고정되어 있기 때문에, 12바이트의 nonce을 전달하면 오류가 발생합니다.
nonce을 24바이트로 생성하는 방법은 여러 가지가 있습니다. 예를 들어, nonce을 12바이트로 생성한 후, 12바이트의 데이터를 24바이트로 확장하는 방법이 있습니다.
다음은 예시입니다.
#hostingforum.kr
c
#include
// nonce을 12바이트로 생성
unsigned char nonce[12];
sodium_random(nonce, 12);
// nonce을 24바이트로 확장
unsigned char expanded_nonce[24];
expanded_nonce[0] = nonce[0];
expanded_nonce[1] = nonce[1];
expanded_nonce[2] = nonce[2];
expanded_nonce[3] = nonce[3];
expanded_nonce[4] = nonce[4];
expanded_nonce[5] = nonce[5];
expanded_nonce[6] = nonce[6];
expanded_nonce[7] = nonce[7];
expanded_nonce[8] = nonce[8];
expanded_nonce[9] = nonce[9];
expanded_nonce[10] = nonce[10];
expanded_nonce[11] = nonce[11];
expanded_nonce[12] = 0; // nonce의 12바이트를 확장
expanded_nonce[13] = 0;
expanded_nonce[14] = 0;
expanded_nonce[15] = 0;
expanded_nonce[16] = 0;
expanded_nonce[17] = 0;
expanded_nonce[18] = 0;
expanded_nonce[19] = 0;
expanded_nonce[20] = 0;
expanded_nonce[21] = 0;
expanded_nonce[22] = 0;
expanded_nonce[23] = 0;
// sodium_crypto_aead_chacha20poly1305_ietf_encrypt 함수에 전달
unsigned char ciphertext[message_length];
unsigned long long ciphertext_length = 0;
sodium_crypto_aead_chacha20poly1305_ietf_encrypt(ciphertext, &ciphertext_length, message, message_length, expanded_nonce, 24, key, key_length);
위의 예시는 nonce을 12바이트로 생성한 후, 12바이트의 데이터를 24바이트로 확장하는 방법을 보여줍니다. nonce을 24바이트로 확장한 후, sodium_crypto_aead_chacha20poly1305_ietf_encrypt 함수에 전달합니다.
2025-08-03 16:08