ecb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2022 License: MIT Imports: 2 Imported by: 0

README

ecb

Go Reference

Warning Never use this in a production system. ECB mode is not secure.

This package implements the electronic codebook (ECB) block cipher mode, which is missing from Go's standard library.

It satisfies all the requirements of the crypto/cipher.BlockMode interface.

Documentation

Overview

Package ecb implements the electronic codebook (ECB) block cipher mode.

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 cipher.Block.

Example
package main

import (
	"crypto/aes"
	"fmt"

	"github.com/clfs/ecb"
)

func main() {
	key := []byte{
		0x65, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x65,
		0x67, 0x67, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64,
	}
	ciphertext := []byte{
		0x18, 0x8d, 0xc8, 0xb9, 0x28, 0xae, 0x0a, 0x13,
		0xb0, 0xe9, 0xab, 0x01, 0x12, 0x7f, 0xd3, 0x41,
	}

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

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

	fmt.Printf("%s\n", plaintext)
}
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 cipher.Block.

Example
package main

import (
	"crypto/aes"
	"fmt"

	"github.com/clfs/ecb"
)

func main() {
	key := []byte{
		0x65, 0x61, 0x73, 0x74, 0x65, 0x72, 0x20, 0x65,
		0x67, 0x67, 0x20, 0x66, 0x6f, 0x75, 0x6e, 0x64,
	}

	plaintext := []byte("exampleplaintext")
	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)
	}

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

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

188dc8b928ae0a13b0e9ab01127fd341

Types

This section is empty.

Jump to

Keyboard shortcuts

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