[go] Go 언어에서의 대칭키 암호화 방식

이번 포스트에서는 Go 언어를 사용하여 대칭키 암호화를 구현하는 방법에 대해 알아보겠습니다.

1. 대칭키 암호화란?

대칭키 암호화는 하나의 키로 데이터를 암호화하고, 같은 키를 사용하여 암호문을 해독하는 방식입니다. Go 언어에서는 crypto 패키지를 사용하여 대칭키 암호화를 구현할 수 있습니다.

2. 예제 코드

아래는 Go 언어를 사용하여 대칭키 암호화를 구현한 예제 코드입니다.

package main

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"fmt"
	"io"
)

func main() {
	key := []byte("example key 1234") // 16, 24, or 32 bytes
	plaintext := []byte("Hello, world!")

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	ciphertext := make([]byte, aes.BlockSize+len(plaintext))
	iv := ciphertext[:aes.BlockSize]
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		panic(err)
	}

	stream := cipher.NewCFBEncrypter(block, iv)
	stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)

	fmt.Printf("%x\n", ciphertext)

	stream = cipher.NewCFBDecrypter(block, iv)
	plaintextCopy := make([]byte, len(plaintext))
	stream.XORKeyStream(plaintextCopy, ciphertext[aes.BlockSize:])

	fmt.Printf("%s\n", plaintextCopy)
}

3. 결과

위의 예제 코드를 실행하면, 평문이 암호화되고, 다시 복호화되어 평문이 출력될 것입니다.

이제 Go 언어를 사용하여 대칭키 암호화를 구현하는 방법에 대해 알아보았습니다.

4. 참고 자료