[ja] Translate 09.6.md into Japanese.
This commit is contained in:
122
ja/ebook/09.6.md
Normal file
122
ja/ebook/09.6.md
Normal file
@@ -0,0 +1,122 @@
|
||||
# 9.6 データを暗号化/復元する
|
||||
前の節でどのようにしてパスワードを保存するかご紹介しました。しかしあるときには、慎重に扱うべきデータを暗号化して保存し、将来のあるときにいつでもそれらを復元したい場合があります。この時双方向暗号化アルゴリズムを使って我々の要求を満たさなければなりません。
|
||||
|
||||
## base64で暗号化/復元する
|
||||
もしWebアプリケーションが十分に簡単であれば、データのセキュリティにはそれほど厳格な要求があるわけではありません。ですので比較的簡単な暗号化である`base64`を採用することができます。このような方法は実装するのが比較的簡単で、Go言語の`base64`パッケージではすでにこれをよくサポートしています。下の例をご覧ください:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func base64Encode(src []byte) []byte {
|
||||
return []byte(base64.StdEncoding.EncodeToString(src))
|
||||
}
|
||||
|
||||
func base64Decode(src []byte) ([]byte, error) {
|
||||
return base64.StdEncoding.DecodeString(string(src))
|
||||
}
|
||||
|
||||
func main() {
|
||||
// encode
|
||||
hello := "你好,世界! hello world"
|
||||
debyte := base64Encode([]byte(hello))
|
||||
fmt.Println(debyte)
|
||||
// decode
|
||||
enbyte, err := base64Decode(debyte)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
if hello != string(enbyte) {
|
||||
fmt.Println("hello is not equal to enbyte")
|
||||
}
|
||||
|
||||
fmt.Println(string(enbyte))
|
||||
}
|
||||
|
||||
|
||||
## 高度な暗号化/復元
|
||||
|
||||
Go言語の`crypto`では双方向暗号の高度な暗号化/復元パッケージがあります:
|
||||
|
||||
- `crypto/aes`パッケージ:AES(Advanced Encryption Standard)は、Rijndael暗号化アルゴリズムとも呼ばれます。アメリカの連邦政府が採用しているブロック暗号の標準の一つです。
|
||||
- `crypto/des`パッケージ:DEA(Data Encryption Algorithm)は双方向暗号化アルゴリズムのひとつです。これは現在秘密鍵のシステムに最も広く使用されています。特に金融データのセキュリティの保護で使われています。
|
||||
|
||||
これら2つのアルゴリズムは使用方法が似ていますので、ここでは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")
|
||||
//暗号化された文字列を渡すと、plaintは渡された文字列になります。
|
||||
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つの関数は暗号化/復元操作を実現します。詳細な操作は上の例をご覧ください。
|
||||
|
||||
## サマリー
|
||||
この節ではいくつかの暗号化/復元アルゴリズムをご紹介しました。Webアプリケーションを開発している時は要求に合わせて異なる方法によって暗号化/複合を行うことができます。一般的なアプリケーションではbase64アルゴリズムを採用することができます。より高度な場合はaesやdesアルゴリズムを採用することができます。
|
||||
|
||||
|
||||
## links
|
||||
* [目次](<preface.md>)
|
||||
* 前へ: [パスワードの保存](<09.5.md>)
|
||||
* 次へ: [サマリー](<09.7.md>)
|
||||
Reference in New Issue
Block a user