ecb2go

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2025 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Electronic Codebook (ECB) mode in Go

Go Reference License Security

ecb2go is a Go implementation of the Electronic Codebook (ECB) block cipher mode.

This package provides an interface compatible with Go’s standard library cipher.BlockMode.

Security warning

The Electronic Codebook (ECB) mode is considered insecure and should be used only for compatibility with legacy systems, not for security.

Installation

go get github.com/antoniorauso/ecb2go

Examples

For complete examples, see the tests in this package.

License

Distributed under the BSD-3-Clause license. See LICENSE for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDecrypter

func NewDecrypter(b cipher.Block) cipher.BlockMode

NewDecrypter returns a cipher.BlockMode which decrypts in electronic codebook mode, using the given Block.

Example
package main

import (
	"crypto/aes"
	"encoding/hex"
	"fmt"

	"github.com/antoniorauso/ecb2go"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	ciphertext, _ := hex.DecodeString("f42512e1e4039213bd449ba47faa1b74")

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	if len(ciphertext) < aes.BlockSize {
		panic("ciphertext too short")
	}

	// ECB mode always works in whole blocks.
	if len(ciphertext)%aes.BlockSize != 0 {
		panic("ciphertext is not a multiple of the block size")
	}

	mode := ecb2go.NewDecrypter(block)

	// CryptBlocks can work in-place if the two arguments are the same.
	mode.CryptBlocks(ciphertext, ciphertext)

	// If the original plaintext lengths are not a multiple of the block
	// size, padding would have to be added when encrypting, which would be
	// removed at this point. For an example, see
	// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. However, it's
	// critical to note that ciphertexts must be authenticated (i.e. by
	// using crypto/hmac) before being decrypted in order to avoid creating
	// a padding oracle.

	fmt.Printf("%s\n", ciphertext)
}
Output:

exampleplaintext

func NewEncrypter

func NewEncrypter(b cipher.Block) cipher.BlockMode

NewEncrypter returns a cipher.BlockMode which encrypts in electronic codebook mode, using the given Block.

Example
package main

import (
	"crypto/aes"
	"encoding/hex"
	"fmt"

	"github.com/antoniorauso/ecb2go"
)

func main() {
	// Load your secret key from a safe place and reuse it across multiple
	// NewCipher calls. (Obviously don't use this example key for anything
	// real.) If you want to convert a passphrase to a key, use a suitable
	// package like bcrypt or scrypt.
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	plaintext := []byte("exampleplaintext")

	// ECB mode works on blocks so plaintexts may need to be padded to the
	// next whole block. For an example of such padding, see
	// https://tools.ietf.org/html/rfc5246#section-6.2.3.2. Here we'll
	// assume that the plaintext is already of the correct length.
	if len(plaintext)%aes.BlockSize != 0 {
		panic("plaintext is not a multiple of the block size")
	}

	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err)
	}

	ciphertext := make([]byte, len(plaintext))
	mode := ecb2go.NewEncrypter(block)
	mode.CryptBlocks(ciphertext, plaintext)

	// It's important to remember that ciphertexts must be authenticated
	// (i.e. by using crypto/hmac) as well as being encrypted in order to
	// be secure.

	fmt.Printf("%x\n", ciphertext)
}

Types

This section is empty.

Directories

Path Synopsis
internal
alias
Package alias implements memory aliasing tests.
Package alias implements memory aliasing tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL