Golang DES加密实现与Python代码对比
{"title":"Golang DES加密实现与Python代码对比","description":"本文展示了使用Golang实现DES加密的代码,并与Python代码进行对比。代码使用DES算法进行加密,并使用PKCS7填充模式。","keywords":"Golang, DES加密, Python, 加密, PKCS7填充, 代码对比","content":"\u003ccode\u003epackage main\n\nimport (\n\t"crypto/cipher"\n\t"crypto/des"\n\t"encoding/hex"\n)\n\nfunc desEncrypt(key []byte, plaintext []byte) ([]byte, error) {\n\tblock, err := des.NewCipher(key)\n\tif err != nil {\n\t return nil, err\n\t}\n\n\tciphertext := make([]byte, len(plaintext))\n\tblockMode := cipher.NewCBCEncrypter(block, key)\n\tblockMode.CryptBlocks(ciphertext, plaintext)\n\n\treturn ciphertext, nil\n}\n\nfunc main() {\n\tkey := []byte("51434574")\n\tplaintext := []byte("Hello, World!")\n\n\tciphertext, err := desEncrypt(key, plaintext)\n\tif err != nil {\n\t panic(err)\n\t}\n\n\tencoded := hex.EncodeToString(ciphertext)\n\tprintln(encoded)\n}\n\u003c/code\u003e\n\n请注意,Go语言的DES加密函数默认使用PKCS7填充(与Python的PAD_PKCS5填充相同),因此无需指定填充模式。"}
原文地址: https://www.cveoy.top/t/topic/poSL 著作权归作者所有。请勿转载和采集!