ciphers

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ciphers provides implementations of various classical and modern encryption ciphers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetShift

func GetShift(char byte) byte

GetShift normalizes ASCII a-zA-Z letters to 0-25, where A is 0 and Z is 25.

If the provided char is not a-zA-Z, returns the char.

func IsASCIILetter

func IsASCIILetter(char byte) bool

IsASCIILetter checks whether the provided char is an ASCII a-zA-Z letter.

func NormalizeVigenereKey

func NormalizeVigenereKey(key []byte) ([]byte, error)

NormalizeVigenereKey normalizes a Vigenere key by converting all letters to lowercase and returning only the shift values (a=0, b=1, ..., z=25).

Returns an error if the key is empty or contains non-letter characters.

func ValidateCardanKey

func ValidateCardanKey(gridKey *CardanKey, gridSize int) error

ValidateCardanKey validates that a CardanKey is valid for the given grid size.

It checks if:

  1. the key has the correct length
  2. all indices are in bounds,
  3. there are no duplicates
  4. there are no overlaps when rotated
  5. all non-center cells are covered.

Returns an error if the key is invalid.

Types

type BlockCipher

type BlockCipher interface {
	/*
		IsInPlace returns whether the cipher can perform encryption/decryption in-place.

		In-place operations allow src and dst slices to alias.
	*/
	IsInPlace() bool
	/*
		EncryptBlock encrypts src and writes the result to dst.

		If implementation allows for in-place operations, dst and src can alias.

		src and dst must be the same length; the expected length is
		implementation-defined — consult the concrete type for details.
	*/
	EncryptBlock(dst []byte, src []byte) error
	/*
		DecryptBlock decrypts src and writes the result to dst.

		If implementation allows for in-place operations, dst and src can alias.

		src and dst must be the same length; the expected length is
		implementation-defined — consult the concrete type for details.
	*/
	DecryptBlock(dst []byte, src []byte) error
}

BlockCipher is the interface that all block ciphers must implement.

It provides methods for encryption and decryption of fixed-size blocks.

type CaesarCipher

type CaesarCipher struct {
	Key               byte
	SubstitutionTable [256]byte
	ReverseTable      [256]byte
}

CaesarCipher is a substitution cipher that shifts letters by a fixed key.

It uses precomputed substitution and reverse tables for efficient encryption/decryption.

Example:

Key:        3
Plaintext:  Hello
-----------------
+H e l l o
 3 3 3 3 3
 K h o o r
-----------------
Ciphertext: Khoor

func NewCaesarCipher

func NewCaesarCipher(key int) (*CaesarCipher, error)

NewCaesarCipher creates a new Caesar cipher with the given key.

The key is the number of positions to shift letters (keys are reduced modulo 26).

Returns an error if the key is negative.

func (*CaesarCipher) DecryptBlock

func (cc *CaesarCipher) DecryptBlock(dst []byte, src []byte) error

DecryptBlock decrypts src using the Caesar cipher and writes the result to dst.

src and dst can alias, because Caesar cipher performs operations in-place.

src and dst must be the same length.

func (*CaesarCipher) EncryptBlock

func (cc *CaesarCipher) EncryptBlock(dst []byte, src []byte) error

EncryptBlock encrypts src using the Caesar cipher and writes the result to dst.

src and dst can alias, because Caesar cipher performs operations in-place.

src and dst must be the same length.

func (*CaesarCipher) IsInPlace

func (cc *CaesarCipher) IsInPlace() bool

IsInPlace returns whether the cipher can perform encryption/decryption in-place.

Caesar cipher supports in-place operations.

type CardanCipher

type CardanCipher struct {
	PermutationTable []int
}

CardanCipher is a grille-based transposition cipher that uses a rotating grid (Cardan grille) to encrypt messages.

The cipher works by placing holes in a grid pattern and rotating the grid four times to mark which positions are "holes".

func NewCardanCipher

func NewCardanCipher(gridKey *CardanKey, gridSize int) (*CardanCipher, error)

NewCardanCipher creates a new Cardan cipher with the given key and grid size.

The gridSize must be positive.

If gridKey is nil, a random key will be generated.

If gridKey is provided, it will be validated first.

The key is not saved.

Returns an error if gridSize is invalid or the key is invalid.

func (*CardanCipher) DecryptBlock

func (cCipher *CardanCipher) DecryptBlock(dst []byte, src []byte) error

DecryptBlock decrypts src using the Cardan cipher and writes the result to dst.

src and dst cannot alias.

src and dst must be the same length and must match gridSize*gridSize.

func (*CardanCipher) EncryptBlock

func (cCipher *CardanCipher) EncryptBlock(dst []byte, src []byte) error

EncryptBlock encrypts src using the Cardan cipher and writes the result to dst.

src and dst cannot alias.

src and dst must be the same length and must match gridSize*gridSize.

func (*CardanCipher) IsInPlace

func (cCipher *CardanCipher) IsInPlace() bool

IsInPlace returns whether the cipher can perform encryption/decryption in-place.

Cardan cipher does not support in-place operations since bytes are written to non-sequential positions, requiring a separate destination buffer.

type CardanKey

type CardanKey struct {
	Key []int `json:"key"`
}

CardanKey represents the key for a Cardan grille cipher.

It contains a list of indexes that form the initial hole pattern.

func GenerateCardanKey

func GenerateCardanKey(gridSize int) (*CardanKey, error)

GenerateCardanKey generates a random valid CardanKey for the given grid size.

The grid size must be positive.

Returns an error if key generation fails.

func (*CardanKey) String

func (cK *CardanKey) String() string

String returns a JSON string representation of the CardanKey.

If JSON string representation fails falls back to fmt.Sprintf formatting.

type CipherMode

type CipherMode int8

CipherMode represents the mode of operation for a cipher, either encryption or decryption.

const (
	// Encrypt indicates encryption mode.
	Encrypt CipherMode = iota
	// Decrypt indicates decryption mode.
	Decrypt
)

func (CipherMode) String

func (mode CipherMode) String() string

String returns the string representation of the CipherMode.

type RailFenceCipher

type RailFenceCipher struct {
	Key              int
	PermutationTable []int
}

RailFenceCipher is a transposition cipher that writes plaintext in a zigzag pattern across a number of "rails" and then reads off row by row.

The key specifies the number of rails to use.

Example:

Key:        3
Plaintext:  HelloWorld
----------------------
  l   o
 e l w r d
H   o   l
----------------------
Ciphertext: loelwrdHol

func NewRailFenceCipher

func NewRailFenceCipher(key int, blockSize int) (*RailFenceCipher, error)

NewRailFenceCipher creates a new Rail Fence cipher with the given key and block size.

The key is the number of rails (must be >= 1).

If key is 0, PermutationTable becomes nil. EncryptBlock() and DecryptBlock() handle the key == 1 case separately.

The block size is the size of plaintext blocks (must be > 0).

Returns an error if the key is < 1 or block size is <= 0.

func (*RailFenceCipher) DecryptBlock

func (rfCipher *RailFenceCipher) DecryptBlock(dst []byte, src []byte) error

DecryptBlock decrypts src using the Rail Fence cipher and writes the result to dst.

src and dst cannot alias.

src and dst must be the same length and must match the blockSize used when creating the cipher.

func (*RailFenceCipher) EncryptBlock

func (rfCipher *RailFenceCipher) EncryptBlock(dst []byte, src []byte) error

EncryptBlock encrypts src using the Rail Fence cipher and writes the result to dst.

src and dst cannot alias.

src and dst must be the same length and must match the blockSize used when creating the cipher.

func (*RailFenceCipher) IsInPlace

func (rfCipher *RailFenceCipher) IsInPlace() bool

IsInPlace returns whether the cipher can perform encryption/decryption in-place.

Rail Fence cipher does not support in-place operations since bytes are written to non-sequential positions, requiring a separate destination buffer.

type VigenereAutoKeyCipher

type VigenereAutoKeyCipher struct {
	VigenereCipher
}

VigenereAutoKeyCipher is a variant of the Vigenere cipher that uses the plaintext itself (after the key) as the key for subsequent encryptions, providing stronger security than the standard Vigenere cipher.

Example:

Key:        Cat
Plaintext:  HelloWorld
---------------------
+H e l l o W o r l d
 C a t H e l l o W o
 J e e s s H z f h r
---------------------
Ciphertext: JeessHzfhr

func NewVigenereAutoKeyCipher

func NewVigenereAutoKeyCipher(key []byte) (*VigenereAutoKeyCipher, error)

NewVigenereAutoKeyCipher creates a new Vigenere Auto-Key cipher with the given key.

The key must be a non-empty string of ASCII letters.

Returns an error if the key is invalid.

func NewVigenereAutoKeyCipherNormalized

func NewVigenereAutoKeyCipherNormalized(normalizedKey []byte) *VigenereAutoKeyCipher

NewVigenereAutoKeyCipherNormalized creates a new Vigenere Auto-Key cipher with an already normalized key.

func (*VigenereAutoKeyCipher) DecryptBlock

func (vCipher *VigenereAutoKeyCipher) DecryptBlock(dst []byte, src []byte) error

DecryptBlock decrypts src using the Vigenere Auto-Key cipher and writes the result to dst.

The key cycles over letter characters only; non-letter characters are passed through unchanged.

src and dst cannot alias.

src and dst must be the same length.

func (*VigenereAutoKeyCipher) EncryptBlock

func (vCipher *VigenereAutoKeyCipher) EncryptBlock(dst []byte, src []byte) error

EncryptBlock encrypts src using the Vigenere Auto-Key cipher and writes the result to dst.

The key cycles over letter characters only; non-letter characters are passed through unchanged.

src and dst cannot alias.

src and dst must be the same length.

func (*VigenereAutoKeyCipher) IsInPlace

func (vCipher *VigenereAutoKeyCipher) IsInPlace() bool

IsInPlace returns whether the cipher can perform encryption/decryption in-place.

Vigenere Auto-Key cipher does not support in-place operations since decryption reads from dst as key material before it is fully written.

type VigenereCipher

type VigenereCipher struct {
	Key []byte
}

VigenereCipher is a polyalphabetic substitution cipher that uses a keyword to encrypt messages by shifting each letter by a different amount based on the corresponding letter in the key.

Example:

Key:        Cat
Plaintext:  HelloWorld
---------------------
+H e l l o W o r l d
 C a t C a t C a t C
 J e e n o P q r e f
---------------------
Ciphertext: JeenoPqref

func NewVigenereCipher

func NewVigenereCipher(key []byte) (*VigenereCipher, error)

NewVigenereCipher creates a new Vigenere cipher with the given key.

The key must be a non-empty string of ASCII letters.

Returns an error if the key is invalid.

func NewVigenereCipherNormalized

func NewVigenereCipherNormalized(normalizedKey []byte) *VigenereCipher

NewVigenereCipherNormalized creates a new Vigenere cipher with an already normalized key.

func (*VigenereCipher) DecryptBlock

func (vCipher *VigenereCipher) DecryptBlock(dst []byte, src []byte) error

DecryptBlock decrypts src using the Vigenere cipher and writes the result to dst.

The key cycles over letter characters only; non-letter characters are passed through unchanged.

src and dst can alias, because Vigenere cipher performs operations in-place.

src and dst must be the same length.

func (*VigenereCipher) EncryptBlock

func (vCipher *VigenereCipher) EncryptBlock(dst []byte, src []byte) error

EncryptBlock encrypts src using the Vigenere cipher and writes the result to dst.

The key cycles over letter characters only; non-letter characters are passed through unchanged.

src and dst can alias, because Vigenere cipher performs operations in-place.

src and dst must be the same length.

func (*VigenereCipher) IsInPlace

func (vCipher *VigenereCipher) IsInPlace() bool

IsInPlace returns whether the cipher can perform encryption/decryption in-place.

Vigenere cipher supports in-place operations.

Directories

Path Synopsis
Package analyze provides functionality for cryptanalysis of classical ciphers.
Package analyze provides functionality for cryptanalysis of classical ciphers.

Jump to

Keyboard shortcuts

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