zdes

package
v0.0.0-alpha.18 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package zdes provides functionality for DES and 3DES encryption/decryption.

DES

  • has 8 bytes block size (See crypto/des.BlockSize)
  • uses 8 bytes key
  • uses 8 bytes initial vector, iv (Except for ECB mode)

3DES

  • has 8 bytes block size (See crypto/des.BlockSize)
  • uses 24 bytes key
  • uses 8 bytes initial vector, iv (Except for ECB mode)

See the following table for modes.

| Mode | Block/Stream | Padding | Use iv | Symmetry? | Status     |
| ---- | ------------ | ------- | ------ | --------- | ---------- |
| ECB  | Block        | PKCS#7  | No     | Symmetry  | Deprecated |
| CBC  | Block        | PKCS#7  | Yes    | Asymmetry |            |
| CFB  | Stream       |         | Yes    | Asymmetry | Deprecated |
| CTR  | Stream       |         | Yes    | Symmetry  |            |
| OFB  | Stream       |         | Yes    | Symmetry  | Deprecated |

If you need to validate DES, you can use openssl:

DES:

[Encryption]

echo -n "${PLAINTEXT}" | openssl enc -des-cbc -K "${KEY}" -iv "${INITIAL_VEC}" | xxd -p
-K  : Hex encoded 8 bytes key.
-iv : Hex encoded 8 bytes initial vector.
modes:
  -des-ecb
  -des-cbc
  -des-cfb
  -des-ofb

Example:
  export PLAINTEXT="plaintext"
  export KEY=$(echo -n "abcd1234" | xxd -p)
  export INITIAL_VEC=$(echo -n "12345678" | xxd -p)
  echo -n "${PLAINTEXT}" | openssl enc -des-cbc -K "${KEY}" -iv "${INITIAL_VEC}" | xxd -p
  >> 1f7c7fc711fb57e74891c07cb0eacc2a

[Decryption]

echo -n "${CIPHERTEXT}" | openssl enc -d -des-cbc -K "${KEY}" -iv "${INITIAL_VEC}"
-K  : Hex encoded 8 bytes key.
-iv : Hex encoded 8 bytes initial vector.

Example:
  export CIPHERTEXT=$(echo -n "1f7c7fc711fb57e74891c07cb0eacc2a" | xxd -p -r)
  export KEY=$(echo -n "abcd1234" | xxd -p)
  export INITIAL_VEC=$(echo -n "12345678" | xxd -p)
  echo -n "${CIPHERTEXT}" | openssl enc -d -des-cbc -K "${KEY}" -iv "${INITIAL_VEC}"
  >> plaintext

3DES:

[Encryption]

echo -n "${PLAINTEXT}" | openssl enc -des-ede3-cbc -K "${KEY}" -iv "${INITIAL_VEC}" | xxd -p
-K  : Hex encoded 24 bytes key.
-iv : Hex encoded 8 bytes initial vector.
modes:
  -des-ede3-ecb
  -des-ede3-cbc
  -des-ede3-cfb
  -des-ede3-ofb

Example:
  export PLAINTEXT="plaintext"
  export KEY=$(echo -n "abcdefghijkl123456789012" | xxd -p)
  export INITIAL_VEC=$(echo -n "12345678" | xxd -p)
  echo -n "${PLAINTEXT}" | openssl enc -des-ede3-cbc -K "${KEY}" -iv "${INITIAL_VEC}" | xxd -p
  >> feea0273aadd2f444d7e4ed28b6c58db

[Decryption]

echo -n "${CIPHERTEXT}" | openssl enc -d -des-ede3-cbc -K "${KEY}" -iv "${INITIAL_VEC}"
-K  : Hex encoded 24 bytes key.
-iv : Hex encoded 8 bytes initial vector.

Example:
  export CIPHERTEXT=$(echo -n "feea0273aadd2f444d7e4ed28b6c58db" | xxd -p -r)
  export KEY=$(echo -n "abcdefghijkl123456789012" | xxd -p)
  export INITIAL_VEC=$(echo -n "12345678" | xxd -p)
  echo -n "${CIPHERTEXT}" | openssl enc -d -des-ede3-cbc -K "${KEY}" -iv "${INITIAL_VEC}"
  >> plaintext

Some online tools available.

DES online tools:

3DES online tools:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyCTR

func CopyCTR(key, iv []byte, dst io.Writer, src io.Reader) error

CopyCTR copies from src to dst. If src is encrypted, then decrypted bytes are written into the dst. If src is not encrypted, then encrypted bytes are written into the dst. Note that the OFB mode (crypto/cipher.NewOFB) is deprecated.

func CopyCTR3

func CopyCTR3(key, iv []byte, dst io.Writer, src io.Reader) error

CopyCTR3 copies from src to dst. If src is encrypted, then decrypted bytes are written into the dst. If src is not encrypted, then encrypted bytes are written into the dst. Note that the OFB mode (crypto/cipher.NewOFB) is deprecated.

func CopyOFB deprecated

func CopyOFB(key, iv []byte, dst io.Writer, src io.Reader) error

CopyOFB copies from src to dst. If src is encrypted, then decrypted bytes are written into the dst. If src is not encrypted, then encrypted bytes are written into the dst.

Deprecated: See the comment on crypto/cipher.NewOFB.

func CopyOFB3 deprecated

func CopyOFB3(key, iv []byte, dst io.Writer, src io.Reader) error

CopyOFB3 copies from src to dst. If src is encrypted, then decrypted bytes are written into the dst. If src is not encrypted, then encrypted bytes are written into the dst.

Deprecated: See the comment on crypto/cipher.NewOFB.

func DecryptCBC

func DecryptCBC(key []byte, ciphertext []byte) ([]byte, error)

DecryptCBC decrypts ciphertext encrypted with DES CBC cipher. The key must be the DES key, 8 bytes. See more details at crypto/des and crypto/cipher.

func DecryptCBC3

func DecryptCBC3(key []byte, ciphertext []byte) ([]byte, error)

DecryptCBC3 decrypts ciphertext encrypted with 3DES CBC cipher. The key must be the 3DES key, 24 bytes. See more details at crypto/des and crypto/cipher.

func DecryptCFB deprecated

func DecryptCFB(key []byte, ciphertext []byte) ([]byte, error)

DecryptCFB decrypts ciphertext encrypted with DES CFC cipher. The key must be the DES key, 8 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: See the comment on crypto/cipher.NewCFBDecrypter.

func DecryptCFB3 deprecated

func DecryptCFB3(key []byte, ciphertext []byte) ([]byte, error)

DecryptCFB3 decrypts ciphertext encrypted with 3DES CFC cipher. The key must be the 3DES key, 24 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: See the comment on crypto/cipher.NewCFBDecrypter.

func DecryptCTR

func DecryptCTR(key []byte, ciphertext []byte) ([]byte, error)

DecryptCTR decrypts ciphertext encrypted with DES CTR cipher. The key must be the DES key, 8 bytes. See more details at crypto/des and crypto/cipher.

func DecryptCTR3

func DecryptCTR3(key []byte, ciphertext []byte) ([]byte, error)

DecryptCTR3 decrypts ciphertext encrypted with 3DES CTR cipher. The key must be the 3DES key, 24 bytes. See more details at crypto/des and crypto/cipher.

func DecryptECB deprecated

func DecryptECB(key []byte, ciphertext []byte) ([]byte, error)

DecryptECB decrypts ciphertext encrypted with DES ECB cipher. The key must be the DES key, 8 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: ECB mode is not recommended because it does not uses iv. Same plaintext is alway be encrypted into the same ciphertext. If block cipher is necessary, use CBC mode instead.

func DecryptECB3 deprecated

func DecryptECB3(key []byte, ciphertext []byte) ([]byte, error)

DecryptECB3 decrypts ciphertext encrypted with 3DES ECB cipher. The key must be the 3DES key, 24 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: ECB mode is not recommended because it does not uses iv. Same plaintext is alway be encrypted into the same ciphertext. If block cipher is necessary, use CBC mode instead.

func DecryptOFB deprecated

func DecryptOFB(key []byte, ciphertext []byte) ([]byte, error)

DecryptOFB decrypts ciphertext encrypted with DES OFB cipher. The key must be the DES key, 8 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: See the comment on crypto/cipher.NewOFB.

func DecryptOFB3 deprecated

func DecryptOFB3(key []byte, ciphertext []byte) ([]byte, error)

DecryptOFB3 decrypts ciphertext encrypted with 3DES OFB cipher. The key must be the 3DES key, 24 bytes. See more details at crypto/des and crypto/cipher.

Deprecated: See the comment on crypto/cipher.NewOFB.

func EncryptCBC

func EncryptCBC(key []byte, plaintext []byte) ([]byte, error)

EncryptCBC encrypts plaintext with DES CBC cipher. The key must be the DES key, 8 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("12345678") // 8 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCBC(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCBC(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
31323334353637385194bfcb8072ea4b1e3d83016ef65d8a
plaintext

func EncryptCBC3

func EncryptCBC3(key []byte, plaintext []byte) ([]byte, error)

EncryptCBC3 encrypts plaintext with 3DES CBC cipher. The key must be the 3DES key, 24 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("123456789012345678901234") // 24 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCBC3(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCBC3(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
3132333435363738f68b09f9c8e1893b8682059b7db02962
plaintext

func EncryptCFB deprecated

func EncryptCFB(key []byte, plaintext []byte) ([]byte, error)

EncryptCFB encrypts plaintext with DES CFB cipher. The key must be the DES key, 8 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Deprecated: See the comment on crypto/cipher.NewCFBEncrypter.

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("12345678") // 8 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCFB(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCFB(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
3132333435363738e6bc63e116a1e9f15c
plaintext

func EncryptCFB3 deprecated

func EncryptCFB3(key []byte, plaintext []byte) ([]byte, error)

EncryptCFB3 encrypts plaintext with 3DES CFB cipher. The key must be the 3DES key, 24 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Deprecated: See the comment on crypto/cipher.NewCFBEncrypter.

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("123456789012345678901234") // 24 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCFB3(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCFB3(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
31323334353637386db9685cd9766d33a2
plaintext

func EncryptCTR

func EncryptCTR(key []byte, plaintext []byte) ([]byte, error)

EncryptCTR encrypts plaintext with DES CTR cipher. The key must be the DES key, 8 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("12345678") // 8 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCTR(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCTR(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
3132333435363738e6bc63e116a1e9f135
plaintext

func EncryptCTR3

func EncryptCTR3(key []byte, plaintext []byte) ([]byte, error)

EncryptCTR3 encrypts plaintext with 3DES CTR cipher. The key must be the 3DES key, 24 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("123456789012345678901234") // 24 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptCTR3(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptCTR3(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
31323334353637386db9685cd9766d33d3
plaintext

func EncryptECB deprecated

func EncryptECB(key []byte, plaintext []byte) ([]byte, error)

EncryptECB encrypts plaintext with DES ECB cipher. The key must be the DES key, 8 bytes. ECB mode does not use iv. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

  • All parts: DES ECB encrypted plaintext (PKCS#7 padding applied).

Deprecated: ECB mode is not recommended because it does not uses iv. Same plaintext is alway be encrypted into the same ciphertext. If block cipher is necessary, use CBC mode instead.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	key := []byte("12345678") // 8 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptECB(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext))

	// Decrypt
	decrypted, _ := zdes.DecryptECB(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
5c74163a9d085a2bf035d51988b42379
plaintext

func EncryptECB3 deprecated

func EncryptECB3(key []byte, plaintext []byte) ([]byte, error)

EncryptECB3 encrypts plaintext with 3DES ECB cipher. The key must be the 3DES key, 24 bytes. ECB mode does not use iv. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

  • All parts: 3DES ECB encrypted plaintext (PKCS#7 padding applied).

Deprecated: ECB mode is not recommended because it does not uses iv. Same plaintext is alway be encrypted into the same ciphertext. If block cipher is necessary, use CBC mode instead.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	key := []byte("123456789012345678901234") // 24 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptECB3(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext))

	// Decrypt
	decrypted, _ := zdes.DecryptECB3(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
5f05e14052c798b8c6bf86b2c0295a1f
plaintext

func EncryptOFB deprecated

func EncryptOFB(key []byte, plaintext []byte) ([]byte, error)

EncryptOFB encrypts plaintext with DES OFB cipher. The key must be the DES key, 8 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Deprecated: See the comment on crypto/cipher.NewOFB.

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("12345678") // 8 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptOFB(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptOFB(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
3132333435363738e6bc63e116a1e9f19d
plaintext

func EncryptOFB3 deprecated

func EncryptOFB3(key []byte, plaintext []byte) ([]byte, error)

EncryptOFB3 encrypts plaintext with 3DES OFB cipher. The key must be the 3DES key, 24 bytes. 8 bytes iv read from crypto/rand.Reader is used. See more details at crypto/des and crypto/cipher.

Returned slice consists of:

Deprecated: See the comment on crypto/cipher.NewOFB.

Example
package main

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"fmt"

	"github.com/aileron-projects/go/zcrypto/zdes"
)

func main() {
	tmp := rand.Reader
	rand.Reader = bytes.NewReader([]byte("12345678")) // Fix value for test.
	defer func() { rand.Reader = tmp }()

	key := []byte("123456789012345678901234") // 24 bytes.
	plaintext := []byte("plaintext")

	// Encrypt
	ciphertext, _ := zdes.EncryptOFB3(key, plaintext)
	fmt.Println(hex.EncodeToString(ciphertext)) // iv is left side of the output.

	// Decrypt
	decrypted, _ := zdes.DecryptOFB3(key, ciphertext)
	fmt.Println(string(decrypted))

}
Output:
31323334353637386db9685cd9766d337e
plaintext

Types

type ErrCipherLength

type ErrCipherLength int

ErrCipherLength is an error type that tells the length of ciphertext is incorrect.

func (ErrCipherLength) Error

func (e ErrCipherLength) Error() string

Jump to

Keyboard shortcuts

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