0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Go】XOR暗号を使用した暗号化、復号化のサンプルコード

Last updated at Posted at 2025-03-21

概要

暗号技術入門 第3版に出てきたXOR暗号で暗号化、復号化をGoで実装してみた

サンプルコード

package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
)

// XOR暗号化・復号化を行う関数
// data: 暗号化または復号化する平文または暗号文
// key: 使用する鍵
// XOR演算を適用し、暗号化または復号化した結果を返す
func xorEncryptDecrypt(data, key string) string {
	dataBytes := []byte(data)              // 平文または暗号文をバイト列に変換
	keyBytes := []byte(key)                // 鍵をバイト列に変換
	output := make([]byte, len(dataBytes)) // 出力用のバイト列を準備

	keyLength := len(keyBytes) // 鍵の長さを取得

	// XOR演算を各バイトに対して適用
	for i := 0; i < len(dataBytes); i++ {
		output[i] = dataBytes[i] ^ keyBytes[i%keyLength] // i番目のデータバイトと鍵バイトをXOR
	}

	// 結果を文字列として返す
	return string(output)
}

// 文字列をバイナリ形式の文字列に変換
func toBinaryString(data string) string {
	binaryStr := ""
	for _, c := range data {
		binaryStr += fmt.Sprintf("%08b ", c)
	}
	return binaryStr
}

// 平文と同じ長さの鍵を生成する関数
func generateRandomKey(length int) (string, error) {
	key := make([]byte, length)

	_, err := rand.Read(key)
	if err != nil {
		return "", err
	}

	return string(key), nil
}

func main() {
	plaintext := "HelloWorld"
	key, err := generateRandomKey(len(plaintext)) // 平文と同じ長さの鍵を生成
	if err != nil {
		fmt.Println("ランダム鍵の生成に失敗しました:", err)
		return
	}

	fmt.Printf("平文: %s\n", plaintext)
	fmt.Printf("暗号文: %s\n", hex.EncodeToString([]byte(encrypted)))

	// 復号化は暗号化と同じ操作を行う(XORは可逆操作)
	decrypted := xorEncryptDecrypt(encrypted, key)
	fmt.Printf("復号文: %s\n", decrypted)
}

// 出力
// 平文 : HelloWorld
// 暗号文: 294fd36b7622b728ab71
// 復号文: HelloWorld

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?