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:
- https://anycript.com/crypto/des
- https://emn178.github.io/online-tools/des/encrypt/
- https://www.toolhelper.cn/en/SymmetricEncryption/DES
3DES online tools:
Index ¶
- func CopyCTR(key, iv []byte, dst io.Writer, src io.Reader) error
- func CopyCTR3(key, iv []byte, dst io.Writer, src io.Reader) error
- func CopyOFB(key, iv []byte, dst io.Writer, src io.Reader) errordeprecated
- func CopyOFB3(key, iv []byte, dst io.Writer, src io.Reader) errordeprecated
- func DecryptCBC(key []byte, ciphertext []byte) ([]byte, error)
- func DecryptCBC3(key []byte, ciphertext []byte) ([]byte, error)
- func DecryptCFB(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func DecryptCFB3(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func DecryptCTR(key []byte, ciphertext []byte) ([]byte, error)
- func DecryptCTR3(key []byte, ciphertext []byte) ([]byte, error)
- func DecryptECB(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func DecryptECB3(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func DecryptOFB(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func DecryptOFB3(key []byte, ciphertext []byte) ([]byte, error)deprecated
- func EncryptCBC(key []byte, plaintext []byte) ([]byte, error)
- func EncryptCBC3(key []byte, plaintext []byte) ([]byte, error)
- func EncryptCFB(key []byte, plaintext []byte) ([]byte, error)deprecated
- func EncryptCFB3(key []byte, plaintext []byte) ([]byte, error)deprecated
- func EncryptCTR(key []byte, plaintext []byte) ([]byte, error)
- func EncryptCTR3(key []byte, plaintext []byte) ([]byte, error)
- func EncryptECB(key []byte, plaintext []byte) ([]byte, error)deprecated
- func EncryptECB3(key []byte, plaintext []byte) ([]byte, error)deprecated
- func EncryptOFB(key []byte, plaintext []byte) ([]byte, error)deprecated
- func EncryptOFB3(key []byte, plaintext []byte) ([]byte, error)deprecated
- type ErrCipherLength
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CopyCTR ¶
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 ¶
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 CopyOFB3
deprecated
func DecryptCBC ¶
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 ¶
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
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
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 ¶
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 ¶
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
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
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
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
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 ¶
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: DES CBC encrypted plaintext (PKCS#7 padding applied).
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 ¶
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: 3DES CBC encrypted plaintext (PKCS#7 padding applied).
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
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: DES CFB encrypted plaintext.
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
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: 3DES CFB encrypted plaintext.
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 ¶
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: DES CTR encrypted plaintext.
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 ¶
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: 3DES CTR encrypted plaintext.
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
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
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
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: DES OFB encrypted plaintext.
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
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:
- Initial 8 bytes (crypto/des.BlockSize): Initial vector generated using crypto/rand.Read.
- Rest of the bytes: 3DES OFB encrypted plaintext.
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