Files
Yoshiyuki Hirano c49c78f42c [ja] fix typo
2017-03-26 02:15:42 +09:00

86 lines
4.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 9.6 データを暗号化/復号する
前の節では安全なパスワードの保存の仕方を説明してきました。しかしあるときには、既にデータベースに保存されている、プライバシーに関わる暗号化されたデータを修正する必要があるかもしれません。データを復号することが必要な時は、既に述べた1方向ハッシュ関数の代わりに、対称鍵暗号アルゴリズムを使うべきです。
## 高度な暗号化/復号
Go言語の`crypto`では対称鍵暗号アルゴリズムをサポートしています。二種類の高度暗号化モジュールがあります。
- `crypto/aes`パッケージ:AES(Advanced Encryption Standard)は、Rijndael暗号化アルゴリズムとも呼ばれます。アメリカの連邦政府が採用しているブロック暗号の標準です。
- `crypto/des`パッケージDES(Data Encryption Standard)は対称鍵暗号の標準です。これは現在最も広く使用されている鍵システムです。特に金融データのセキュリティの保護で使われています。かつてアメリカ連邦政府の暗号化のスタンダードでしたがすでにAESにとってかわられています。
これらつのアルゴリズムは使用方法が似ていますので、以下ではaesパッケージを使ってどのようにこれらのパッケージを使うのかを説明していきたいと思います。
package main
import (
"crypto/aes"
"crypto/cipher"
"fmt"
"os"
)
var commonIV = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}
func main() {
// 暗号化したい文字列
plaintext := []byte("My name is Astaxie")
// 暗号化された文字列を渡すと、plaintextは渡された文字列になります。
if len(os.Args) > 1 {
plaintext = []byte(os.Args[1])
}
// aesの暗号化文字列
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
if len(os.Args) > 2 {
key_text = os.Args[2]
}
fmt.Println(len(key_text))
// 暗号化アルゴリズムaesを作成
c, err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s", len(key_text), err)
os.Exit(-1)
}
// 暗号化文字列
cfb := cipher.NewCFBEncrypter(c, commonIV)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
fmt.Printf("%s=>%x\n", plaintext, ciphertext)
// 復号文字列
cfbdec := cipher.NewCFBDecrypter(c, commonIV)
plaintextCopy := make([]byte, len(plaintext))
cfbdec.XORKeyStream(plaintextCopy, ciphertext)
fmt.Printf("%x=>%s\n", ciphertext, plaintextCopy)
}
上では`aes.NewCipher`(引数keyはかならず16、24または32桁の[]byteとなります。それぞれAES-128, AES-192とAES-256アルゴリズムに対応します。)関数をコールすると`cipher.Block`インターフェースを返します。このインターフェースは3つの機能を実現します:
type Block interface {
// BlockSize returns the cipher's block size.
BlockSize() int
// Encrypt encrypts the first block in src into dst.
// Dst and src may point at the same memory.
Encrypt(dst, src []byte)
// Decrypt decrypts the first block in src into dst.
// Dst and src may point at the same memory.
Decrypt(dst, src []byte)
}
この3つの関数は暗号化/復号操作を実装します。詳細な操作はGoのドキュメントをご覧ください。
## まとめ
この節ではいくつかの暗号化アルゴリズムを紹介しました。これらのアルゴリズムは、Webアプリケーションにおける暗号化/復号の必要に応じて異なる方法で使用することができます。基本的な安全性のみを必要とするアプリケーションではAESアルゴリズムを使うことが勧められます。
## links
* [目次](<preface.md>)
* 前へ: [パスワードの保存](<09.5.md>)
* 次へ: [まとめ](<09.7.md>)