How to use DES in golang
This article will explain in detail how to use DES in golang. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Package utilimport ("bytes"crypto/cipher"crypto/des"encoding/base64") var (iv = [] byte {1, 2, 3, 4, 5, 6, 7, 8} key = [] byte ("java")) / / DES encryption func DesEncrypt (data [] byte) string {result, _: = DesEncrypt (data, key) Iv) return base64.StdEncoding.EncodeToString (result)} / / DES decrypt func DesDecrypt (base64Str string) string {result, err: = base64.StdEncoding.DecodeString (base64Str) if err! = nil {return ""} origData, err: = DesDecrypt (result, key, iv) if err! = nil {return ""} return string (origData)} func DesEncrypt (origData, key) Iv [] byte) ([] byte, error) {block, err: = des.NewCipher (key) if err! = nil {return nil, err} origData = PKCS5Padding (origData, block.BlockSize () blockMode: = cipher.NewCBCEncrypter (block, iv) crypted: = make ([] byte, len (origData)) blockMode.CryptBlocks (crypted, origData) return crypted, nil} func DesDecrypt (crypted Key, iv [] byte) ([] byte, error) {block, err: = des.NewCipher (key) if err! = nil {return nil, err} blockMode: = cipher.NewCBCDecrypter (block, iv) origData: = make ([] byte, len (crypted) blockMode.CryptBlocks (origData, crypted) origData = PKCS5UnPadding (origData) return origData Nil} func ZeroUnPadding (origData [] byte) [] byte {return bytes.TrimRightFunc (origData, func (r rune) bool {return r = = rune (0)}) func PKCS5Padding (ciphertext [] byte, blockSize int) [] byte {padding: = blockSize-len (ciphertext)% blockSize padtext: = bytes.Repeat ([] byte {byte (padding)}, padding) return append (ciphertext) Padtext...)} func PKCS5UnPadding (origData [] byte) [] byte {length: = len (origData) unpadding: = int (origDB [length-1]) return origData [: (length- unpadding)]} about "how to use DES in golang" ends here Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.