Standard library / crypto/cipher

crypto/cipher.BlockMode

Legacy interoperability only; this mode provides no authentication. Prefer AEAD for new designs. Fixed key and IV below are public test fixtures, never deployment values. BlockSize reports the block length; CryptBlocks processes complete blocks and maintains chaining state between calls. Output must fit and overlap must be exact or absent. Expected stdout: '16 16\n'.

Formal Syntax & Structure

go syntaxgo
type BlockMode interface {  BlockSize() int
  CryptBlocks(dst, src []byte)}

Working Example

Example Snippetgo
package main
import (  "crypto/aes"  "crypto/cipher"  "fmt")
func main() {  // Historical interoperability demonstration only. Never deploy this cipher/mode.  b := must(aes.NewCipher(make([]byte, 16)))  iv := make([]byte, 16)  plain := make([]byte, 16)  encrypted := make([]byte, 16)  var m cipher.BlockMode = cipher.NewCBCEncrypter(b, iv)  m.CryptBlocks(encrypted, plain)  fmt.Println(m.BlockSize(), len(encrypted))}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference