
nonce 재사용의 위험성을 완화하기 위한 방안은 다음과 같습니다.
1. Nonce를 매번 생성하기: nonce는 암호화에 사용되는 임의의 값으로, 매번 암호화할 때마다 새로운 nonce를 생성해야 합니다. Sodium 라이브러리는 `sodium_randombytes_buf` 함수를 사용하여 nonce를 생성할 수 있습니다.
2. Nonce 저장하기: nonce를 저장하는 방법으로, 암호화할 때 nonce를 저장하고, 복호화할 때 저장된 nonce를 사용할 수 있습니다. 하지만 nonce를 저장하는 방법은 보안에 취약하므로 주의가 필요합니다.
3. Nonce를 재사용하지 않기: nonce를 재사용하지 않기 위한 구현 방법으로, 암호화할 때마다 새로운 nonce를 생성하고, 복호화할 때 저장된 nonce를 사용하는 방법이 있습니다.
nonce를 재사용하지 않기 위한 구현 예제는 다음과 같습니다.
#hostingforum.kr
c
#include
// nonce를 저장할 변수
unsigned char nonce[SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES];
// 암호화 함수
void encrypt(unsigned char *plaintext, unsigned long long plaintext_len, unsigned char *ciphertext, unsigned long long *ciphertext_len) {
// nonce를 생성하기
sodium_randombytes_buf(nonce, SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
// 암호화하기
unsigned long long ciphertext_len_tmp;
sodium_crypto_aead_aes256gcm_encrypt(nonce, ciphertext_len_tmp, ciphertext, plaintext, plaintext_len, NULL, 0);
// 저장된 nonce를 복호화할 때 사용하기
*ciphertext_len = ciphertext_len_tmp;
}
// 복호화 함수
void decrypt(unsigned char *ciphertext, unsigned long long ciphertext_len, unsigned char *plaintext, unsigned long long *plaintext_len) {
// 저장된 nonce를 복호화할 때 사용하기
unsigned long long ciphertext_len_tmp;
sodium_crypto_aead_aes256gcm_decrypt(nonce, ciphertext_len_tmp, ciphertext, plaintext, NULL, 0);
// 복호화된 데이터의 길이를 저장하기
*plaintext_len = ciphertext_len_tmp;
}
위의 예제는 nonce를 재사용하지 않기 위한 구현 방법을 보여줍니다. 암호화할 때마다 새로운 nonce를 생성하고, 복호화할 때 저장된 nonce를 사용합니다.
2025-08-10 11:44