
Sodium 라이브러리에서 XChaCha20 스트림 암호화 키를 생성하는 방법은 `crypto_secretstream_xchacha20poly1305_keypair()` 함수를 사용합니다. 이 함수는 키-이베이터 쌍을 생성하며, 이 키-이베이터 쌍을 사용하여 스트림 암호화를 수행할 수 있습니다.
#hostingforum.kr
c
#include
int main() {
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
unsigned char nonce[crypto_secretstream_xchacha20poly1305_NONCEBYTES];
if (crypto_secretstream_xchacha20poly1305_keypair(key, nonce) != 0) {
return 1;
}
// 키와 이베이터를 사용하여 스트림 암호화를 수행합니다.
return 0;
}
데이터의 크기가 큰 경우, 스트림 암호화를 수행할 때는 `crypto_secretstream_xchacha20poly1305_push()` 함수를 사용하여 데이터를 암호화할 수 있습니다. 이 함수는 암호화된 데이터와 태그를 반환하며, 태그를 사용하여 데이터의 무결성을 확인할 수 있습니다.
#hostingforum.kr
c
#include
int main() {
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
unsigned char nonce[crypto_secretstream_xchacha20poly1305_NONCEBYTES];
unsigned char tag[crypto_secretstream_xchacha20poly1305_ADSIZE];
unsigned char ciphertext[crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX];
if (crypto_secretstream_xchacha20poly1305_keypair(key, nonce) != 0) {
return 1;
}
// 데이터를 암호화합니다.
if (crypto_secretstream_xchacha20poly1305_push(&state, "Hello, World!", strlen("Hello, World!"), tag, ciphertext) != 0) {
return 1;
}
// 암호화된 데이터와 태그를 반환합니다.
return 0;
}
스트림 암호화를 수행한 후 데이터를 복호화할 때는 `crypto_secretstream_xchacha20poly1305_pull()` 함수를 사용하여 데이터를 복호화할 수 있습니다. 이 함수는 복호화된 데이터와 태그를 반환하며, 태그를 사용하여 데이터의 무결성을 확인할 수 있습니다.
#hostingforum.kr
c
#include
int main() {
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
unsigned char nonce[crypto_secretstream_xchacha20poly1305_NONCEBYTES];
unsigned char tag[crypto_secretstream_xchacha20poly1305_ADSIZE];
unsigned char ciphertext[crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX];
unsigned char plaintext[crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX];
if (crypto_secretstream_xchacha20poly1305_keypair(key, nonce) != 0) {
return 1;
}
// 데이터를 암호화합니다.
if (crypto_secretstream_xchacha20poly1305_push(&state, "Hello, World!", strlen("Hello, World!"), tag, ciphertext) != 0) {
return 1;
}
// 데이터를 복호화합니다.
if (crypto_secretstream_xchacha20poly1305_pull(&state, ciphertext, tag, plaintext) != 0) {
return 1;
}
// 복호화된 데이터를 반환합니다.
return 0;
}
2025-08-06 19:21