Skip to content

TEA

TEA(Tiny Encryption Algorithm)はシンプルで高効率なブロック暗号アルゴリズムで、固定長 16 バイト鍵を使用してデータの暗号化と復号化を行います。dongle は標準およびストリーミング TEA 暗号化をサポートし、多様なブロックモード、パディングモード、出力形式を提供します。

サポートされているブロックモード:

  • CBC(Cipher Block Chaining):暗号ブロック連鎖モード、鍵 Key、初期化ベクトル IV(8バイト)、パディングモード Padding の設定が必要
  • ECB(Electronic Codebook):電子コードブックモード、鍵 Key とパディングモード Padding の設定が必要
  • CTR(Counter):カウンターモード、鍵 Key と初期化ベクトル IV(8バイト)の設定が必要
  • CFB(Cipher Feedback):暗号フィードバックモード、鍵 Key と初期化ベクトル IV(8バイト)の設定が必要
  • OFB(Output Feedback):出力フィードバックモード、鍵 Key と初期化ベクトル IV(8バイト)の設定が必要

注意:TEAアルゴリズムは GCM(Galois/Counter Mode)モードをサポートしていません。これは GCM モードが暗号アルゴリズムに 128 ビットブロックサイズを要求するのに対し、TEA64 ビットブロックサイズ(8 バイト)しか持たないためです。これは暗号学標準の技術的制限であり、実装の問題ではありません。

サポートされているパディングモード:

  • No:パディングなし、平文長は8の倍数でなければならない
  • Zero:ゼロパディング、ブロック境界までゼロバイトでパディング、平文長が8の倍数でない場合は0x00バイトでパディング
  • PKCS7:PKCS#7パディング、最も一般的に使用されるパディング方式、N個の値Nのバイトでパディング、Nはパディングバイト数
  • PKCS5:PKCS#5パディング、8バイトブロックサイズに適用、N個の値Nのバイトでパディング、Nはパディングバイト数
  • AnsiX923:ANSI X.923パディング、最後のバイト以外は0x00でパディング、最後のバイトはパディングバイト数を示す
  • ISO97971:ISO/IEC 9797-1パディング、最初のバイトは0x80、残りは0x00でパディング
  • ISO10126:ISO/IEC 10126パディング、最後のバイト以外はランダムバイトでパディング、最後のバイトはパディングバイト数を示す
  • ISO78164:ISO/IEC 7816-4パディング、最初のバイトは0x80、残りは0x00でパディング
  • Bit:ビットパディング、平文末尾に1ビットを追加し、ブロック境界まで0ビットでパディング
  • TBC:末尾ビット補数パディング、最後のデータバイトの最上位ビットに基づいてパディングバイトを決定(MSB=0は0x00、MSB=1は0xFFを使用)

注意CBC/ECBブロックモードのみパディングモードの設定が必要、CBC/CTR/CFB/OFBブロックモードのみ初期化ベクトルの設定が必要

関連モジュールをインポート:

go
import (
    "github.com/dromara/dongle"
    "github.com/dromara/dongle/crypto/cipher"
)

CBCモード

Cipherの作成

go
c := cipher.NewTeaCipher(cipher.CBC)
// 鍵を設定(必ず16バイト)
c.SetKey([]byte("dongle1234567890"))
// 初期化ベクトルを設定(8バイト)
c.SetIV([]byte("87654321"))
// パディングモードを設定
c.SetPadding(cipher.PKCS7)
// ラウンド数を設定(オプション、デフォルト64ラウンド)
c.SetRounds(64)

データの暗号化

入力データ

go
// 入力文字列
encrypter := dongle.Encrypt.FromString("hello world").ByTea(c)
// 入力バイトスライス
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByTea(c)
// 入力ファイルストリーム
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByTea(c)

// 暗号化エラーをチェック
if encrypter.Error != nil {
	fmt.Printf("暗号化エラー: %v\n", encrypter.Error)
	return
}

出力データ

go
// Hexエンコード文字列を出力
encrypter.ToHexString() // a97fc8fdda9bebc7
// Hexエンコードバイトスライスを出力
encrypter.ToHexBytes()  // []byte("a97fc8fdda9bebc7")

// Base64エンコード文字列を出力
encrypter.ToBase64String() // qX/I/dqb68c=
// Base64エンコードバイトスライスを出力
encrypter.ToBase64Bytes()  // []byte("qX/I/dqb68c=")

// エンコードされていない生の文字列を出力
encrypter.ToRawString()
// エンコードされていない生のバイトスライスを出力
encrypter.ToRawBytes()

データの復号化

入力データ

go
// Hexエンコード文字列を入力
decrypter := dongle.Decrypt.FromHexString(hexString).ByTea(c)
// Hexエンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByTea(c)
// Hexエンコードファイルストリームを入力
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByTea(c)

// Base64エンコード文字列を入力
decrypter := dongle.Decrypt.FromBase64String(base64String).ByTea(c)
// Base64エンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByTea(c)
// Base64エンコードファイルストリームを入力
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByTea(c)

// エンコードされていない生の文字列を入力
decrypter := dongle.Decrypt.FromRawString(rawString).ByTea(c)
// エンコードされていない生のバイトスライスを入力
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByTea(c)
// エンコードされていない生のファイルストリームを入力
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByTea(c)

// 復号化エラーをチェック
if decrypter.Error != nil {
	fmt.Printf("復号化エラー: %v\n", decrypter.Error)
	return
}

出力データ

go
// 復号化後の文字列を出力
decrypter.ToString() // hello world
// 復号化後のバイトスライスを出力
decrypter.ToBytes()  // []byte("hello world")

ECBモード

Cipherの作成

go
c := cipher.NewTeaCipher(cipher.ECB)
// 鍵を設定(必ず16バイト)
c.SetKey([]byte("dongle1234567890"))
// パディングモードを設定
c.SetPadding(cipher.PKCS7)
// ラウンド数を設定(オプション、デフォルト64ラウンド)
c.SetRounds(64)

データの暗号化

入力データ

go
// 入力文字列
encrypter := dongle.Encrypt.FromString("hello world").ByTea(c)
// 入力バイトスライス
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByTea(c)
// 入力ファイルストリーム
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByTea(c)

// 暗号化エラーをチェック
if encrypter.Error != nil {
	fmt.Printf("暗号化エラー: %v\n", encrypter.Error)
	return
}

出力データ

go
// Hexエンコード文字列を出力
encrypter.ToHexString() // 2b4e8f1a5c7d9e3f6a8b2c4d5e7f9a1b
// Hexエンコードバイトスライスを出力
encrypter.ToHexBytes()  // []byte("2b4e8f1a5c7d9e3f6a8b2c4d5e7f9a1b")

// Base64エンコード文字列を出力
encrypter.ToBase64String() // K06PGsdfnj+aqyzUXn+aGw==
// Base64エンコードバイトスライスを出力
encrypter.ToBase64Bytes()  // []byte("K06PGsdfnj+aqyzUXn+aGw==")

// エンコードされていない生の文字列を出力
encrypter.ToRawString()
// エンコードされていない生のバイトスライスを出力
encrypter.ToRawBytes()

データの復号化

入力データ

go
// Hexエンコード文字列を入力
decrypter := dongle.Decrypt.FromHexString(hexString).ByTea(c)
// Hexエンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByTea(c)
// Hexエンコードファイルストリームを入力
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByTea(c)

// Base64エンコード文字列を入力
decrypter := dongle.Decrypt.FromBase64String(base64String).ByTea(c)
// Base64エンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByTea(c)
// Base64エンコードファイルストリームを入力
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByTea(c)

// エンコードされていない生の文字列を入力
decrypter := dongle.Decrypt.FromRawString(rawString).ByTea(c)

// エンコードされていない生のバイトスライスを入力
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByTea(c)

// エンコードされていない生のファイルストリームを入力
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByTea(c)

// 復号化エラーをチェック
if decrypter.Error != nil {
	fmt.Printf("復号化エラー: %v\n", decrypter.Error)
	return
}

出力データ

go
// 復号化後の文字列を出力
decrypter.ToString() // hello world
// 復号化後のバイトスライスを出力
decrypter.ToBytes()  // []byte("hello world")

CTRモード

Cipherの作成

go
c := cipher.NewTeaCipher(cipher.CTR)
// 鍵を設定(必ず16バイト)
c.SetKey([]byte("dongle1234567890"))
// 初期化ベクトルを設定(8バイト)
c.SetIV([]byte("87654321"))
// ラウンド数を設定(オプション、デフォルト64ラウンド)
c.SetRounds(64)

データの暗号化

入力データ

go
// 入力文字列
encrypter := dongle.Encrypt.FromString("hello world").ByTea(c)
// 入力バイトスライス
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByTea(c)
// 入力ファイルストリーム
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByTea(c)

// 暗号化エラーをチェック
if encrypter.Error != nil {
	fmt.Printf("暗号化エラー: %v\n", encrypter.Error)
	return
}

出力データ

go
// Hexエンコード文字列を出力
encrypter.ToHexString() // 7f3a9b2e4d6c8f1a5e7b9c3d4f6a8b2e
// Hexエンコードバイトスライスを出力
encrypter.ToHexBytes()  // []byte("7f3a9b2e4d6c8f1a5e7b9c3d4f6a8b2e")

// Base64エンコード文字列を出力
encrypter.ToBase64String() // fzqbLk1sjxpeec09T2qLLg==
// Base64エンコードバイトスライスを出力
encrypter.ToBase64Bytes()  // []byte("fzqbLk1sjxpeec09T2qLLg==")

// エンコードされていない生の文字列を出力
encrypter.ToRawString()
// エンコードされていない生のバイトスライスを出力
encrypter.ToRawBytes()

データの復号化

入力データ

go
// Hexエンコード文字列を入力
decrypter := dongle.Decrypt.FromHexString(hexString).ByTea(c)
// Hexエンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByTea(c)
// Hexエンコードファイルストリームを入力
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByTea(c)

// Base64エンコード文字列を入力
decrypter := dongle.Decrypt.FromBase64String(base64String).ByTea(c)
// Base64エンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByTea(c)
// Base64エンコードファイルストリームを入力
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByTea(c)

// エンコードされていない生の文字列を入力
decrypter := dongle.Decrypt.FromRawString(rawString).ByTea(c)
// エンコードされていない生のバイトスライスを入力
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByTea(c)
// エンコードされていない生のファイルストリームを入力
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByTea(c)

// 復号化エラーをチェック
if decrypter.Error != nil {
	fmt.Printf("復号化エラー: %v\n", decrypter.Error)
	return
}

出力データ

go
// 復号化後の文字列を出力
decrypter.ToString() // hello world
// 復号化後のバイトスライスを出力
decrypter.ToBytes()  // []byte("hello world")

CFBモード

注意:CFBモードはCFB8実装を使用します。最初の16バイトのデータの場合、CFB8とOFBモードは同じ暗号化結果を生成します。これはGo標準ライブラリCFB8実装の特性であり、バグではありません。

Cipherの作成

go
c := cipher.NewTeaCipher(cipher.CFB)
// 鍵を設定(必ず16バイト)
c.SetKey([]byte("dongle1234567890"))
// 初期化ベクトルを設定(8バイト)
c.SetIV([]byte("87654321"))
// ラウンド数を設定(オプション、デフォルト64ラウンド)
c.SetRounds(64)

データの暗号化

入力データ

go
// 入力文字列
encrypter := dongle.Encrypt.FromString("hello world").ByTea(c)
// 入力バイトスライス
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByTea(c)
// 入力ファイルストリーム
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByTea(c)

// 暗号化エラーをチェック
if encrypter.Error != nil {
	fmt.Printf("暗号化エラー: %v\n", encrypter.Error)
	return
}

出力データ

go
// Hexエンコード文字列を出力
encrypter.ToHexString() // 5a8c3f1e7b4d9a2c6e8f1b5d3a7c9e2f
// Hexエンコードバイトスライスを出力
encrypter.ToHexBytes()  // []byte("5a8c3f1e7b4d9a2c6e8f1b5d3a7c9e2f")

// Base64エンコード文字列を出力
encrypter.ToBase64String() // WowPHntNmi5ujxtaPHyf
// Base64エンコードバイトスライスを出力
encrypter.ToBase64Bytes()  // []byte("WowPHntNmi5ujxtaPHyf")

// エンコードされていない生の文字列を出力
encrypter.ToRawString()
// エンコードされていない生のバイトスライスを出力
encrypter.ToRawBytes()

データの復号化

入力データ

go
// Hexエンコード文字列を入力
decrypter := dongle.Decrypt.FromHexString(hexString).ByTea(c)
// Hexエンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByTea(c)
// Hexエンコードファイルストリームを入力
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByTea(c)

// Base64エンコード文字列を入力
decrypter := dongle.Decrypt.FromBase64String(base64String).ByTea(c)
// Base64エンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByTea(c)
// Base64エンコードファイルストリームを入力
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByTea(c)

// エンコードされていない生の文字列を入力
decrypter := dongle.Decrypt.FromRawString(rawString).ByTea(c)
// エンコードされていない生のバイトスライスを入力
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByTea(c)
// エンコードされていない生のファイルストリームを入力
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByTea(c)

// 復号化エラーをチェック
if decrypter.Error != nil {
	fmt.Printf("復号化エラー: %v\n", decrypter.Error)
	return
}

出力データ

go
// 復号化後の文字列を出力
decrypter.ToString() // hello world
// 復号化後のバイトスライスを出力
decrypter.ToBytes()  // []byte("hello world")

OFBモード

注意:CFBモードはCFB8実装を使用します。最初の16バイトのデータの場合、CFB8とOFBモードは同じ暗号化結果を生成します。これはGo標準ライブラリCFB8実装の特性であり、バグではありません。

Cipherの作成

go
c := cipher.NewTeaCipher(cipher.OFB)
// 鍵を設定(必ず16バイト)
c.SetKey([]byte("dongle1234567890"))
// 初期化ベクトルを設定(8バイト)
c.SetIV([]byte("87654321"))
// ラウンド数を設定(オプション、デフォルト64ラウンド)
c.SetRounds(64)

データの暗号化

入力データ

go
// 入力文字列
encrypter := dongle.Encrypt.FromString("hello world").ByTea(c)
// 入力バイトスライス
encrypter := dongle.Encrypt.FromBytes([]byte("hello world")).ByTea(c)
// 入力ファイルストリーム
file, _ := os.Open("test.txt")
encrypter := dongle.Encrypt.FromFile(file).ByTea(c)

// 暗号化エラーをチェック
if encrypter.Error != nil {
	fmt.Printf("暗号化エラー: %v\n", encrypter.Error)
	return
}

出力データ

go
// Hexエンコード文字列を出力
encrypter.ToHexString() // 3f7a9c2e5d8b1f4a6e9c3d7b2f5a8e1c
// Hexエンコードバイトスライスを出力
encrypter.ToHexBytes()  // []byte("3f7a9c2e5d8b1f4a6e9c3d7b2f5a8e1c")

// Base64エンコード文字列を出力
encrypter.ToBase64String() // P3qcLl2LH0puPD17L1qOHA==
// Base64エンコードバイトスライスを出力
encrypter.ToBase64Bytes()   // []byte("P3qcLl2LH0puPD17L1qOHA==")

// エンコードされていない生の文字列を出力
encrypter.ToRawString()
// エンコードされていない生のバイトスライスを出力
encrypter.ToRawBytes()

データの復号化

入力データ

go
// Hexエンコード文字列を入力
decrypter := dongle.Decrypt.FromHexString(hexString).ByTea(c)
// Hexエンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromHexBytes(hexBytes).ByTea(c)
// Hexエンコードファイルストリームを入力
file, _ := os.Open("encrypted.hex")
decrypter := dongle.Decrypt.FromHexFile(file).ByTea(c)

// Base64エンコード文字列を入力
decrypter := dongle.Decrypt.FromBase64String(base64String).ByTea(c)
// Base64エンコードバイトスライスを入力
decrypter := dongle.Decrypt.FromBase64Bytes(base64Bytes).ByTea(c)
// Base64エンコードファイルストリームを入力
file, _ := os.Open("encrypted.base64")
decrypter := dongle.Decrypt.FromBase64File(file).ByTea(c)

// エンコードされていない生の文字列を入力
decrypter := dongle.Decrypt.FromRawString(rawString).ByTea(c)
// エンコードされていない生のバイトスライスを入力
decrypter := dongle.Decrypt.FromRawBytes(rawBytes).ByTea(c)
// エンコードされていない生のファイルストリームを入力
file, _ := os.Open("encrypted.bin")
decrypter := dongle.Decrypt.FromRawFile(file).ByTea(c)

// 復号化エラーをチェック
if decrypter.Error != nil {
	fmt.Printf("復号化エラー: %v\n", decrypter.Error)
	return
}

出力データ

go
// 復号化後の文字列を出力
decrypter.ToString() // hello world
// 復号化後のバイトスライスを出力
decrypter.ToBytes()  // []byte("hello world")

MITライセンスに基づいて公開されており、許可なく複製することは禁止されています