package main

import ( "bytes" "crypto/cipher" "crypto/des" "encoding/hex" "fmt" )

func desEncrypt(key []byte, plaintext []byte) ([]byte, error) { block, err := des.NewCipher(key) if err != nil { return nil, err } blockSize := block.BlockSize() plaintext = PKCS5Padding(plaintext, blockSize) ciphertext := make([]byte, len(plaintext)) blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) blockMode.CryptBlocks(ciphertext, plaintext) return ciphertext, nil }

func PKCS5Padding(src []byte, blockSize int) []byte { padding := blockSize - len(src)%blockSize padText := bytes.Repeat([]byte{byte(padding)}, padding) return append(src, padText...) }

func main() { key := []byte('51434574') plaintext := []byte('Hello, World!')

ciphertext, err := desEncrypt(key, plaintext)
if err != nil {
	panic(err)
}

encoded := hex.EncodeToString(ciphertext)
fmt.Println(encoded)

}


原文地址: https://www.cveoy.top/t/topic/poKJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录