github.com/emmansun/gmsm@v0.29.1/cipher/example_test.go (about)

     1  package cipher_test
     2  
     3  import (
     4  	"crypto/aes"
     5  	"encoding/hex"
     6  	"fmt"
     7  
     8  	"github.com/emmansun/gmsm/cipher"
     9  )
    10  
    11  func ExampleNewECBEncrypter() {
    12  	// Load your secret key from a safe place and reuse it across multiple
    13  	// NewCipher calls. (Obviously don't use this example key for anything
    14  	// real.) If you want to convert a passphrase to a key, use a suitable
    15  	// package like bcrypt or scrypt.
    16  	key, _ := hex.DecodeString("6368616e676520746869732070617373")
    17  	plaintext := []byte("exampleplaintextexampleplaintext")
    18  
    19  	// ECB mode works on blocks so plaintexts may need to be padded to the
    20  	// next whole block. For an example of such padding, see
    21  	// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. Here we'll
    22  	// assume that the plaintext is already of the correct length.
    23  	if len(plaintext)%aes.BlockSize != 0 {
    24  		panic("plaintext is not a multiple of the block size")
    25  	}
    26  
    27  	block, err := aes.NewCipher(key)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  
    32  	ciphertext := make([]byte, len(plaintext))
    33  	mode := cipher.NewECBEncrypter(block)
    34  	mode.CryptBlocks(ciphertext, plaintext)
    35  
    36  	// It's important to remember that ciphertexts must be authenticated
    37  	// (i.e. by using crypto/hmac) as well as being encrypted in order to
    38  	// be secure.
    39  
    40  	fmt.Printf("%x\n", ciphertext)
    41  }
    42  
    43  func ExampleNewECBDecrypter() {
    44  	// Load your secret key from a safe place and reuse it across multiple
    45  	// NewCipher calls. (Obviously don't use this example key for anything
    46  	// real.) If you want to convert a passphrase to a key, use a suitable
    47  	// package like bcrypt or scrypt.
    48  	key, _ := hex.DecodeString("6368616e676520746869732070617373")
    49  	ciphertext, _ := hex.DecodeString("f42512e1e4039213bd449ba47faa1b74f42512e1e4039213bd449ba47faa1b74")
    50  
    51  	block, err := aes.NewCipher(key)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  
    56  	// ECB mode always works in whole blocks.
    57  	if len(ciphertext)%aes.BlockSize != 0 {
    58  		panic("ciphertext is not a multiple of the block size")
    59  	}
    60  
    61  	mode := cipher.NewECBDecrypter(block)
    62  
    63  	// CryptBlocks can work in-place if the two arguments are the same.
    64  	mode.CryptBlocks(ciphertext, ciphertext)
    65  
    66  	// If the original plaintext lengths are not a multiple of the block
    67  	// size, padding would have to be added when encrypting, which would be
    68  	// removed at this point. For an example, see
    69  	// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. However, it's
    70  	// critical to note that ciphertexts must be authenticated (i.e. by
    71  	// using crypto/hmac) before being decrypted in order to avoid creating
    72  	// a padding oracle.
    73  
    74  	fmt.Printf("%s\n", ciphertext)
    75  	// Output: exampleplaintextexampleplaintext
    76  }