
sodium_crypto_box_keypair 함수는 Edwards-curve Digital Signature Algorithm (Ed25519) 키 쌍을 생성하는 함수입니다. 이 함수는 두 개의 32바이트의 바이너리 데이터를 반환하는데, 첫 번째 데이터는 공개 키이고 두 번째 데이터는 개인 키입니다.
공개 키와 개인 키는 각각 64자(32바이트)의 문자열로 표현할 수 있습니다. 공개 키는 일반적으로 "public key"로, 개인 키는 일반적으로 "secret key"로 표현됩니다.
공개 키를 생성하는 방법은 다음과 같습니다.
1. sodium_crypto_box_keypair 함수를 호출하여 키 쌍을 생성합니다.
2. 생성된 키 쌍의 두 번째 데이터를 개인 키로 사용합니다.
3. 생성된 키 쌍의 첫 번째 데이터를 공개 키로 사용합니다.
개인 키를 사용하여 암호화하는 방법은 다음과 같습니다.
1. sodium_crypto_box 함수를 호출하여 암호화할 데이터를 암호화합니다.
2. 암호화 함수에 개인 키를 입력합니다.
복호화하는 방법은 다음과 같습니다.
1. sodium_crypto_box_open 함수를 호출하여 암호화된 데이터를 복호화합니다.
2. 복호화 함수에 공개 키를 입력합니다.
예를 들어, 다음 코드는 sodium_crypto_box_keypair 함수를 호출하여 키 쌍을 생성하고, 생성된 키 쌍을 사용하여 암호화와 복호화를 수행합니다.
#hostingforum.kr
c
#include
int main() {
unsigned char public_key[32];
unsigned char secret_key[32];
unsigned char message[32] = "Hello, World!";
unsigned char encrypted_message[32];
unsigned char decrypted_message[32];
if (sodium_crypto_box_keypair(public_key, secret_key) != 0) {
printf("Error generating key pairn");
return 1;
}
if (sodium_crypto_box(encrypted_message, message, 32, public_key, secret_key) != 0) {
printf("Error encrypting messagen");
return 1;
}
if (sodium_crypto_box_open(decrypted_message, encrypted_message, 32, public_key, secret_key) != 0) {
printf("Error decrypting messagen");
return 1;
}
printf("Decrypted message: %sn", decrypted_message);
return 0;
}
이 코드는 sodium_crypto_box_keypair 함수를 호출하여 키 쌍을 생성하고, 생성된 키 쌍을 사용하여 "Hello, World!"라는 메시지를 암호화하고 복호화합니다.
2025-06-13 18:13