欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

golang实现des3加解密

程序员文章站 2024-03-14 10:58:34
...
package main

import (
	"encoding/hex"
	"fmt"
	"github.com/forgoer/openssl"
	"strings"
)

var key = []byte{.......}

func main() {
	mingwen := "112233445566"
	y := encrypt(mingwen)
	fmt.Println(y)

	x2 := "d3fb432f2f71593a7baf1a445c808819"
	x := decrypt(x2)
	fmt.Println(x)

	fmt.Println(y == x2)
}

func decrypt(passwd string) string {
	srcBytes, _ := hex.DecodeString(passwd)
	y, err := openssl.Des3ECBDecrypt(srcBytes, key, openssl.ZEROS_PADDING)
	if err != nil {
		return ""
	}
	return strings.Trim(string(y), "")
}

func encrypt(passwd string) string {
	x, err := openssl.Des3ECBEncrypt([]byte(passwd), key, openssl.ZEROS_PADDING)
	if err != nil {
		return ""
	}
	return hex.EncodeToString(x)
}

相关标签: go golang des3