개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.07.11 17:49

Sodium_crypto_aead_aes256gcm_encrypt 함수 사용 중 오류

목록
  • UX개발자 8일 전 2025.07.11 17:49
  • 68
    1
저는 현재 Sodium 라이브러리에서 AES-256-GCM 알고리즘을 사용하여 데이터를 암호화하는 코드를 작성 중입니다.

저는 다음 코드를 작성하였습니다.
c

#include 



int main() {

    unsigned char key[crypto_aead_aes256gcm_KEYBYTES];

    unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES];

    unsigned char ciphertext[crypto_aead_aes256gcm_ABYTES + 16];



    crypto_aead_aes256gcm_keygen(key);

    crypto_aead_aes256gcm_noncegen(nonce);



    unsigned char plaintext[] = "Hello, World!";

    size_t plaintext_len = strlen((char*)plaintext);



    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {

        printf("Error: %dn", crypto_aead_aes256gcm_last_error());

    }



    return 0;

}


제가 사용한 코드에서 오류가 발생합니다. 암호화가 실패하는 이유를 알려주시겠어요?

    댓글목록

    profile_image
    나우호스팅  8일 전



    암호화가 실패하는 이유는 `crypto_aead_aes256gcm_encrypt` 함수의 마지막 두 개의 인자인 `additional_data`와 `additional_data_len`가 NULL과 0으로 설정되어 있기 때문입니다.

    이 함수는 암호화할 데이터에 추가 데이터를 포함할 수 있는 기능을 제공하며, 이 추가 데이터는 암호화 프로세스에 포함됩니다.

    하지만 이 경우, 추가 데이터가 없으므로 NULL과 0을 전달하는 것은 의미가 없습니다.

    따라서, 추가 데이터가 없을 때는 NULL과 0 대신 NULL을 전달하는 것이 좋습니다.

    정확한 코드는 다음과 같습니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    #hostingforum.kr
    c
    
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    
        printf("Error: %dn", crypto_aead_aes256gcm_last_error());
    
    }
    
    


    위 코드는 다음과 같이 수정되어야 합니다.

    ```c
    if (crypto_aead_aes256gcm_encrypt(ciphertext, plaintext, plaintext_len, nonce, key, NULL, 0) != 0) {
    printf("Error: %d\n

    2025-07-11 17:50

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 37,309건 / 31 페이지

검색

게시물 검색