Standard library / crypto/cipher
crypto/cipher.NewCTR()
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. CTR requires unique counter blocks for a given key; never overlap counter ranges. Stateful stream processes arbitrary byte counts. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func NewCTR(block Block, iv []byte) StreamParameters & Attributes
| Name | Type | Description |
|---|---|---|
| block | Block | Underlying block cipher. |
| iv | []byte | Block-sized IV or initial counter. |
Return Value
Stream
Stateful cipher stream.
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) p := []byte("Go") enc := make([]byte, len(p)) cipher.NewCTR(b, iv).XORKeyStream(enc, p) cipher.NewCTR(b, iv).XORKeyStream(p, enc) fmt.Println(string(p))}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) }}